1
votes

I have a query as follows to use function_score but it gives an error that the query is malformed and [END_OBJECT] is expected but found [FIELD_NAME]. I have checked the documentation and couldn't find what is incorrect or inconsistent with this. I haven't added any script in script_score here but even when I tried with adding script it gave the same problem.

{
  "from": 0,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "address.area.area.raw": "Durbarmarg"
                          }
                        },
                        {
                          "match": {
                            "address.area.city.raw": "Kathmandu"
                          }
                        },
                        {
                          "match": {
                            "address.area.district.raw": "Kathmandu"
                          }
                        },
                        {
                          "match": {
                            "address.area.state.raw": "State-3"
                          }
                        },
                        {
                          "match": {
                            "address.area.country.raw": "Nepal"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "nested": {
                      "inner_hits": {},
                      "path": "branchAddress",
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "match": {
                                "branchAddress.area.area.raw": "Durbarmarg"
                              }
                            },
                            {
                              "match": {
                                "branchAddress.area.city.raw": "Kathmandu"
                              }
                            },
                            {
                              "match": {
                                "branchAddress.area.district.raw": "Kathmandu"
                              }
                            },
                            {
                              "match": {
                                "branchAddress.area.state.raw": "State-3"
                              }
                            },
                            {
                              "match": {
                                "branchAddress.area.country.raw": "Nepal"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          ],
          "must": [
            {
              "term": {
                "sub_categories.raw": "Restaurant"
              }
            }
          ],
          "must_not": [],
          "should": []
        }
      }
    },
    "script_score": {}
  },
  "size": 10
}

The error is as bellow:

raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'parsing_exception', '[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')
1

1 Answers

1
votes

The script_score section is not at the right place, it needs to be a sibling of the inner query:

{
  "from": 0,
  "query": {
    "function_score": {
      "query": {
        ...
      },
      "script_score": {}       <--- script_score goes here
    }
  },
  "size": 10
}