學習筆記 | ElasticSearch 中文分詞

此文是之前ES技術測試的筆記,主要是簡單的測試結果記錄。

中文分詞包

此處分詞用的是ik分詞,分詞效果還是不錯的,而且只要將自己的特殊短語加到配置中即可準確分詞。

下載ik包,解壓到plugins目錄下,5.5.1會自動加載,不需要在配置文件里配置了

GitHub中有詳細的說明以及對應ES版本的分詞包,GitHub 傳送門

示例

  1. 創(chuàng)建索引

     curl -XPUT http://localhost:9200/index
    
  2. 創(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"
           }
       }
     }'
    
  3. 添加數(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":"三生三世鎖場"}
     '
    
  4. 查詢 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>良心之作啊"
                         ]
                     }
                 }
             ]
         }
     }
    
  5. 查詢 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真是個好電影啊"
             }
           }
         ]
       }
     }
    
  6. 查看分詞器效果

    格式:http://localhost:9200/your_index/_analyze?text=中華人民共和國MN&tokenizer=my_ik

    示例:http://localhost:9200/index/_analyze?text=中華人民共和國MN&tokenizer=chinese

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容