Elasticsearch整合IK分词器

发布时间: 5年前 (2019-12-16)浏览: 1116评论: 0


原文链接:

https://blog.csdn.net/wdy_2099/article/details/85690138


一.ik分词器的打包安装

官网代码地址:https://github.com/medcl/elasticsearch-analysis-ik
1)下载-解压
elasticsearch-analysis-ik-master.zip
解压后:
在这里插入图片描述
2)进入解压后的目录执行mvn打包命令

mvn package -Pdist,native -DskipTests -Dtar

   

整个过程如下:

F:\BaiduNetdiskDownload\elasticsearch-analysis-ik-master>mvn package -Pdist,native -DskipTests -Dtar
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for org.elasticsearch:elasticsearch-analysis-ik:jar:5.6.1
[WARNING] 'version' contains an expression but should be a constant. @ org.elasticsearch:elasticsearch-analysis-ik:${elasticsearch.version}, F:\BaiduNetdiskDownload\elasticsearch-analysis-ik-master\pom.xml, line 9, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building elasticsearch-analysis-ik 5.6.1
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-enforcer-plugin/1.2/maven-enforcer-plugin-1.2.pom
.......................

4)打包完成后,在.\elasticsearch-analysis-ik-master\target\releases目录下会看到打包后的zip文件elasticsearch-analysis-ik-版本号.zip,我这里是elasticsearch-analysis-ik-5.6.1.zip
5)解压生成的elasticsearch-analysis-ik-5.6.1.zip
6)将解压后的elasticsearch目录拷贝到es的安装目录下的plugins目录下
7)重启es:bin/elasticsearch (注意是非root用户哦)
启动过程中,会发现analysis-ik插件已经被加载成功
在这里插入图片描述
二.测试使用

【接前文工程代码哦】
1)新建一个index:blog3

//忽略代码
  @Test
    public void esTest() throws UnknownHostException {
        //获取集群信息
        Settings settings = Settings.builder().put("cluster.name","my-es-search").build();
        //获取客户端
        PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);
        //连接指定客户端主机
        preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.117"), 9300));
        System.out.println("" + preBuiltTransportClient.toString());
        //创建索引
        preBuiltTransportClient.admin().indices().prepareCreate("blog3").get();
        //最后关闭客户端
        preBuiltTransportClient.close();
    }

   

    创建对应mapping:给对应的字段上新增.field(“analyzer”,“ik_smart”)
    这里analyzer取值有2种:
    1.ik_smart:表示智能分词(可以认为最小化分词个数)
    2.ik_max_word:表示最大化分词个数

        //##通用代码,同上##
        //  1设置mapping
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("article")
                .startObject("properties")
                .startObject("id")
                .field("type", "string")
                .field("store", "yes")
                .field("analyzer","ik_smart")
                .endObject()
                .startObject("title")
                .field("type", "string")
                .field("store", "no")
                .field("analyzer","ik_smart")
                .endObject()
                .startObject("content")
                .field("type", "string")
                .field("store", "yes")
                .field("analyzer","ik_smart")
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        // 2 添加mapping
        PutMappingRequest mapping = Requests.putMappingRequest("blog3").type("article").source(builder);
        preBuiltTransportClient.admin().indices().putMapping(mapping).get();
        //##通用代码,同上##

  

加完mapping的效果:
在这里插入图片描述
3)最后,在执行查询的时候就能查询到值了(前文中用默认的分词器没有查到,因为默认的分词器会把中文分成一个一个的字)
blog3的数据如下
在这里插入图片描述

        ////通配符查询
       String keywordLike = "第?";
       SearchResponse searchResponse = preBuiltTransportClient.prepareSearch("blog3").setTypes("article")
               .setQuery(QueryBuilders.wildcardQuery("title",keywordLike)).get();
       SearchHits searchHits = searchResponse.getHits();
       System.out.println("查询的总条数:" + searchHits.getTotalHits());//查询的总条数:3
       for (SearchHit searchHit : searchHits) {
           System.out.println("" + searchHit.getSourceAsString());
       }
       //查询的总条数:2
//{"atcId":"2","title":"第二文","content":"ID:ffmkiejjmecolpfloofpjologoblkegm\n"}
//{"atcId":"1","title":"第一文","content":"浏览器查看http://hadoop102:9200/blog2"}



发现数据都被查询出来了。

标签:

上一篇: ElasticSearch安装 --- windows版
下一篇: elastic search2.3.1(3) 查询语句拼接实战termQuery ,matchQuery, boolQuery, rangeQuery, wildcardQuery

相关文章暂无相关
评论列表暂无评论
发表评论
验证码

«   2024年4月   »
1234567
891011121314
15161718192021
22232425262728
2930
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
    网站收藏
    友情链接
    • RainbowSoft Studio Z-Blog
    • 订阅本站的 RSS 2.0 新闻聚合
    ︿
    Top