Elasticsearch安装IK分词器

安装方式大概分为两种:

  1. 自行编译;
  2. 使用elasticsearch-rtf版,利用别人编译好的文件进行安装;

环境说明:

我下载的es版本是:elasticsearch-1.7.4.tar.gz,这里我是用的ik版本是elasticsearch-rtf-1.0.0.zip解压后得到的elasticsearch-analysis-ik-1.2.6.jar文件。

方式一:自行编译

自行编译的步骤如下:

1.到https://github.com/medcl/elasticsearch-analysis-ik页面下载elasticsearch-analysis-ik-x.x.x.zip压缩包;
2.解压elasticsearch-analysis-ik-x.x.x.zip,然后进行elasticsearch-analysis-ik-x.x.x目录;
3.使用maven进行打包,得到elasticsearch-analysis-ik-x.x.x.jar(然而我并不会使用maven进行打包,所以我没用这种方式);
4.进行elasticsearch-1.7.4/plugins目录,创建目录analysis-ik,并把你编译好的elasticsearch-analysis-ik-x.x.x.jar放置到此目录;
5.将你下载的elasticsearch-analysis-ik-x.x.x.zip解压后的config目录下的ik目录复制到elasticsearch-1.7.4/config目录;
6.配置elasticsearch-1.7.4/config目录下的elasticsearch.yml文件,在文件尾部加入如下代码:

index:  
      analysis:                     
        analyzer:        
          ik:  
              alias: [ik_analyzer]  
              type: org.elasticsearch.index.analysis.IkAnalyzerProvider  
          ik_max_word:  
              type: ik  
              use_smart: false  
          ik_smart:  
              type: ik  
              use_smart: true

或简单配置:

index.analysis.analyzer.ik.type : "ik"

7.重新启动es;

方式二:使用elasticsearch-rtf版,利用别人编译好的文件进行安装

我使用的就是这种方式,第一种方式我并没有进行测试,所以暂时不保证正确,着重说明一下第二种方式,步骤如下:

1.到这里https://github.com/medcl/elasticsearch-rtf/releases下载rtf版的es,我下载的是elasticsearch-rtf-1.0.0.zip;
2.解压elasticsearch-rtf-1.0.0.zip压缩包;
3.将elasticsearch-rtf-1.0.0/plugins/analysis-ik目录,复制到elasticsearch-1.7.4/plugins得到elasticsearch-1.7.4/plugins/analysis-ik;
4.将elasticsearch-rtf-1.0.0/config/ik复制到elasticsearch-1.7.4/config/得到elasticsearch-1.7.4/config/ik;
5.编辑elasticsearch-1.7.4/config/elasticsearch.yml文件,在文件尾部加入以下内容:

index:  
      analysis:                     
        analyzer:        
          ik:  
              alias: [ik_analyzer]  
              type: org.elasticsearch.index.analysis.IkAnalyzerProvider  
          ik_max_word:  
              type: ik  
              use_smart: false  
          ik_smart:  
              type: ik  
              use_smart: true

或简单配置:

index.analysis.analyzer.ik.type : "ik"

6.重新启动es;

测试(我在第二种安装方式下进行的测试)

创建索引:

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

创建映射:

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
            }
        }
    }
}'

为索引添加一些内容:

curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

进行高亮查询:

curl -XPOST http://localhost:9200/index/fulltext/_search  -d'
{
    "query" : { "term" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

查询结果:

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

或者你可以直接在浏览器地址栏进行测试:http://localhost:9200/index/_analyze?analyzer=ik&pretty=true&text=%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA

注意:如果你的版本不对应,可能会出现如下错误:

{"error":"IndexCreationException[[index] failed to create index]; nested: ElasticsearchIllegalArgumentException[failed to find analyzer type [ik] or tokenizer for [ik_max_word]]; nested: NoClassSettingsException[Failed to load class setting [type] with value [ik]]; nested: ClassNotFoundException[org.elasticsearch.index.analysis.ik.IkAnalyzerProvider]; ","status":400}

参考文章:

1.http://samchu.logdown.com/posts/277928-elasticsearch-chinese-word-segmentation;
2.https://github.com/medcl/elasticsearch-analysis-ik;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 Elasticsearch安装 IK 分词器,需要以下几步操作: 1. 下载 IK 分词器插件,并将其复制到 Elasticsearch 插件目录中。 2. 使用 Elasticsearch 的命令行工具安装插件,例如: ``` bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.0/elasticsearch-analysis-ik-7.10.0.zip ``` 3. 重启 Elasticsearch 节点,使 IK 分词器生效。 4. 在你的索引的映射中使用 IK 分词器,例如: ``` PUT my_index { "mappings": { "_doc": { "properties": { "message": { "type": "text", "analyzer": "ik_max_word" } } } } } ``` 这样 IK 分词器安装完成了。 ### 回答2: Elasticsearch是一个广泛应用于全文搜索、日志分析、业务指标和数据分析等领域的开源搜索引擎,对于中文搜索的支持是非常重要的。而IK分词器为其提供了中文分词的支持。本答案将为您解释如何安装IK分词器。 1. 下载IK分词器: 首先需要下载适用于您当前使用的Elasticsearch版本的IK分词器,可以在开源中国上下载。下载后将其解压至任意目录,例如解压到“C:\ik”。 2. 安装IK分词器: 将解压后的IK分词器安装Elasticsearch。只需在Elasticsearch的根目录下启动终端,通过以下命令安装IK分词器: ``` ./bin/elasticsearch-plugin install file:///PATH_TO_FILE/ik-plugin-x.x.x.zip ``` 其中,PATH_TO_FILE即为解压后保存IK分词器的路径。"x.x.x"为IK分词器的版本号,替换为实际的版本号。 3. 测试IK分词器: 在Elasticsearch中创建索引,指定使用IK分词器: ``` PUT /my_index { "settings": { "analysis": { "analyzer": { "ik_analyzer": { "type": "custom", "tokenizer": "ik_smart" } } } } } ``` 以上示例将IK分词器设置为my_index索引的默认分词器。可通过以下命令来测试IK分词器的效果: ``` GET /my_index/_analyze { "analyzer": "ik_analyzer", "text": "中文分词测试" } ``` 以上即为安装IK分词器的步骤和测试方法,通过以上步骤可以快速、方便地为您的Elasticsearch添加中文搜索的支持。 ### 回答3: Elasticsearch是一个开源的搜索引擎,它可以帮助用户快速和准确地检索大量的数据。而IK分词器则是一款中文分词插件,它可以将中文文本分解为词语,从而提高搜索结果的准确性。那么,如何在Elasticsearch安装IK分词器呢? 安装方法如下: Step 1:下载ik分词器插件 可以从GitHub下载最新的ik分词器此时版本为7.11.2 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.2/elasticsearch-analysis-ik-7.11.2.zip Step 2:解压ik插件 将下载好的插件解压到elasticsearch的plugins目录中: unzip elasticsearch-analysis-ik-7.11.2.zip -d /path/to/elasticsearch-<version>/plugins/ Step 3:重启ElasticSearch 重启Elasticsearch服务器,使插件生效: ./elasticsearch Step 4:安装分词器 使用curl命令安装中文分词器: curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_index_settings.json http://localhost:9200/_settings curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_synonyms.json http://localhost:9200/_settings Step 5:创建测试索引 为了测试是否安装成功,可以创建一个测试索引和mapping: curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_test_index.json http://localhost:9200/ik_test 注意事项: - 确认Elasticsearch和插件版本是否一致。 - 插件应该安装在每个Elasticsearch节点上的plugins目录中。 - 确认分词器的配置是否正确。 - 在添加文档时,使用正确的字段名和正确的分词器。 总之,以上是在Elasticsearch安装IK分词器的方法,如果您遇到了什么问题,请详细阅读官方文档或向社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值