检索仅包含允许标记的文档(完全等于)

Iva*_*van 10 elasticsearch

对于每个搜索请求,我都允许标签列表.例如,

["search", "open_source", "freeware", "linux"]
Run Code Online (Sandbox Code Playgroud)

我想要检索此列表中包含所有标签的文档.我想要检索:

{
    "tags": ["search", "freeware"]
}
Run Code Online (Sandbox Code Playgroud)

并排除

{
    "tags": ["search", "windows"]
}
Run Code Online (Sandbox Code Playgroud)

因为list不包含windows标签.

在Elasticsearch文档中有一个完全等于的示例:

https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html

首先,我们包含一个维护标签数量的字段:

{ "tags" : ["search"], "tag_count" : 1 }
{ "tags" : ["search", "open_source"], "tag_count" : 2 }
Run Code Online (Sandbox Code Playgroud)

其次,我们使用需要的tag_count进行检索

GET /my_index/my_type/_search
{
    "query": {
        "filtered" : {
            "filter" : {
                 "bool" : {
                    "must" : [
                        { "term" : { "tags" : "search" } }, 
                        { "term" : { "tags" : "open_source" } }, 
                        { "term" : { "tag_count" : 2 } } 
                    ]
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道tag_count.

此外,我也试着写与查询script_field tags_count,写方面的查询每个允许的标签,并设置minimal_should_matchtags_count,但我不能设置脚本变量中minimal_should_match.

我可以调查什么?

小智 1

所以我承认这不是一个很好的解决方案,但也许它会激发其他更好的解决方案?

您正在搜索的记录的部分内容类似于您帖子中带有 tag_count 字段的内容:

"tags" : ["search"],
"tag_count" : 1
Run Code Online (Sandbox Code Playgroud)

或者

"tags" : ["search", "open_source"],
"tag_count" : 2
Run Code Online (Sandbox Code Playgroud)

并且您有这样的查询:

["search", "open_source", "freeware"]
Run Code Online (Sandbox Code Playgroud)

然后您可以以编程方式生成如下查询:

{
    "query" : {
        "bool" : {
            "should" : [
                {
                    "bool" : {
                        "should" : [
                            { "term" : { "tags" : "search" } },
                            { "term" : { "tags" : "open_source" } },
                            { "term" : { "tags" : "freeware" } },
                            { "term" : { "tag_count" : 1 } },
                        ],
                        "minimum_should_match" : 2
                    }
                },
                {
                    "bool" : {
                        "should" : [
                            { "term" : { "tags" : "search" } },
                            { "term" : { "tags" : "open_source" } },
                            { "term" : { "tags" : "freeware" } },
                            { "term" : { "tag_count" : 2 } },
                        ],
                        "minimum_should_match" : 3
                    }
                },
                {
                    "bool" : {
                        "should" : [
                            { "term" : { "tags" : "search" } },
                            { "term" : { "tags" : "open_source" } },
                            { "term" : { "tags" : "freeware" } },
                            { "term" : { "tag_count" : 3 } },
                        ],
                        "minimum_should_match" : 4
                    }
                }
            ],
            "minimum_should_match" : 1
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

嵌套布尔查询的数量将与查询标签的数量查询相匹配(由于多种原因,这不是很好 - 但对于较小的查询/较小的索引,也许可以摆脱这个问题?)。基本上,每个子句将处理 tag_count 的每种可能情况,并且 minus_should_match 将是 tag_count + 1 (因此匹配 tag_count 和适当的标签数量 - tag_count 数量)。