Elasticsearch search query: why params._source.nested_field.size() is not working in script?

后端 未结 1 1576
天命终不由人
天命终不由人 2021-01-20 21:11

There are lot of questions and answers about this but still didn\'t get a satisfied answers. Elasticsearch version: 6.5

Index mapping

\"_doc\": {
          


        
相关标签:
1条回答
  • 2021-01-20 21:35

    Despite the fact that this would've been slow and against the guidance of not accessing _source in search queries, the query would have worked before v6.4.

    After v6.4, thanks to an unintentional side effect of refactoring, accessing _source in the script query context is not possible.

    With that being said, we can "hijack" a function_score query whose Painless context still has access to the _source:

    {
      "query": {
        "function_score": {
          "query": {
            "match_all": {}
          },
          "functions": [
            {
              "script_score": {
                "script": {
                  "source": "params._source.containsKey('nested_field') && params._source['nested_field'] != null && params._source.nested_field.size() > 1 ? 1 : 0"
                }
              }
            }
          ],
          "min_score": 1
        }
      }
    }
    

    You could use a pipeline to calculate the field size and then save it on the doc's top level.

    Alternatively, you could use copy_to as outlined in my related answer.

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