elasticsearch 6.2.3 安装IK分词器 及 IK分词器简单使用demo

Ik分词器github,本文参考了该内容。

IK分词器与elasticsearch 有版本对应关系,点击这里


安装

一、安装

第一种方式:下载预编译好的IK:https://github.com/medcl/elasticsearch-analysis-ik/releases,解压到你的es安装目录下的plugins目录:{your-es-root}/plugins/

第二种方式:使用elasticsearch的命令elasticsearch-plugin安装( version > v5.5.1 ),网络不好不一定能成功,这时可以采用第一种方式。

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.3/elasticsearch-analysis-ik-6.2.3.zip

二、重启elasticsearch,安装完成。


使用Demo

IK提供了两种elasticsearch分词器:ik_smart 和ik_max_word 

1、创建索引

PUT index

2、创建type,并指定字段使用ik_max_word分词器

POST index/fulltext/_mapping
{
  "properties": {
    "content":{
      "type":"text",
      "analyzer": "ik_max_word",
      "search_analyzer":"ik_max_word"
    }
  }
}

3、添加数据

POST index/fulltext/1
{
  "content" :"美国留给伊拉克的是个烂摊子吗"
}

POST index/fulltext/2
{
  "content" :"公安部:各地校车将享最高路权"
}

POST index/fulltext/3
{
  "content" :"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
}

POST index/fulltext/4
{
  "content" :"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
}

4、查询,并使结果高亮(<em>标签即高亮标签)

POST index/fulltext/_search
{
  "query": {
    "match": {
      "content": "中国"
    }
  },
  "highlight": {
    "fields": {"content":{}}
  }
}
----------------------------------------------------------------
{
  "took": 190,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.6489038,
    "hits": [
      {
        "_index": "index",
        "_type": "fulltext",
        "_id": "4",
        "_score": 0.6489038,
        "_source": {
          "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
        },
        "highlight": {
          "content": [
            "<em>中国</em>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
          ]
        }
      },
      {
        "_index": "index",
        "_type": "fulltext",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
        },
        "highlight": {
          "content": [
            "中韩渔警冲突调查:韩警平均每天扣1艘<em>中国</em>渔船"
          ]
        }
      }
    ]
  }
}

猜你喜欢

转载自blog.csdn.net/jiandabang/article/details/83340808
今日推荐