2015-10-03 51 views
0

我的文件看起来像:ElasticSearch自动完成从一个字符串关键字

"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 
      } 
     } 
     ] 
    } 

如何elasticsearch创建自动完成例如像: 我把在输入单词“SH”,在那之后我要看到导致

SH OES

SH ampoo

SH IRT

.....

Example of what I need

回答

0

看看ngrams。或者实际上,edge ngrams可能是你所需要的。

Qbox有一个有关设置自动完成与n元语法,所以进行了较深入的讨论,我想请您看看这些夫妇的博客文章:

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

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

但只是很很快,这应该让你开始。

首先,我建立了索引:

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" 
      } 
     } 
     } 
    } 
} 

然后我索引的文档:

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} 

现在我可以用一个简单的match query自动完成结果:

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

这退货:

{ 
    "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 
      } 
     } 
     ] 
    } 
} 

这是我用来测试它的代码:

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

相关问题