Elasticsearch安装ik分词

elasticsearch是自带中文分词的, 但是基本上是每个单字的分, 效果不好。

medcl大神的ik分词, 是专门的中文分词。更多信息,可参考 https://github.com/medcl/elasticsearch-analysis-ik

1. 安装ik插件

可以从 https://github.com/medcl/elasticsearch-analysis-ik/releases 下载适合的匹配ES版本的包,

下载之后, 放到plugin目录下, 解压即可使用了。

2. 创建index的时候, 给出mapping, 在mapping中, 指定字段所使用的analyzer为ik

e.g. curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        },
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'

具体的case可参考https://github.com/medcl/elasticsearch-analysis-ik

3. 对于分词的效果, 可使用 _analyze 来查看

e.g. GET localhost:9200/_analyze -d '
 {
  "analyzer":"ik_max_word",
  "text" : "助手P5 5.14.9003"
}’

4. 对于同一个字段, 使用不同分词器的情况, 可参考http://keenwon.com/1404.html 给出的例子, 对一个field建立多个子field, 对该field及多个子fields使用不同的analyzer。

e.g.  下面黄色标出的部分, 即是title这个field的子fileds : cn 和 en

对title本身使用的是标准分词器, 对title.cn使用的是ik分词器,对title.cn使用的是自带的英文分词器。

PUT http://192.168.159.159:9200/index1
{
  "settings": {
     "refresh_interval": "5s",
     "number_of_shards" :   1, // 一个主节点
     "number_of_replicas" : 0 // 0个副本,后面可以加
  },
  "mappings": {
    "_default_":{
      "_all": { "enabled":  false } // 关闭_all字段,因为我们只搜索title字段
    },
    "resource": {
      "dynamic": false, // 关闭“动态修改索引”
      "properties": {
        "title": {
          "type": "string",
          "index": "analyzed",
          "fields": {
            "cn": {
              "type": "string",
              "analyzer": "ik"
            },
            "en": {
              "type": "string",
              "analyzer": "english"
            }
          }
        }
      }
    }
  }
}
在搜索的时候, 同时匹配该字段及其子字段就可以了。

POST http://192.168.159.159:9200/index1/resource/_search

{
  "query": {
    "multi_match": {
      "type":     "most_fields", 
      "query":    "最新",
      "fields": [ "title", "title.cn", "title.en" ]
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值