/** * 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); } }
标签:searchSourceBuilder,searchRequest,查询,ElasticSearch,searchResponse,todo,match From: https://www.cnblogs.com/Rover20230226/p/17498844.html