2

On Elasticsearch how can you query a string attribute ignoring white spaces ?

The scenario is having phone numbers indexed as strings, with different formatings. Querying a phone without spaces, can I get all strings that have that phone (with or without spaces). For example:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "filter": {
    "nested": {
      "path": "Contacts",
      "query": {
        "query_string": {
          "default_field": "Contacts.ContactValue",
          "query": "261981255"
        }
      }
    }
  }
}

This query would return contacts like: "261981255", "261 981 255", "26 19 81 25 5", etc.

2
  • 1
    Not specific to the question (dadoonet covered that), but don't use the filter that way. A filter that lives outside of the query is a post filter. You want to use it like the link shows (inside of the query) to avoid extra work. Also, match_all can just be "query" : { "match_all" : { } }. Commented Jun 6, 2015 at 17:34
  • @jovin4: did you manage to get a solution? Commented Sep 26, 2017 at 8:52

2 Answers 2

2

You need to use a specific analyzer which breaks your query into grams.

You can do that by setting un the mapping a search_analyzer for your field.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I get that by applying a default terms analyzer? The analyzer will split the attribute values in terms (grams?), but then will it match to return the records that have a match, while joining all the terms ? Is this an OOTB behaviour? Thanks!
0

Try to reindex your data using a char filter in your analyzer to recognize these patterns and adjust to a desired way.

By this way, all your future queries will be easy to perform if you have a single pattern in entire data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.