elasticsearch中文搜索环境搭建

分类: 后端

2017-08-23

|

6919

|

评论:1

分享:

1.下载安装elsticsearch

https://www.elastic.co/downloads/elasticsearch 直接下载解压就行 运行:bin/elasticsearch (-d 可以后台运行),不能用root运行!

2.安装ik插件

【ik版本必须和es版本一样】 https://github.com/medcl/elasticsearch-analysis-ik 下载后解压到elasticsearch 下的plugins目录。 最好不要用elasticsearch-plugin安装插件,会少了自带的字典。 注:ik插件在加载远程字典时会输出每条字典,不可配置加载字典的时间间隔,可以我用修改过的:https://github.com/gojuukaze/elasticsearch-analysis-ik/releases

3.配置

3.1 配置es

  • 配置外网访问
vi config/elasticsearch.yml 
加入:
http.host: 172.20.140.5, 127.0.0.1
  •  创建index与mapping,参照ik的说明就行:
curl -XPUT http://localhost:9200/index


curl -XPOST http://localhost:9200/index/question/_mapping -d'
{
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
    
}'

3.2配置ik

配置ik字典,也可以不配置,他默认会有一个字典
~ vi plugins/ik/config/IKAnalyzer.cfg.xml

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 扩展配置</comment>
    <!--用户可以在这里配置自己的扩展字典 -->
    <!--插件自带了一下扩展字典,可以都加上 -->
    <entry key="ext_dict">extra_main.dic;extra_single_word.dic;extra_single_word_full.dic</entry>
     <!--用户可以在这里配置自己的扩展停止词字典-->
    <entry key="ext_stopwords">ext_stopword.dic</entry>
    <!--用户可以在这里配置远程扩展字典 -->
    <entry key="remote_ext_dict">http://xxx.com/xxx.dic</entry>
    <!--用户可以在这里配置远程扩展停止词字典-->
    <entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>
如果用我编译的ik还可以配置字典更新时间
  <!--获取远程扩展字典的时间间隔,单位:s ,默认60s-->
   <entry key="update_remote_ext_dict_period">60</entry>
   <!--获取远程扩展停止词字典的时间间隔,单位:s ,默认60s-->
   <entry key="update_remote_stop_dict_period">60</entry>

测试插入数据

curl -XPOST http://localhost:9200/zhizi_prod_es/question_document/1 -d'
{"title":"我是问题?"}'
curl -XPOST http://localhost:9200/zhizi_prod_es/question_document/2 -d'
{"title":"第二个问题?"}'

测试搜索数据

curl -XPOST http://localhost:9200/zhizi_prod_es/question_document/_search  -d'
{
    "query" : { 
        "match" : { "title" : "问题" }
    }
}
'
match:匹配任意一个词。比如搜“第二个问题”,仍然可以搜出“我是问题?”。 其他还有 match_phrase:匹配所有词 term:不进行分词,直接比较值。比如搜“第二个问题”,少了问号,则搜不出“第二个问题?”    



转载请注明来源

文章:elasticsearch中文搜索环境搭建

链接:/article/26

作者:gojuukaze

本文共 1 个回复


发表评论 (对文章评论)

captcha