Xavier Bouclet
Xavier Bouclet

Reputation: 1012

Elasticsearch java.lang.NumberFormatExcepion when requesting on string and number fields

I am trying to implement a search request in an app with ElasticSearch.

The user type some words and those words are sent to Elastic in a json request.

To be simple the my request example is like this :

body: {
  'query': {
        'multi_match': {
            'query': 'nameValue idValue',
            'type': 'most_fields',
            'fields': ['name','id'],
        }
    },}

I get the following error :

Caused by: java.lang.NumberFormatException: For input string: "nameValue"

I understand why I get this error. It's because I am trying to search a string in a number field : id. But I would like to be able to request on a string and number at the same time.

I would also like to avoid to split the words and try to determine which word is a number and which one is a string prior to send to Elastic Search.

I tried to change my request without success.

I could change the mapping of id but it'S a number so I try to avoid this solution.

Upvotes: 0

Views: 534

Answers (1)

Val
Val

Reputation: 217554

You can try to add lenient: true to your query to ignore such data-type errors.

body: {
  'query': {
        'multi_match': {
            'query': 'nameValue idValue',
            'type': 'most_fields',
            'fields': ['name','id'],
            'lenient': true                  <--- add this
        }
    },}

Upvotes: 2

Related Questions