此文是之前ES技術測試的筆記,主要是簡單的測試結果記錄。
中文分詞包
此處分詞用的是ik分詞,分詞效果還是不錯的,而且只要將自己的特殊短語加到配置中即可準確分詞。
下載ik包,解壓到plugins目錄下,5.5.1會自動加載,不需要在配置文件里配置了
GitHub中有詳細的說明以及對應ES版本的分詞包,GitHub 傳送門
示例
-
創(chuàng)建索引
curl -XPUT http://localhost:9200/index -
創(chuàng)建mapping
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d' { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } }' -
添加數(shù)據(jù)
curl -XPOST http://localhost:9200/index1/fulltext/1 -d' {"content":"戰(zhàn)狼2真是個好電影啊"} ' curl -XPOST http://localhost:9200/index1/fulltext/2 -d' {"content":"戰(zhàn)狼良心之作啊"} ' curl -XPOST http://localhost:9200/index1/fulltext/3 -d' {"content":"三生三世鎖場"} ' -
查詢 match
curl -XPOST http://localhost:9200/index/fulltext/_search -d' { "query" : { "match" : { "content" : "戰(zhàn)狼2" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } ' Result: { "took": 36, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.854655, "hits": [ { "_index": "index", "_type": "fulltext", "_id": "1", "_score": 0.854655, "_source": { "content": "戰(zhàn)狼2真是個好電影啊" }, "highlight": { "content": [ "<tag1>戰(zhàn)</tag1><tag1>狼</tag1><tag1>2</tag1>真是個好電影啊" ] } }, { "_index": "index", "_type": "fulltext", "_id": "2", "_score": 0.5716521, "_source": { "content": "戰(zhàn)狼良心之作啊" }, "highlight": { "content": [ "<tag1>戰(zhàn)</tag1><tag1>狼</tag1>良心之作啊" ] } } ] } } -
查詢 match_phrase
curl -XGET 'localhost:9200/index/fulltext/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_phrase" : { "content" : "戰(zhàn)狼2" } } } ' Result { "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.85465515, "hits" : [ { "_index" : "index", "_type" : "fulltext", "_id" : "1", "_score" : 0.85465515, "_source" : { "content" : "戰(zhàn)狼2真是個好電影啊" } } ] } } -
查看分詞器效果
格式:http://localhost:9200/your_index/_analyze?text=中華人民共和國MN&tokenizer=my_ik
示例:http://localhost:9200/index/_analyze?text=中華人民共和國MN&tokenizer=chinese