dontberude
dontberude

Reputation: 551

Get suggestion on field Elasticsearch

I am trying to make a suggestion feature with Elasticsearch. following this article https://qbox.io/blog/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams

What I have now works but not for two words in the same sentence.

The data I have now in ES is.

{
   "_index": "books",
   "_type": "book",
   "_id": "AVJp8p4ZTfM-Ee45GnF5",
   "_score": 1,
   "_source": {
      "title": "Making a dish",
      "author": "Jim haunter"
   }
},
{
   "_index": "books",
   "_type": "book",
   "_id": "AVJp8jaZTfM-Ee45GnF4",
   "_score": 1,
   "_source": {
       "title": "The big fish",
       "author": "Jane Stewart"
   }
},
{
   "_index": "books",
   "_type": "book",
   "_id": "AVJp8clRTfM-Ee45GnF3",
   "_score": 1,
   "_source": {
        "title": "The Hunter",
        "author": "Jame Franco"
    }
}

Here is the mapping and settings.

{"settings": {
  "analysis": {
     "filter": {
        "nGram_filter": {
           "type": "nGram",
           "min_gram": 2,
           "max_gram": 20,
           "token_chars": [
              "letter",
              "digit"
           ]
        }
     },
     "analyzer": {
        "nGram_analyzer": {
           "type": "custom",
           "tokenizer": "whitespace",
           "filter": [
              "lowercase",
              "nGram_filter"
           ]
        },
        "whitespace_analyzer": {
           "type": "custom",
           "tokenizer": "whitespace",
           "filter": [
              "lowercase"
           ]
        } 
     }
  }
},
"mappings": {
  "books": {
     "_all": {
        "index_analyzer": "nGram_analyzer",
        "search_analyzer": "whitespace_analyzer"
     },
     "properties": {
        "title": {
           "type": "string",
           "index": "no"
        },
        "author": {
           "type": "string",
           "index": "no"
        }
      }
    }
  }
}

Here is the search

{
   "size": 10,
   "query": {
   "match": {
   "_all": {
      "query": "Hunter",
      "operator": "and",
      "fuzziness": 1
   }
  }
 }
}

when I search for "The" I get "The big fish" and "The hunter". However when I enter "The Hunt" I get nothing. To get the book again I need to enter "The Hunte". Any suggestions? Any help appreciated.

Upvotes: 2

Views: 73

Answers (1)

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

Removing "index": "no" from the fields worked for me. Also, since I'm using ES 2.x, I had to replace "index_analyzer" with "analyzer". So here is the mapping:

PUT /test_index
{
   "settings": {
      "analysis": {
         "filter": {
            "nGram_filter": {
               "type": "nGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit"
               ]
            }
         },
         "analyzer": {
            "nGram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "nGram_filter"
               ]
            },
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase"
               ]
            }
         }
      }
   },
   "mappings": {
      "books": {
         "_all": {
            "analyzer": "nGram_analyzer",
            "search_analyzer": "whitespace_analyzer"
         },
         "properties": {
            "title": {
               "type": "string"
            },
            "author": {
               "type": "string"
            }
         }
      }
   }
}

Here's some code I used to test it:

http://sense.qbox.io/gist/0140ee0f5043f66e76cc3109a18d573c1d09280b

Upvotes: 1

Related Questions