繁体   English   中英

ElasticSearch:筛选文档,其中数组字段中的任何值都不在列表中

[英]ElasticSearch: Filter for documents where any value in an array field is not in a list

我遇到的情况是我的字段包含整数数组。 我需要能够实现一个查询,该查询可以过滤出包含此数组中的一组特定值的文档。

例如,假设我创建了以下索引:

PUT /test/stuff/1
{
    "foo": [1,2,3]
}

PUT /test/stuff/2
{
    "foo": [2]
}

PUT /test/stuff/3
{
    "foo": [1,3]
}

现在,我想获取所有文档,其中“ foo”包含不在[2,4]中的任何值。 我想返回ID为1和3的文档。 并非该文档1确实包含值2 ,还包含其他值。 像这样的简单must_not会过滤掉文档1:

POST /test/stuff/_search
{
    "query": { 
        "bool": {
            "must" : {
                "match_all": {}
            },
            "filter" : {
                "bool": {
                    "must_not": [
                        {
                            "terms" : {"foo" : [2,4]}
                        }
                    ]
                }
            }
        }
    }
}

上面的查询将仅匹配文档3。是否可以重写此文档以使其也包含文档1?

这是可行的一个脚本查询,以示无痛如下:

{
    "query": { 
        "bool": {
            "must" : {
                "match_all": {}
            },
            "filter" : {
                "bool": {
                    "must": {
                        "script" : {
                            "script" : {
                            "inline" : "for(int i: doc['foo']) { boolean matches = true; for(int j: params.param1){if(i==j) matches = false;} if(matches) return true;} ",
                            "lang"   : "painless",
                            "params" : { "param1": [2,4]}
                            }
                        }
                    }

                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM