查询的基本步骤是:
1. 创建 SearchRequest 对象 2. 准备 Request.source() ,也就是 DSL 。 ① QueryBuilders 来构建查询条件 ② 传入 Request.source() 的 query() 方法 3. 发送请求,得到结果 4. 解析结果(参考 JSON 结果,从外到内,逐层解析)@Test void testMatchAllHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.matchAllQuery()); // 3.发起请求 testFindHotelIndex(request); } @Test void tesMatchHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.matchQuery("all","如家")); // 3.发起请求 testFindHotelIndex(request); } public void testFindHotelIndex(SearchRequest request) throws IOException { // 3.发起请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //System.out.println(response); //4.解析结果 SearchHits hits = response.getHits(); //4.1查询总条数 long value = hits.getTotalHits().value; System.out.println("es搜索到:" + value + "条数据"); //4.2查询的结果数组 SearchHit[] hitsHits = hits.getHits(); for (SearchHit e : hitsHits) { //4.3 得到source String json = e.getSourceAsString(); //4.4 打印 System.out.println("json = " + json); HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); System.out.println("hotelDoc = " + hotelDoc.toString()); } }
其它查询变化不大
private RestHighLevelClient client; @BeforeEach void setUp() { this.client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.2.199:9200") )); } @AfterEach void tearDown() throws IOException { this.client.close(); } @Test void testClient() { System.out.println(client); } @Test void testMatchAllHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.matchAllQuery()); // 3.发起请求 testFindHotelIndex(request); } @Test void testMultiMatchHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.multiMatchQuery("如家","name","business","brand")); // 3.发起请求 testFindHotelIndex(request); } @Test void tesMatchHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.matchQuery("all","如家")); // 3.发起请求 testFindHotelIndex(request); } @Test void testTermHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.termQuery("city","上海")); // 3.发起请求 testFindHotelIndex(request); } @Test void testRangeHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(500)); // 3.发起请求 testFindHotelIndex(request); } @Test void testBoolHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL // 2.1创建布尔查询 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //2.2 添加must条件 2.3添加filter条件 boolQuery.must(QueryBuilders.termQuery("city", "上海")) .filter(QueryBuilders.rangeQuery("price").lte(600)); //boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250)); request.source().query(boolQuery); // 3.发起请求 testFindHotelIndex(request); } @Test void testSortHotelIndex() throws IOException { int page = 1,size = 5; // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.matchAllQuery()) .sort("price", SortOrder.ASC).from((page-1)*size).size(size); // 3.发起请求 testFindHotelIndex(request); } @Test void testHighLightHotelIndex() throws IOException { // 1.创建Request对象 SearchRequest request = new SearchRequest("hotel"); // 2.准备DSL request.source().query(QueryBuilders.termQuery("city","上海")); //2.2 高亮查询 request.source().highlighter(new HighlightBuilder().field("name"). requireFieldMatch(false)); // 3.发起请求 testFindHotelIndex(request); } public void testFindHotelIndex(SearchRequest request) throws IOException { // 3.发起请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //System.out.println(response); //4.解析结果 SearchHits hits = response.getHits(); //4.1查询总条数 long value = hits.getTotalHits().value; System.out.println("es搜索到:" + value + "条数据"); //4.2查询的结果数组 SearchHit[] hitsHits = hits.getHits(); for (SearchHit e : hitsHits) { //4.3 得到source String json = e.getSourceAsString(); //4.4 打印 //System.out.println("json = " + json); //反序列化 HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); //获取高亮结果 Map
算法控制实现(广告排名靠前)
//2.算法控制 FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery( //原始数据查询,相关性算分的查询 boolQueryBuilder, new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ //其中一个function score 元素 new FunctionScoreQueryBuilder.FilterFunctionBuilder( //过滤条件 QueryBuilders.termQuery("isAD", true), //算分函数 ScoreFunctionBuilders.weightFactorFunction(10) ) }); request.source().query(functionScoreQueryBuilder);