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}}}}'json

 

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

注意:

  • term 查詢中min_word_length 參數,這個參數默認值4,若是不設置這個參數返回結果會爲空,詳細參考官方文檔curl

  • 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"}}}}'url

注意

  • 建立 mapping 的時候使用的 simple analyzer,若是是其它的可能會有一些默認處理(好比 engligh 過濾停用詞)spa

相關文章
相關標籤/搜索