Elasticsearch returns country from query string

I have a country type in my index that includes a list of country names. I would like to find any country names that the user could enter in their request. For example, if a user searches for:

car dealerships in japan

      

Then I would like to return the country to Japan. This works for single word countries if I do something like:

GET /my_index/country/_search
{
  "query": {
        "match" : {
            "name": {
                  "query": "car dealerships in japan"
            }
        }

    }
}

      

This brings the country back to Japan, which is what I would like to do.

But if there are multiple words in the name of the country, I'm not sure how this can happen so that it will recognize it. Otherwise, if the request looks something like this:

car dealerships in the united kingdom

      

It will return multiple countries like United States, United Kingdom, United Arab Emirates ... But I would like this to return the only kingdom for this request.

I'm not sure how best to approach this.

+3


source to share


1 answer


I would suggest trying Elasticsearch's synonyms feature. For a simple reason for this, consider that your user will not be using "United States" all the time or "United Kingdom" all the time in their request. What if the user is using "USA" or "us a" or "states" or "england". For these situations, you can use this function.

Here's the starting point:

{
  "settings": {
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym",
          "synonyms": [
            "u s a,united states,united states of america => usa",
            "g b,gb,great britain,united kingdom, uk, u k => britain,england,scotland,wales",
            "united arab emirates, emirates, arab emirates => emirates"
          ]
        }
      },
      "analyzer": {
        "my_synonyms": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonym_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "country": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "my_synonyms"
        }
      }
    }
  }
}

      

And, given that you have listed these countries in your countries:

{ "index": {}}
{ "name": "japan"}
{ "index": {}}
{ "name": "united kingdom"}
{ "index": {}}
{ "name": "united states"}
{ "index": {}}
{ "name": "united arab emirates"}

      

Search



{
  "query": {
    "match": {
      "name": {
        "query": "car dealerships in the uk, japan and emirates"
      }
    }
  }
}

      

Would give you all three countries:

  "hits": [
     {
        "_index": "my_index",
        "_type": "country",
        "_id": "CMZe2ygBS4OLL3_lT_B2_Q",
        "_score": 0.03739948,
        "_source": {
           "name": "japan"
        }
     },
     {
        "_index": "my_index",
        "_type": "country",
        "_id": "T-e7rg_rTx-3rtTJYxJrBg",
        "_score": 0.03739948,
        "_source": {
           "name": "united arab emirates"
        }
     },
     {
        "_index": "my_index",
        "_type": "country",
        "_id": "EqlMu2RiRiSdwyqJa2nyzA",
        "_score": 0.017334092,
        "_source": {
           "name": "united kingdom"
        }
     }
  ]

      

And if you only request one country, only one will be returned:

{
  "query": {
    "match": {
      "name": {
        "query": "car dealerships in the united states"
      }
    }
  }
}

      

Read more about this feature here .

+1


source







All Articles