美文网首页ES
Docker安装IK分词器

Docker安装IK分词器

作者: 问瑾遗 | 来源:发表于2019-02-23 18:05 被阅读0次

    上文: Win10中Docker安装Elasticsearch

    我们需要安装IK分词器
    进入容器,会发现此时plugins文件夹下空空如也:

    D:\>docker exec -it tensquare_es /bin/bash
    root@fe7ebf1c90bf:/usr/share/elasticsearch# cd plugins/
    root@fe7ebf1c90bf:/usr/share/elasticsearch/plugins# ls
    root@fe7ebf1c90bf:/usr/share/elasticsearch/plugins# exit
    exit
    

    1.进入容器

    D:\>docker exec -it tensquare_es /bin/bash
    

    2.安装ik插件

    root@b9bc5264d13d:/usr/share/elasticsearch# elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.8/elasticsearch-analysis-ik-5.6.8.zip
    

    由于用的是校园网,刚开始总是报SSL异常的错误,后来更换为自己的手机热点,不再报错,很快就完成了。
    当然也可以从别处下载到本地后直接将ik解压cp到plugins目录下

    3.重启容器

    docker restart myes
    

    IK分词器用法示例:

    参考github上ik示例

    Quick Example

    1.create a index

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

    2.create a mapping

    curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
    {
            "properties": {
                "content": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
    
    }'
    

    3.index some docs

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

    4.query with highlighting

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

    Result

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

    发现分词器已生效了。

    相关: Docker安装elasticsearch‐head

    相关文章

      网友评论

        本文标题:Docker安装IK分词器

        本文链接:https://www.haomeiwen.com/subject/rlcuyqtx.html