Json Elasticsearch匹配给定数组中的所有标记
目前正在使用elasticsearch开发标记搜索应用程序,我为索引中的每个文档提供了一个标记数组,下面是一个文档外观示例:Json Elasticsearch匹配给定数组中的所有标记,json,node.js,search,elasticsearch,Json,Node.js,Search,elasticsearch,目前正在使用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/k
_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"
]
}
但我只是得到了像这样的异常
IllegalStateException[Can't get text on a START_ARRAY at 1:128];
我还尝试连接数组并使用逗号分隔的字符串,但是如果第一个标记匹配,这似乎与任何内容都匹配
关于如何处理这个问题有什么建议吗?欢呼声选项1:下一个示例应该有效(v2.3.2): 选项2:您也可以尝试:
curl -XPOST 'localhost:9200/yourIndex/yourType/_search?pretty' -d '{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"terms": {
"tags": ["employee", "training"]
}
}
}
}
}'
但是如果没有“最小值应匹配”:1
它工作得很好,但不准确。
我还发现了“execution:”和“
,但它也不准确
选项3:您还可以尝试query\u 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}}
]
}
}
问题是标记的数量可能会有所不同,它并不总是2-理想情况下,我可以将长度不同的数组传递给query@WilliamPaul我添加了另一个示例,但我找不到如何使用
“最小值应匹配”的解决方案:1
在es v2.3.2中…@WilliamPaul我还添加了带有query\u string
的示例。选项3最适合我,只需连接数组并使用一些分隔符,非常感谢。
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}}
]
}
}