Search code examples
c#elasticsearchindexingautocompleteprefix

ElasticSearch query trouble


After creating my index in elastic search with the next default settings:

{    "autosuggest_destination": {
      "aliases": {},
      "mappings": {
         "destination": {
            "properties": {
               "city": {
                  "type": "string"
               },
               "country": {
                  "type": "string"
               },
               "id": {
                  "type": "string"
               },
               "state": {
                  "type": "string"
               }
            }
         }
      },
      "settings": {
         "index": {
            "creation_date": "1459329252404",
            "number_of_shards": "1",
            "number_of_replicas": "0",
            "version": {
               "created": "1070299"
            },
            "uuid": "_1D7ZW0dQwy9kiKn0kKrLw"
         }
      },
      "warmers": {}    } }

after insert data to index and verified.

I get this problem while trying to search for an auto complete word:

when i try to search for 'new' it founds matches (New York). but if i tries to add a space and first letter after 'new y', it founds nothing. and after it tries to add a space and two letters after 'new yo' it works.

The main problem is understand why the first letter after a word with a space is not a match?

example :

    GET autosuggest_destination/destination/_search {    "query": {
        "match": {
          "city": {
            "query": "new", 
            "type": "phrase_prefix"
          }

           } }

    Result : New York


    GET autosuggest_destination/destination/_search {    "query": {
        "match": {
          "city": {
            "query": "new y", 
            "type": "phrase_prefix"
          }

           } }

    No Result

    GET autosuggest_destination/destination/_search {    "query": {
        "match": {
          "city": {
            "query": "new yo", 
            "type": "phrase_prefix"
          }

           } }

Result : New York

Does anyone have any idea what should be the problem?


Solution


  • Elasticsearch at indexing a document is using an Analyzer (a default one or a custom one) when you search for some values it splits the document into terms and then it will make the matching with what you wanted to search.
    In your example: New York is analyzed with the default analyzer takes place and it will split your document into 2 terms New, York.
    When you search for New it will find you the first term New, so it will give you the response New York. If you search by New Y it will split the search into 2 terms: New, Y or New, Yo and it will try to find documents that match these 2 terms. The problem that you encounter it may be from max_expansions param.
    Take a look here http://elasticsearch-users.115913.n3.nabble.com/How-exactly-works-quot-max-expansions-quot-in-match-phrase-prefix-query-td4030146.html Also regarding the analyzer.

    https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-analyzers.html