Elasticsearch匹配给定数组中的所有标签

目前正在使用elasticsearch开发一个标签search应用程序,我已经给索引中的每个文档提供了一个标签数组,下面是一个文档外观的例子:

_source: { title: "Keep in touch scheme", intro: "<p>hello this is a test</p> ", full: " <p>again this is a test mate</p>", media: "", link: "/training/keep-in-touch", tags: [ "employee", "training" ] } 

我希望能够进行search,并只返回所有指定标签的文档。

使用上面的例子,如果我search了一个标签["employee", "training"]的文档,那么上面的结果将被返回。

相反,如果我用标签["employee", "other"] ,则不会返回任何内容。 search查询中的所有标签都必须匹配。

目前我在做:

 query: { bool: { must: [ { match: { tags: ["employee","training"] }} ] } } 

但我只是得到像返回的exception

 IllegalStateException[Can't get text on a START_ARRAY at 1:128]; 

我也尝试连接数组和使用逗号分隔的string,但是这似乎匹配任何给定的第一个标签匹配。

有关如何解决这个问题的任何build议? 干杯

选项1:下一个例子应该工作(v2.3.2):

 curl -XPOST 'localhost:9200/yourIndex/yourType/_search?pretty' -d '{ "query": { "bool": { "must": [ { "term": { "tags": "employee" } } , { "term": { "tags": "training" } } ] } } }' 

选项2:你也可以尝试:

 curl -XPOST 'localhost:9200/yourIndex/yourType/_search?pretty' -d '{ "query": { "filtered": { "query": {"match_all": {}}, "filter": { "terms": { "tags": ["employee", "training"] } } } } }' 

但是,如果没有"minimum_should_match": 1它工作的"minimum_should_match": 1不准确。 我也发现"execution": "and"但它也不准确。

选项3:也是你的猫尝试query_string它完美的作品,但看起来有点复杂:

 curl -XPOST 'localhost:9200/yourIndex/yourType/_search?pretty' -d '{ "query" : { "query_string": { "query": "(tags:employee AND tags:training)" } } }' 

也许它会对你有帮助…

为确保该集合仅包含指定的值,请维护一个辅助字段以跟踪标签数量。 然后你可以像下面这样查询来得到想要的结果

 "query":{ "bool":{ "must":[ {"term": {"tags": "employee"}}, {"term": {"tags": "training"}}, {"term": {"tag_count": 2}} ] } }