SpringBoot整合新旧版本ElasticSearch及使用


前言

ElasticSearch整合SpringBoot因为ES版本问题有不同的使用方法,这里写两种。
区分版本信息查看ElasticsearchRepository接口
新版本:
旧版本接口

旧版本:
旧版本接口


一、新版本

1.引入依赖

提示:exclusion是解决版本冲突问题

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

2.配置文件

spring:
  elasticsearch:
    uris: http://127.0.0.1:9200

3.测试类

@Service
@Slf4j
public class WordSearchServiceImpl implements WordSearchService {
    @Resource
    private RestHighLevelClient client;
    private final RequestOptions options = RequestOptions.DEFAULT;

    @Override
    public List<AirportEsEntity> searchAirport(String keyWord) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.should(QueryBuilders.matchPhraseQuery("cityName", keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportCode", keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportName", keyWord))
                .should(QueryBuilders.matchPhraseQuery("citySpell", keyWord))
                .should(QueryBuilders.matchPhraseQuery("cityShortSpell", keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportNam" + "eSpell", keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportNameShortSpell", keyWord))
                .should(QueryBuilders.matchPhraseQuery("initials", keyWord));
        sourceBuilder.query(builder);
        SearchRequest searchRequest = new SearchRequest("airport");//该参数为indexName
        searchRequest.source(sourceBuilder);

        try {
            SearchResponse searchResp = client.search(searchRequest, options);
            SearchHit[] searchHitArr = searchResp.getHits().getHits();
            List<AirportEsEntity> airportEntityList = new ArrayList<>();
            for (SearchHit searchHit : searchHitArr) {
                Map<String, Object> temp = searchHit.getSourceAsMap();
                temp.put("id", searchHit.getId());
                AirportEsEntity airportEsEntity = JSONObject.parseObject(JSON.toJSONString(temp), AirportEsEntity.class);
                airportEntityList.add(airportEsEntity);
            }

            return airportEntityList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

二、旧版本

1.引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>

2.配置文件

提示:旧版本yml路径不同

spring:
  elasticsearch:
    rest:
      uris: http://127.0.0.1:9200 #配置ES的连接地址 --测试地址

3.实体类

/**
 * 实体
 * indexName = "movies" movies 就是ES中的索引(库)
 */
@Document(indexName = "airport")
@Data
public class AirportEntity {
   /**
   * 表示ID属性
   */
    @Id
    private Integer id;
   /**
    * @Field 定义属性字段 (指定分词规则,指定属性的类型)
    * ik_smart 粗分
    * ik_max_word 细分
    */
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String cityName;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String airportCode;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String airportName;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String citySpell;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String cityShortSpell;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String airportNameSpell;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String airportNameShortSpell;
    @Field(analyzer = "ik_smart", type = FieldType.Text)
    private String initials;
}

4.编写Repository

代码如下(示例):

public interface AirportRepository extends ElasticsearchRepository<AirportEntity,Integer> {
//AirportEntity是实体类
//Integer为主键类型
}

5.测试类

@Service
public class AirportServiceImpl implements AirportSerivce {
    @Resource
    private AirportRepository airportRepository;

    @Override
    public List<AirportEntity> search(String keyWord) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery("cityName",keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportCode",keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportName",keyWord))
                .should(QueryBuilders.matchPhraseQuery("citySpell",keyWord))
                .should(QueryBuilders.matchPhraseQuery("cityShortSpell",keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportNameSpell",keyWord))
                .should(QueryBuilders.matchPhraseQuery("airportNameShortSpell",keyWord))
                .should(QueryBuilders.matchPhraseQuery("initials",keyWord)));

        Iterable<AirportEntity> airportEntityIterable = airportRepository.search(builder.build());
        List<AirportEntity>airportEntityList = new ArrayList<>();
        airportEntityIterable.forEach(airportEntityList::add);
        return airportEntityList;
    }

总结

更多使用方法:参考链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值