ElasticSearch利用IK實現全文搜索


要做到中文全文檢索還需要按照中文分詞庫 ,這里就使用 IK來設置

安裝中文分詞庫
相關命令:

whereis elasticsearch  找到目錄
進入 到/usr/elasticsearch/bin 
執行  ./elasticsearch-plugin插件命令 安裝插件

./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.1/elasticsearch-analysis-ik-6.6.1.zip

service elasticsearch restart 重啟服務

安裝好之后可以看到 plugin 下有 analysis-ik 

接下來按照官方的步驟往下面走

1、創建一個索引

curl -XPUT http://localhost:9200/myfulltext

2、創建

curl -XPOST http://localhost:9200/liyouming/liyoumingtext/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "mytext": {  
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }

}'
這里要注意 content 不能沖突 ,分析器字段要定義
Mapper for [content] conflicts with existing mapping in other types:\n[mapper [content] has different [analyzer]]
Mapper for[content]與其他類型的現有映射沖突:[mapper[content]有不同的[分析器]

這里我們還是通過WebAPI來測試

首先創建我們的索引

OK后創建 全文檢索相關設置 設置字段、分析器配置    ik_smart  、ik_max_word

 分別添加如下數據

 

{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"深夜還在寫代碼的人只有黎又銘"}
}
{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"中午黎又銘在操場上打籃球"}
}
{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"黎又銘早上吃了一碗面"} 
}

 查詢下所有數據可以看到

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 3.0561461,
    "hits": [
      {
        "_index": "liyouming",
        "_type": "liyoumingtext",
        "_id": "c7eQAWoB0Mh7sqcTGY-K",
        "_score": 3.0561461,
        "_source": {
          "mytext": "中午黎又銘在操場上打籃球"
        }
      },
      {
        "_index": "liyouming",
        "_type": "liyoumingtext",
        "_id": "dLeQAWoB0Mh7sqcTdo9b",
        "_score": 2.1251993,
        "_source": {
          "mytext": "深夜還在寫代碼的人只有黎又銘"
        }
      },
      {
        "_index": "liyouming",
        "_type": "liyoumingtext",
        "_id": "crePAWoB0Mh7sqcTzY-2",
        "_score": 0.8630463,
        "_source": {
          "mytext": "黎又銘早上吃了一碗面"
        }
      }
    ]
  }
}

檢索下籃球並高亮文本內容可以看到下面的結果 <tag1>籃球</tag1> 已經被高亮標簽處理

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0187154,
    "hits": [
      {
        "_index": "liyouming",
        "_type": "liyoumingtext",
        "_id": "c7eQAWoB0Mh7sqcTGY-K",
        "_score": 1.0187154,
        "_source": {
          "mytext": "中午黎又銘在操場上打籃球"
        },
        "highlight": {
          "mytext": [
            "中午黎又銘在操場上打<tag1>籃球</tag1>"
          ]
        }
      }
    ]
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM