SpringBoot整合新旧版本ElasticSearch及使用


目录
  • 前言
  • 一、新版本
    • 1.引入依赖
    • 2.配置文件
    • 3.测试类
  • 二、旧版本
    • 1.引入依赖
    • 2.配置文件
      • 3.实体类
    • 4.编写Repository
    • 5.测试类
  • 总结


前言

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

旧版本:
旧版本接口


一、新版本

1.引入依赖

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

 	
            org.elasticsearch
            elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
            2.6.4
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.15.2
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.15.2
            
                
                    org.elasticsearch
                    elasticsearch
                
            
      

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 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 airportEntityList = new ArrayList<>();
            for (SearchHit searchHit : searchHitArr) {
                Map 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.引入依赖

        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
            2.3.1.RELEASE
        

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为主键类型
}

5.测试类

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

    @Override
    public List 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 airportEntityIterable = airportRepository.search(builder.build());
        ListairportEntityList = new ArrayList<>();
        airportEntityIterable.forEach(airportEntityList::add);
        return airportEntityList;
    }

总结

更多使用方法:参考链接