当前位置:   article > 正文

spring boot 集成 ES +ik分词器_springboot集成ik分词器

springboot集成ik分词器

1.TransportClient

  <!--es-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

public interface *****Respository extends ElasticsearchRepository<*****Vo, String> {
}
  • 1
  • 2

Repository接口的自动实现,包括对自定义查找器方法的支持。

2.RestClient

   <!--es-->
        <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.8.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.8.10</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

@Configuration
public class ElasticsearchConf extends AbstractElasticsearchConfiguration {

    @Value("${es.endpoints}")
    String esIp;

    @Override
    public RestHighLevelClient elasticsearchClient() {
        return RestClients.create(ClientConfiguration.create(esIp)).rest();
    }

    @Bean
    @Override
    public EntityMapper entityMapper() {

        ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
                elasticsearchMappingContext(), new DefaultConversionService()
        );
        entityMapper.setConversions(elasticsearchCustomConversions());

        return entityMapper;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
es:
  endpoints: 192.168.1.12:9200
  • 1
  • 2
@AllArgsConstructor
@NoArgsConstructor
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
@Document(indexName = "new_indexName")
public class TestDto {

   

    @ApiModelProperty(value = "采集地址")
    @Field(type = FieldType.Keyword,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String collectionAddr;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

因为我用的是docker所以只做了docker相关笔记

  1. 进入docker 容器

docker exec -it a70fda65d2ba /bin/bash

  1. 安装es对应分词器版本(找了半天对应的版本)

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.10/elasticsearch-analysis-ik-6.8.10.zip
分词器及相关下载
https://elasticsearch.cn/download/

  1. 查看安装的分词器
    在这里插入图片描述
  2. 退出/重启

exit
docker restart elasticsearch

5.分词查询

 @GetMapping("/ik")
    @ApiImplicitParam(name = "addr",value = "分词查询条件", paramType = "query", dataType = "String")
    public    Page<TestDto> getAddr(String addr){
        //使用dis_max直接取多个query中,分数最高的那一个query的分数即可
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();

        BoolQueryBuilder query = QueryBuilders.boolQuery().
                must(QueryBuilders.matchQuery("collectionAddr", addr).analyzer("ik_max_word"));
        NativeSearchQuery build = new NativeSearchQueryBuilder()
                .withQuery(query)
                .build();
       return testSaveEs.search(build);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6.如果需要分页

 //指定查询数据的分页数和按照时间进行排序
        Pageable pageable = PageRequest.of(pageindex, pagesize, Sort.Direction.ASC, "dateTime");
        //分页
          return testSaveEs.search(build,pageable);
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/860240
推荐阅读
相关标签
  

闽ICP备14008679号