栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合新旧版本ElasticSearch及使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合新旧版本ElasticSearch及使用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言一、新版本

1.引入依赖2.配置文件3.测试类 二、旧版本

1.引入依赖2.配置文件

3.实体类 4.编写Repository5.测试类 总结


前言

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.实体类
@document(indexName = "airport")
@Data
public class AirportEntity {
   
    @Id
    private Integer id;
   
    @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;
    }

总结

更多使用方法:参考链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777822.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号