文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言一、新版本
1.引入依赖2.配置文件3.测试类 二、旧版本
1.引入依赖2.配置文件
3.实体类 4.编写Repository5.测试类 总结
前言
ElasticSearch整合SpringBoot因为ES版本问题有不同的使用方法,这里写两种。
区分版本信息查看ElasticsearchRepository接口
新版本:
旧版本:
一、新版本 1.引入依赖
提示:exclusion是解决版本冲突问题
2.配置文件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
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.引入依赖
2.配置文件org.springframework.boot spring-boot-starter-data-elasticsearch 2.3.1.RELEASE
提示:旧版本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 ElasticsearchRepository5.测试类{ //AirportEntity是实体类 //Integer为主键类型 }
@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;
}
总结
更多使用方法:参考链接



