springboot整合elasticsearch:7.6.2版本

温馨提示:

        es,kibana,ik的安装可看之前的文章

       docker安装elasticsearch,kibana,ik分词器_Give_time_to_Bug的博客-CSDN博客

再次强调:

        elasticsearch版本与springboot版本一定要对应,否则bug极多

        我安装的elasticsearch是7.6.2版本,对应使用的springboot是2.3.2.RELEASE

接下来介绍两种接入方式:

一.使用high-level-client的方式

1.pom依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- 这里我的springboot的版本使用是2.3.2 -->
        <version>2.3.2.RELEASE</version>
    </parent>


    <!--es客户端-->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.6.2</version>
    </dependency>

2.配置文件application.properties

es.ip=127.0.0.1
es.port=9200

3.客户端配置文件

@ConfigurationProperties("es")
@Data
@Component
public class ESProperties {

    private String ip;
    private int port;
}
@Configuration
public class ESConfig {

    @Autowired
    private ESProperties properties;

    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        //在这里配置你的elasticsearch的情况
                        new HttpHost(properties.getIp(), properties.getPort(), "http")
                )
        );
        return client;
    }

}

4.service接口调用

    @Autowired
    private RestHighLevelClient client;

    @Override
    public List<Shop> query(String keyword) throws IOException {
        SearchRequest request = new SearchRequest("shop");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.should(QueryBuilders.matchQuery("name", keyword));
        query.should(QueryBuilders.matchQuery("location", keyword));

        builder.query(query)
                .from(0)
                .size(5);
        request.source(builder);

        SearchResponse search = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = search.getHits().getHits();

        return Arrays.stream(hits).map(hit -> {
            String str = hit.getSourceAsString();
            return JSONObject.parseObject(str, Shop.class);
        }).collect(Collectors.toList());
    }

二.使用spring-boot-data的方式

1.依赖

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        注:因为我pom 中有high-level,所以我手动剔除了一下

2.application.properties配置

spring.elasticsearch.rest.uris=http://127.0.0.1:9200

3.实体类

@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "shop",shards = 1,replicas = 1)
public class Shop implements Serializable {

    //@Id
    @Field(type = FieldType.Integer,name = "id")
    @ApiModelProperty(value = "主键")
    private Integer id;

    @ApiModelProperty(value = "名称")
    @Field(type = FieldType.Text,name = "name", analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String name;

    @ApiModelProperty(value = "地址")
    @Field(type = FieldType.Text,name = "location", analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String location;

    @ApiModelProperty(value = "平均消费")
    @Field(type = FieldType.Integer,name = "price_per_man")
    private Integer pricePerMan;

}

4.dao层接口

@Repository
public interface ShopDao extends ElasticsearchRepository<Shop, Long> {

    List<Shop> findByNameOrLocation(String name,String location);

}

        注:这个方法是自定义方法,es会自动解析为查询name和location,因为查询内容为两个属性,所以传参需要是两个

5.service接口调用

    @Autowired
    private ShopDao shopDao;

    
    @Override
    public List<Shop> test(String keyword) {
        return shopDao.findByNameOrLocation(keyword,keyword);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值