检查弹性搜索中的列表字段
Check against list field in elastic search
我有一个值为列表的字段。例如
_id = 1
tags = ["IT", "mobile", "OS"]
_id = 2
tags = ["Mac", "fast", "laptop"]
_id = 3
tags = ["IT", "android", "OS"]
我得到了一个列表来检查标签字段。
sample = ["OS", "opera", "mobile"]
所以当我使用示例集查询标签时,带有 id 1
和 id 3
的文档应该匹配。(原因 id 1
包含 "OS"
和 "mobile"
和id 3
conatins "OS"
.)
我如何在弹性搜索上执行此操作?
使用 "should" keyword.
尝试以下布尔查询
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "tags": "OS" }},
{ "match": { "tags": "opera" }},
{ "match": { "tags": "mobile" }}
]
}
}
}
可能是进一步查询的重要说明:如果您计划搜索包含所有 3 个值(但不是 2 个或仅 1 个标签)的对象,您绝对应该首先阅读 Elasticsearch 中的 nested objects。
试试这个
GET /_search
{
"query": {
"bool": {
"should": [
{
"terms":
{
"tags": ["OS", "opera","mobile"]
}
}
]
}
}
}
我有一个值为列表的字段。例如
_id = 1
tags = ["IT", "mobile", "OS"]
_id = 2
tags = ["Mac", "fast", "laptop"]
_id = 3
tags = ["IT", "android", "OS"]
我得到了一个列表来检查标签字段。
sample = ["OS", "opera", "mobile"]
所以当我使用示例集查询标签时,带有 id 1
和 id 3
的文档应该匹配。(原因 id 1
包含 "OS"
和 "mobile"
和id 3
conatins "OS"
.)
我如何在弹性搜索上执行此操作?
使用 "should" keyword.
尝试以下布尔查询GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "tags": "OS" }},
{ "match": { "tags": "opera" }},
{ "match": { "tags": "mobile" }}
]
}
}
}
可能是进一步查询的重要说明:如果您计划搜索包含所有 3 个值(但不是 2 个或仅 1 个标签)的对象,您绝对应该首先阅读 Elasticsearch 中的 nested objects。
试试这个
GET /_search
{
"query": {
"bool": {
"should": [
{
"terms":
{
"tags": ["OS", "opera","mobile"]
}
}
]
}
}
}