elasticsearch filtering by the size of a field that is an array

后端 未结 8 954
旧巷少年郎
旧巷少年郎 2020-12-08 03:47

How can I filter documents that have a field which is an array and has more than N elements?

How can I filter documents that have a field which is an empty array?

8条回答
  •  盖世英雄少女心
    2020-12-08 04:16

    Still posting to here for who stuck same situation with me. Let's say your data look like this:

    {
        "_source": {
            "fieldName" : [
                {
                    "f1": "value 11",
                    "f2": "value 21"
                },
                {
                    "f1": "value 12",
                    "f2": "value 22"
                }
            ]
        }
    }
    

    Then to filter fieldName with length > 1 for example:

    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['fieldName.f1'].values.length > 1",
                        "lang": "painless"
                     }
                }
            }
        }
    }
    

    The script syntax is as ES 5.4 documentation https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html.

提交回复
热议问题