2014-03-05 108 views
12

我有一個數組字段一堆文件,像這樣:Elasticsearch條款過濾器不返回任何結果

{ "feed_uids": ["math.CO", "cs.IT"] } 

我想找到包含這些值即的某個子集把他們當作標籤的所有文件。文檔使我相信一個條款過濾器應該工作:

{ "query": { "filtered": { "filter": { "terms": { "feed_uids": [ "cs.IT" ] } } } } } 

但是,查詢不匹配任何內容。我究竟做錯了什麼?

+0

如果你正在做一個條件過濾器,你要過濾的列應該被分析。是否要設置feeds_uids字段進行分析?如果是的話,它使用什麼分析儀?您可以測試將字符串cs.IT傳遞到分析器時返回哪些令牌。您可以通過執行以下操作來測試分析儀的返回數據:localhost:9200/{您的索引名稱}/_ analyze?analyzer = {您的分析儀名稱}&text = cs.IT。 –

+0

我剛剛通過默認的雪球分析器運行您的文本,並獲得了以下令牌響應{「令牌」:「cs.it」,「start_offset」:0,「end_offset」:5,「type 「:」「,」position「:1}]}所以文本的標記似乎是正確的,所以我可以想象的唯一的其他事情是您的feed_uids字段未設置爲分析。 –

回答

16

terms -filter按照您的預期工作。我想你的問題在於你有一個映射,其中feed_uids正在使用標準分析器。

這是在多一點深度這裏描述的一個相當普遍的問題:Troubleshooting Elasticsearch searches, for Beginners

這裏是一個可運行的例子,展示,如果您的字段中指定"index": "not_analyzed"它是如何工作的:https://www.found.no/play/gist/bc957d515597ec8262ab

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ 
    "mappings": { 
     "type": { 
      "properties": { 
       "feed_uids": { 
        "type": "string", 
        "index": "not_analyzed" 
       } 
      } 
     } 
    } 
}' 

# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"feed_uids":["math.CO","cs.IT"]} 
{"index":{"_index":"play","_type":"type"}} 
{"feed_uids":["cs.IT"]} 
' 

# Do searches 

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "filtered": { 
      "filter": { 
       "terms": { 
        "feed_uids": [ 
         "cs.IT" 
        ] 
       } 
      } 
     } 
    } 
} 
'