ElasticSearch autocomplete for keywords from a string -


my document looks like:

   "hits": {       "total": 4,       "max_score": 1,       "hits": [          {             "_index": "test_db2",             "_type": "test",             "_id": "1",             "_score": 1,             "_source": {                "name": "very cool shoes",                "price": 26             }          },          {             "_index": "test_db2",             "_type": "test",             "_id": "2",             "_score": 1,             "_source": {                "name": "great shampoo",                "price": 15             }          },          {             "_index": "test_db2",             "_type": "test",             "_id": "3",             "_score": 1,             "_source": {                "name": "shirt",                "price": 25             }          }       ]     } 

how create autocomplete in elasticsearch example: put in input word "sh" , after should see result

shoes

shampoo

shirt

.....

example of need

take @ ngrams. or actually, edge ngrams need.

qbox has couple of blog posts setting autocomplete ngrams, more in-depth discussion refer these:

https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch

https://qbox.io/blog/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams

but quickly, should started.

first set index:

put /test_index {    "settings": {       "analysis": {          "analyzer": {             "autocomplete": {                "type": "custom",                "tokenizer": "standard",                "filter": [                   "standard",                   "stop",                   "kstem",                   "edgengram_filter"                ]             }          },          "filter": {             "edgengram_filter": {                "type": "edgengram",                "min_gram": 2,                "max_gram": 15             }          }       }    },    "mappings": {       "doc": {          "properties": {             "name": {                "type": "string",                "index_analyzer": "autocomplete",                "search_analyzer": "standard"             },             "price":{                 "type": "integer"             }          }       }    } } 

then indexed documents:

post /test_index/doc/_bulk {"index":{"_id":1}} {"name": "very cool shoes","price": 26} {"index":{"_id":2}} {"name": "great shampoo","price": 15} {"index":{"_id":3}} {"name": "shirt","price": 25} 

now can autocomplete results simple match query:

post /test_index/_search {    "query": {       "match": {          "name": "sh"       }    } } 

which returns:

{    "took": 3,    "timed_out": false,    "_shards": {       "total": 5,       "successful": 5,       "failed": 0    },    "hits": {       "total": 3,       "max_score": 0.30685282,       "hits": [          {             "_index": "test_index",             "_type": "doc",             "_id": "3",             "_score": 0.30685282,             "_source": {                "name": "shirt",                "price": 25             }          },          {             "_index": "test_index",             "_type": "doc",             "_id": "2",             "_score": 0.19178301,             "_source": {                "name": "great shampoo",                "price": 15             }          },          {             "_index": "test_index",             "_type": "doc",             "_id": "1",             "_score": 0.15342641,             "_source": {                "name": "very cool shoes",                "price": 26             }          }       ]    } } 

here's code used test it:

http://sense.qbox.io/gist/0886488ddfb045c69eed67b15e9734187c8b2491


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -