Home » Java » Elasticsearch query builder search exact email

Elasticsearch query builder search exact email

Posted by: admin October 22, 2018 Leave a comment

Questions:

Problem:

Using Elasticsearch version 5.6.8, I want to search for exact email address.

I have two ES servers running both on version 6.1.0 and 5.6.8. I could search for email easily on version 6.1.0 (see codes below), but it won’t work on version 5.6.8.

I needed to make it work for version 5.6.8 since my staging as well as production servers are running on 5.6.8 version.

By the way, when I say it does not work.. it means it does not return anything in the result or the result is wrong.

Trials: Works on 6.1.0 but did not work on 5.6.8

Tried changing the email type from text to keyword, but still no luck.

"email": {
    "type": "keyword",
    "ignore_above": 256,
    "normalizer": "custom_sort_normalizer"
}

Below are the details of my index, mappings, query, etc.

Create User Index

PUT http://localhost:9200/acme_users

{
  "settings": {
    "analysis": {   
      "normalizer": {
        "custom_sort_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  }
}

Set Mapping for User

PUT http://localhost:9200/acme_users/_mapping/user

{
  "properties": {
        "name": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                "normalizer": "custom_sort_normalizer"
                }
            }
        },
        "company": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                    "normalizer": "custom_sort_normalizer"
                }
            }
        },
        "country": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                    "normalizer": "custom_sort_normalizer"
                }
            }
        },
        "role": {
            "properties": {
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256,
                            "normalizer": "custom_sort_normalizer"
                        }
                    }
                }
            }
        },
        "groups": {
            "properties": {
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256,
                            "normalizer": "custom_sort_normalizer"
                        }
                    }
                }
            }
        },
        "groupsKeyword": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                    "normalizer": "custom_sort_normalizer"
                }
            }
        },      
        "email": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "custom_sort_normalizer"
        }
  }
}

Query

GET http://localhost:9200/acme_users/user/_search

{
  "from" : 0,
  "size" : 15,
  "query" : {
    "bool" : {
      "must" : [
        {
          "query_string" : {
            "query" : "activedInd: true AND deletedInd: false",
            "fields" : [ ],
            "use_dis_max" : true,
            "tie_breaker" : 0.0,
            "default_operator" : "or",
            "auto_generate_phrase_queries" : false,
            "max_determinized_states" : 10000,
            "enable_position_increments" : true,
            "fuzziness" : "AUTO",
            "fuzzy_prefix_length" : 0,
            "fuzzy_max_expansions" : 50,
            "phrase_slop" : 0,
            "escape" : false,
            "split_on_whitespace" : true,
            "boost" : 1.0
          }
        },
        {
          "query_string" : {
            "query" : "bungoton.si.bryan@gmail.com*",
            "fields" : [ ],
            "use_dis_max" : true,
            "tie_breaker" : 0.0,
            "default_operator" : "or",
            "auto_generate_phrase_queries" : false,
            "max_determinized_states" : 10000,
            "enable_position_increments" : true,
            "fuzziness" : "AUTO",
            "fuzzy_prefix_length" : 0,
            "fuzzy_max_expansions" : 50,
            "phrase_slop" : 0,
            "escape" : false,
            "split_on_whitespace" : true,
            "boost" : 1.0
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "sort" : [
    {
      "name.keyword" : {
        "order" : "asc"
      }
    }
  ]
}

If I use * in the query value, it does not return anything on 5.6.8, but on 6.1.0 it returns the exact record.

"query" : "bungoton.si.bryan@gmail.com*"

On 5.6.8, if I remove * it will return the result, but all accounts with emails having gmail.com are returned. Same effect when done on windows side.

I do not know what I am missing. It just do not return anything on 5.6.8.

By the way, the Query part is generated from my java application (ES High Level Java Client).

Answers: