2014-03-05 61 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" 
        ] 
       } 
      } 
     } 
    } 
} 
'