RestClient查询ElasticSearch
Posted Rover20230226
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RestClient查询ElasticSearch相关的知识,希望对你有一定的参考价值。
package cn.itcast.hotel; import cn.itcast.hotel.pojo.HotelDoc; import com.alibaba.fastjson.JSON; import org.apache.http.HttpHost; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; @SpringBootTest public class HotelSearchApplicationTests private RestHighLevelClient client; @BeforeEach public void before() //client对象就是操作ES的对象 client = new RestHighLevelClient(RestClient.builder (HttpHost.create("http://192.168.221.135:9200"))); @AfterEach public void after() throws IOException client.close(); /** * match 分词再查询 */ @Test public void test01() throws IOException //todo 2.搜索条件封装 SearchRequest searchRequest = new SearchRequest("hotel"); //todo 4.所有搜索条件封装到 searchSourceBuilder对象 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //todo 5.添加对象 searchSourceBuilder.query(QueryBuilders.matchQuery("name","如家全季")); //todo 3.添加搜索条件对象 searchRequest.source(searchSourceBuilder); //todo 1.找到搜索的API SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); //todo 6.解析 searchResponse 返回结果 SearchHits hits = searchResponse.getHits(); System.out.println("查询到的总条数为:" + hits.getTotalHits().value); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) String sourceAsString = hit.getSourceAsString(); HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class); System.out.println("查询到的数据:" + hotelDoc); /** * match_all 查询所有 */ @Test public void test02() throws IOException SearchRequest searchRequest = new SearchRequest("hotel"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits searchHits = searchResponse.getHits(); System.out.println("查询总条数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class); System.out.println("数据:" + hotelDoc); /** * term 精确查询 */ @Test public void test03() throws IOException SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.termQuery("brand","万怡")); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits searchHits = searchResponse.getHits(); System.out.println("总条数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class); System.out.println("数据:" + hotelDoc); /** * rang 范围查询 */ @Test public void test04() throws IOException SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.rangeQuery("price").gt(1000).lt(3000)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits searchHits = searchResponse.getHits(); System.out.println("总条数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class); System.out.println("数据:" + hotelDoc);
package cn.itcast.hotel.pojo; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class HotelDoc private Long id; private String name; private String address; private Integer price; private Integer score; private String brand; private String city; private String starName; private String business; private String location; private String pic; public HotelDoc(Hotel hotel) this.id = hotel.getId(); this.name = hotel.getName(); this.address = hotel.getAddress(); this.price = hotel.getPrice(); this.score = hotel.getScore(); this.brand = hotel.getBrand(); this.city = hotel.getCity(); this.starName = hotel.getStarName(); this.business = hotel.getBusiness(); this.location = hotel.getLatitude() + ", " + hotel.getLongitude(); this.pic = hotel.getPic();
以上是关于RestClient查询ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch之使用RestClient实现null和非null的查询操作
Elasticsearch之使用RestClient实现日期和数值的范围查询操作