elasticsearch - 过滤具有多个值的字段

标签 elasticsearch

我将如何解决以下问题:
我想过滤一个包含多个值的字段(例如[“value1”、“value2”、“value3”])。

过滤器还将包含多个值(例如 ["value1", "value2"]。
我只想取回与过滤器具有相同字段值的项目,例如。字段是 ["value1", "value2"] 过滤器也是 ["value1", "value2"]

任何帮助将不胜感激

最佳答案

我认为最近添加的 (v6.1) terms_set query (其中 Val 引用了他在评论中链接的问题)就是您想要的。
terms_set ,与常规 terms 不同, 有一个参数来指定搜索词和字段中包含的词之间必须存在的最小匹配数。

鉴于:

PUT my_index/_doc/1
{ 
    "values": ["living", "in a van", "down by the river"],
}
PUT my_index/_doc/2
{
    "values": ["living", "in a house", "down by the river"],
}

一个 terms查询 ["living", "in a van", "down by the river"]将返回给您两个文档:不好。一个 terms_set配置为需要所有三个匹配项(脚本 params.num_terms 评估为 3 )可以给你匹配的一个:
GET my_index/_search
{
    "query": { 
        "terms_set": {
            "values": {
                "terms": ["living", "in a van", "down by the river"],
                "minimum_should_match_script": {
                  "source": "params.num_terms"
                }
            }            
        }
    }
}

注意:虽然我使用了 minimum_should_match_script在上面的例子中,它不是一个非常有效的模式。替代方案 minimum_should_match_field是更好的方法,但在示例中使用它意味着需要更多的 PUT 来将必要的字段添加到文档中,所以我简洁地去了。

关于elasticsearch - 过滤具有多个值的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394802/

相关文章:

elasticsearch - 如何将弹性时间戳json转换为C#时间戳

java - log4j tcp appender 和 logstash 源主机

c# - 如何在Elasticsearch中匹配搜索查询中的单个单词

elasticsearch - ElasticSearch 2.4.1上的MapperParsingException不一致

c# - 使用 NEST 搜索 elasticsearch 索引没有结果

elasticsearch - 在 Elasticsearch 中使用 date_histogram 聚合计算嵌套字段的总和

java - Elasticsearch QueryBuilder 匹配多个术语

elasticsearch - 如何在Presto Elasticsearch中按子句推栈

elasticsearch - 在Elasticsearch中需要对具有数字和字母数字值的字段进行排序

Elasticsearch 将精确的术语与跨不同字段的空格匹配