elasticsearchkibana-6

Has there been any change in the format of using function_score in ES 6.8?


I have the query in below format and it runs in ES 2.4

{"query":{"function_score":{"filter":{"bool":{"must":[{"exists":{"field":"x"}},{"query_string":{"query":"en","fields":["locale"]}},{"query_string":{"query":"US","fields":["channel"]}},{"query_string":{"query":"UG","fields":["usergroups"]}}]}},"query":{"bool":{"should":{"multi_match":{"query":"refund","fields":["doc","key","title","title.standard_analyzed^3","x"],"type":"phrase","slop":20}},"must":{"multi_match":{"fuzziness":"0","query":"refund","prefix_length":"6","fields":["doc","key","title","title.standard_analyzed^3","x"],"max_expansions":"30"}}}},"functions":[{"field_value_factor":{"field":"usage","factor":1,"modifier":"log2p","missing":1}}]}},"from":0,"size":21}

But when I try the same query in 6.8 it returns errors {"error":{"root_cause":[{"type":"parsing_exception","reason":"no [query] registered for [function_score]",

If I put filters inside query, I get the response but the order of the docs don't match due to the difference in score


Solution

  • There should only be the "query" key below the function score. You have to add the filter in the bool query. I don't know about your mapping but I would use the "Term" query instead of the query string.

    {
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "filter": { 
                "bool": {
                  "must": [
                    {
                      "exists": {
                        "field": "x"
                      }
                    },
                    {
                      "query_string": {
                        "query": "en",
                        "fields": [
                          "locale"
                        ]
                      }
                    },
                    {
                      "query_string": {
                        "query": "US",
                        "fields": [
                          "channel"
                        ]
                      }
                    },
                    {
                      "query_string": {
                        "query": "UG",
                        "fields": [
                          "usergroups"
                        ]
                      }
                    }
                  ]
                }
              },
              "should": {
                "multi_match": {
                  "query": "refund",
                  "fields": [
                    "doc",
                    "key",
                    "title",
                    "title.standard_analyzed^3",
                    "x"
                  ],
                  "type": "phrase",
                  "slop": 20
                }
              },
              "must": {
                "multi_match": {
                  "fuzziness": "0",
                  "query": "refund",
                  "prefix_length": "6",
                  "fields": [
                    "doc",
                    "key",
                    "title",
                    "title.standard_analyzed^3",
                    "x"
                  ],
                  "max_expansions": "30"
                }
              }
            }
          },
          "functions": [
            {
              "field_value_factor": {
                "field": "usage",
                "factor": 1,
                "modifier": "log2p",
                "missing": 1
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 21
    }
    

    About FunctionScore (doc 6.8)