当前正在使用elasticsearch开发标签搜索应用程序,我给索引中的每个文档分配了标签数组,这是文档外观的示例:
_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"
]
}
我希望能够进行搜索,并且只返回带有所有指定标签的文档。
使用上面的示例,如果我搜索带有
["employee", "training"]
标签的文档,则将返回以上结果。相反,如果我使用
["employee", "other"]
标签进行搜索,则不会返回任何内容。搜索查询中的所有标签必须匹配。目前我正在做:
query: {
bool: {
must: [
{ match: { tags: ["employee","training"] }}
]
}
}
但我只是得到返回的异常
IllegalStateException[Can't get text on a START_ARRAY at 1:128];
我也尝试连接数组并使用逗号分隔的字符串,但是在第一个标记匹配的情况下,这似乎可以匹配任何内容。
有关如何处理此问题的任何建议?干杯
最佳答案
选项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
,它的工作原理将是不准确的。我也找到了
"execution": "and"
,但它也不太准确。选项3:您也可以尝试使用
query_string
,它非常有效,但看起来有点复杂:curl -XPOST 'localhost:9200/yourIndex/yourType/_search?pretty' -d '{
"query" : {
"query_string": {
"query": "(tags:employee AND tags:training)"
}
}
}'
也许对您有帮助...