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

后端 未结 8 922
旧巷少年郎
旧巷少年郎 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:36

    Based on this: https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/collect/RegularImmutableList.java?r=707f3a276d4ea8e9d53621d137febb00cd2128da

    And on lisak's answer here.

    There is size() function which returns the length of list:

    "filter" : {
        "script" : {
            "script" : "doc['fieldname'].values.size() > 10"
        }
    }
    
    0 讨论(0)
  • 2020-12-08 04:37

    javanna's answer is correct on Elasticsearch 1.3.x and earlier, since 1.4 the default scripting module has changed to groovy (was mvel).

    To answer OP's question.

    On Elasticsearch 1.3.x and earlier, use this code:

    "filter" : {
        "script" : {
            "script" : "doc['fieldname'].values.length > 10"
        }
    }
    

    On Elasticsearch 1.4.x and later, use this code:

    "filter" : {
        "script" : {
            "script" : "doc['fieldname'].values.size() > 10"
        }
    }
    

    Additionally, on Elasticsearch 1.4.3 and later, you will need to enable the dynamic scripting as it has been disabled by default, because of security issue. See: https://www.elastic.co/guide/en/elasticsearch/reference/1.4/modules-scripting.html

    0 讨论(0)
提交回复
热议问题