restHighLevelClient配置IK分词器

前提:ElasticSearch已经在插件目录下安装IK分词器

启动Elasticsearch。

首先创建一个restHighLevelClient连接

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client =
            new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        return client;
    }

}

然后创建索引,配置IK分词的规则。

 @PostMapping("/ikCreate")
    public void ikCreate(String index,@RequestBody String data) throws  Exception {
        //1.创建索引的请求
        CreateIndexRequest request = new CreateIndexRequest(index);

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                //创建电影ID文档字段
                builder.startObject("movie_id");
                {
                    builder.field("type", "text");
                }
                builder.endObject();
                //创建电影名字文档字段
                builder.startObject("movie_name");
                {
                    builder.field("type", "text")
                            //插入时分词
                            .field("analyzer", "ik_max_word")
                            //搜索时分词
                            .field("search_analyzer", "ik_smart");
                }
                builder.endObject();
                //创建电影描述文档字段

                builder.startObject("movie_detail");
                {
                    builder.field("type", "text")
                            //插入时分词
                            .field("analyzer", "ik_max_word")
                            //搜索时分词
                            .field("search_analyzer", "ik_smart");
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        request.mapping(builder);
        //2客户端执行请求,请求后获得响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response);

    }

然后插入数据,便于后续验证查询

    //添加文档(进行了IK分词的doc)
    @PostMapping("testAddIKDoc")
    void testAddIKDoc(String index, @RequestBody String data) throws IOException {
        Object o = JSON.parseObject(data);
        //创建请求
        IndexRequest request = new IndexRequest(index);
        request.timeout(TimeValue.timeValueSeconds(1));
        //request.type("ysz");
        //将数据放入请求中
        request.source(JSON.toJSONString(o), XContentType.JSON);
        //客户端发送请求,获取请求结果
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse);

    }

最后再Postman中验证接口

    @PostMapping("/getLikeDoc")
    List GetLikeDoc(String name,String index) throws IOException {
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders
                //.fuzzyQuery("op_member.name", name)
                .fuzzyQuery("movie_name",name)
        );
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        List result = new ArrayList();
        for (int i = 0; i < hits.length; i++) {
            result.add(i, hits[i].getSourceAsMap());
        }
        Stream.of(hits).forEach(System.out::println);
        return result;
    }

在postman中查看分词效果

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值