简体   繁体   中英

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 to create autocomplete in elasticsearch like for example: I put in input word "sh" , after that I should see result

sh oes

sh ampoo

sh irt

.....

Example of what I need

Take a look at ngrams . Or actually, edge ngrams are probably all you need.

Qbox has a couple of blog posts about setting up autocomplete with ngrams, so for a more in-depth discussion I would refer you to 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 just very quickly, this should get you started.

First I set up the 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 I indexed your 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 I can get autocomplete results with a 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 the code I used to test it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM