Jonathan Petitcolas
Jonathan Petitcolas

Reputation: 4574

multi_match query with number throws number_format_exception

I am currently migrating my search engine to Elastic Search. Let's consider I have the following records:

make            | model     | year  | owner_email
Crossroad       | Salem     | 2014  | [email protected]
Dutchmen        | Launch    | 1949  | [email protected]
Four winds      | SLX Baja  | 2008  | [email protected]

I'd like to retrieve vehicle for a given make and year. For instance, looking for Four winds 2008 should return the last result.

I am currently using the following request:

{
    "query": {
        "multi_match": {
            "query": "Four winds 2008",
            "fields": ["make", "model", "year"],
            "type": "cross_fields",
            "minimum_should_match": "100%"
        }
    }
}

Yet, this query raises an error:

{
    "error": {
        "caused_by": {
            "reason": "For input string: \"Eclipse White Hawk Ultra Lite 1989\"",
            "type": "number_format_exception"
        }
    }
}

And indeed, the year column is a long one, unlike the other ones, which are text columns.

I tried to add the lenient option to my query, to ignore type errors. But adding it makes the query return nothing.

Have you any other idea to make this query work? Meanwhile, as a workaround, I've updated the index mapping to set year as a text field. Fortunately, we don't need any range search based on year, for now at least.

Thanks for your help! :)

Upvotes: 1

Views: 505

Answers (1)

Nishant
Nishant

Reputation: 7854

In order to use year in a multi_match query as well as range query, you can define the year field as multi field in mapping as below:

{
  "year": {
    "type": "text",
    "fields": {
      "numeric": {
        "type": "integer"
      }
    }
  }
}

With the above change you can continue to use year in multi_match query and whenever you want to apply a range query on the year field then refer the year.numeric field instead of year.

Upvotes: 1

Related Questions