0

my query is very simple, for the sake of even making it simpler, lets say I only search on two fields, name(text) & age(long):

GET person_db/person/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase_prefix": {
            "name": "hank"
          }
        },
        {
          "match_phrase_prefix": {
            "age": "hank"
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1.0
    }
  }
}

if I search for "23", no problem, elastic knows how to change it to numeric and it won't fail, but if the search input is "john" I get error 400 "reason": "failed to create query: {\n \"bool\....".

what should I do in this case?

I thought of changing the values that are numeric to strings before insert to es, but trying to avoid it, I think es should have a way to support it.

appreciate it

This query works: (thanks to @jmlw)

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "alt",
            "type": "phrase_prefix",
            "fields": [
              "name",
              "taxid",
              "providers.providerAddress.street"
            ],
            "lenient": true
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1.0
    }
  }
}
jack miao
  • 1,398
  • 1
  • 16
  • 32
  • 1
    Can you add the full error? I suspect that Es doesn't like searching for a textual value in a numeric field... – Val Jul 27 '18 at 14:04
  • @Val yes I'm sure of that, but how do I solve that? when I build the query i put the client input inside all the fields...I don't know what the input will be – jack miao Jul 27 '18 at 14:34
  • Can you try to do a [`multi_match` of type `phrase_prefix`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-phrase) with both fields instead of separate `match_phrase_prefix` clauses? – Val Jul 27 '18 at 14:36

1 Answers1

1

Without details of your documents, or your mappings, my first guess is that the age field is interpreted as a numeric field by Elasticsearch. Passing in anything other than a 'number' type, or something that can be converted into a number will cause the query to fail, with some exception reporting a failure to convert your string into a number.

With that said, you may try add ing lenient: true to your match_phrase_prefix search term, which will allow Elasticsearch to ignore failures to convert to a numeric type, and remove that term from the search.

Another approach is to only allow users to query on multiple fields of the same type, or specify what data they'd like to query in which field. I.E. I'm a user, and I want to search for people where age is 23, and have the name John, instead of typing in 23 John, or similar.

Otherwise, you may need to pre-process the query string, and split search terms and pass them into search clauses individually with lenient: true to attempt searching multiple terms in multiple fields with different data types.

You could also try using a different search type, like a multi_match, query_string, or simple_query_string as these will likely have more flexibility for what you are wanting to do.

jmlw
  • 91
  • 1
  • 3
  • thanks buddy, added the working solution thanks to your help. let me know if you would add something to it to make it strong :) @jmlw – jack miao Jul 27 '18 at 15:01
  • some small issue, maybe you'd know: I used phrase_prefix type query, and incase of the text it works great, and incase on a number it will only pull exact number...is it possible to edit it someohow? – jack miao Jul 27 '18 at 15:04
  • Unfortunately, I do not know. You might try fuzziness parameter for multi-match like in this SO post [ElasticSearch multi_match query over multiple fields with Fuzziness ] (https://stackoverflow.com/questions/29632339/elasticsearch-multi-match-query-over-multiple-fields-with-fuzziness), but I'm not sure how it behaves with numeric data. – jmlw Jul 27 '18 at 18:36