ElasticSearch利用IK實現全文搜索

要作到中文全文檢索還須要按照中文分詞庫 ,這裏就使用 IK來設置git

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

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來測試github

首先建立咱們的索引json

OK後建立 全文檢索相關設置 設置字段、分析器配置    ik_smart  、ik_max_wordapp

 分別添加以下數據curl

 

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

 查詢下全部數據能夠看到elasticsearch

{
  "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>"
          ]
        }
      }
    ]
  }
}
相關文章
相關標籤/搜索