【Elasticsearch】Elasticsearch 6.x 探索之路-中文分词器IK

1.分词机制

Elasticsearch对于查询,是采取按分词的结果进行查询的,作为一款非国产的软件,自然对于中文的查询支持并不是很好,默认只会把中文拆分成单字,而通常使用都是以“词”作为基准单位的。

我们可以使用插件(plugins)机制去拓展Elasticsearch的分词器(analyzer)


2.中文分词器-IK

IK是常用的一款中文分词插件

Github地址:https://github.com/medcl/elasticsearch-analysis-ik/

有两种方法安装插件


2.1方法一:直接将插件相关文件放到es的plugins文件夹下

路径示例:

 /usr/share/elasticsearch/plguins/ik

2.2方法二:使用es提供的elasticsearch plugin install

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

其中的6.3.0是es的版本号

 


3.使用

安装插件后,直接重启es即可自动加载

systemctl stop elasticsearch.service
systemctl start elasticsearch.service

我们创建一个index并在mappings配置中指定分词器为IK

$ curl -X PUT 'localhost:9200/news' -d '
{
  "mappings": {
    "new": {
      "properties": {
        "content": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}'

以下使用IK上的示例

curl -XPOST http://localhost:9200/news/new/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/news/new/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/news/new/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'

 

curl -XPOST http://localhost:9200/news/new/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

进行查询

curl -XPOST http://localhost:9200/news/new/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

结果: 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值