使用 ES (elasticsearch) 搜索中文

1、创建索引
curl -XPUT http://172.16.125.139:9200/ques
2、创建索引类型
curl -XPOST http://172.16.125.139:9200/ques/common/_mapping -H 'Content-Type:application/json' -d '{"properties":{"question":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_max_word","similarity":"BM25"},"answer":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_max_word","similarity":"BM25"}}}'
3、push 数据
curl -XPOST http://172.16.125.139:9200/ques/common/1 -H 'Content-Type:application/json' -d '{"question":"xx", "answer":"xx"}'
curl -XPOST http://172.16.125.139:9200/ques/common/2 -H 'Content-Type:application/json' -d '{"question":"xx", "answer":"xx"}'
4、搜索
curl -XPOST http://172.16.125.139:9200/ques/common/_search -H 'Content-Type:application/json' -d '{"query":{"match": {"question":"xxx"}}}’

备注:

1、该操作是在 es 支持 ik 分词的基础上进行的,可以参考官网https://github.com/medcl/elasticsearch-analysis-ik

2、push、search数据的时候数据都是使用 xx 代替的,实际使用时替换成真是数据即可;

2、这里的相似度使用的是 BM25,也可以替换为 TFIDF

猜你喜欢

转载自www.cnblogs.com/shanguanghui/p/9244789.html