elasticsearch 中文 term & completion suggester
Term suggester
创建索引
curl -XPUT 'http://172.16.125.136:9200/term?pretty'
创建 mapping
curl -XPOST http://172.16.125.136:9200/term/suggester/_mapping -H 'Content-Type:application/json' -d '{"properties":{"body":{"type":"text","analyzer":"ik_smart","search_analyzer":"ik_smart"}}}'
push 数据
curl -XPOST http://172.16.125.136:9200/term/suggester/1 -H 'Content-Type:application/json' -d '{"body":"长安街大饭店"}'
curl -XPOST http://172.16.125.136:9200/term/suggester/2 -H 'Content-Type:application/json' -d '{"body":"长安街天上人间大剧院"}'
term 查询
curl -XPOST http://172.16.125.136:9200/term/suggester/_search -H 'Content-Type:application/json' -d '{"suggest":{"my_suggester":{"text":"长安城天上大剧院","term":{"field":"body","suggest_mode":"always","min_word_length":2}}}}'
{"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]},"suggest":{"didyoumean":[{"text":"长安城","offset":0,"length":3,"options":[{"text":"长安街","score":0.6666666,"freq":2}]},{"text":"天上","offset":3,"length":2,"options":[]},{"text":"大","offset":5,"length":1,"options":[]},{"text":"剧院","offset":6,"length":2,"options":[]}]}}
注意:
-
term 查询中min_word_length 参数,这个参数默认值4,如果不设置这个参数返回结果会为空,详细参考官方文档
- mapping 不能使用 ik_max_word
Completion Suggester
创建索引
curl -XPUT 'http://172.16.125.139:9200/completion?pretty'
创建 mapping
curl -XPOST http://172.16.125.139:9200/completion/suggester/_mapping -H 'Content-Type:application/json' -d '{"properties":{"body":{"type":"completion"}}}'
push 数据
curl -XPOST http://172.16.125.139:9200/completion/suggester/1 -H 'Content-Type:application/json' -d '{"body":"长安街大饭店"}'
curl -XPOST http://172.16.125.139:9200/completion/suggester/2 -H 'Content-Type:application/json' -d '{"body":"长安街天上人间大剧院"}'
completion 查询
curl -XPOST http://172.16.125.139:9200/completion/suggester/_search -H 'Content-Type:application/json' -d '{"suggest":{"my_suggester":{"prefix":"长安街","completion":{"field":"body"}}}}'
注意
-
创建 mapping 的时候使用的 simple analyzer,如果是其它的可能会有一些默认处理(比如 engligh 过滤停用词)