簡體   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