The previous section describedSpringBoot implementation of elasticsearch indexing operationsThis section describes SpringBoot implementation of elasticsearch query operations.
0. Environmental preparation
The structure of the indexing library used in the case
PUT /hotel { "mappings": { "properties": { "id": { "type": "keyword" }, "name":{ "type": "text", "analyzer": "ik_max_word", "copy_to": "all" }, "address":{ "type": "keyword", "index": false }, "price":{ "type": "integer" }, "score":{ "type": "integer" }, "brand":{ "type": "keyword", "copy_to": "all" }, "city":{ "type": "keyword", "copy_to": "all" }, "starName":{ "type": "keyword" }, "business":{ "type": "keyword" }, "location":{ "type": "geo_point" }, "pic":{ "type": "keyword", "index": false }, "all":{ "type": "text", "analyzer": "ik_max_word" } } } }
1. Search all
@GetMapping("/searchAll") public List<HotelDoc> searchAll() throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types ().query(()); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
2. query by name match split word query
@GetMapping("/searchByName/{name}") public List<HotelDoc> searchByName(@PathVariable("name") String name) throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types ().query(("name", name)); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
3. query multiMatch splitter query by name and brand
@GetMapping("/searchByNameAndBrand/{name}") public List<HotelDoc> searchByNameAndBrand(@PathVariable("name") String name) throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types ().query((name,"name","brand")); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
4. according to brand query match split word query
@GetMapping("/searchByBrand/{name}") public List<HotelDoc> searchByBrand(@PathVariable("name") String name) throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types ().query(("brand", name)); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
5. Search by price range
@GetMapping("/searchByPrice/{low}/{high}") public List<HotelDoc> searchByPrice(@PathVariable("low") String low, @PathVariable("high") String high) throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types ().query(("price").gte(low).lte(high)); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
6. Precision queries
@GetMapping("/termQueryCity/{city}") public List<HotelDoc> termQueryCity(@PathVariable("city") String city) throws Exception { //1. Create a request semantic object SearchRequest searchRequest = new SearchRequest("Index name"); // QueryBuilders: building query types SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //((("city", city)); There's a little problem with this line. // /p/270426807 Ref. (("", city)); (searchSourceBuilder); SearchResponse searchResponse = (searchRequest, ); return handleResponse(searchResponse); }
7. boolQuery
@GetMapping("/testBool") public List<HotelDoc> testBool() throws Exception { // 1. Prepare the Request SearchRequest request = new SearchRequest("Index name"); // 2. Prepare the DSL // 2.1. Preparing BooleanQuery BoolQueryBuilder boolQuery = (); // 2.2. Adding a term (("", "Hangzhou subprovincial city and capital of Zhejiang province in southeast China")); // 2.3. Adding a range (("price").lte(250)); ().query(boolQuery); // 3. Send the request SearchResponse response = (request, ); // 4. Parsing the response return handleResponse(response); }
8. Paging
@GetMapping("/testPageAndSort/{currentPage}/{pageSize}") public List<HotelDoc> testPageAndSort(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) throws Exception { // Page number, size per page // 1. Prepare the Request SearchRequest request = new SearchRequest("Index name"); // 2. Prepare the DSL // 2. ().query(()); // 2.2. sort sort ().sort("price", ); // 2.3. Pagination from, size ().from((currentPage - 1) * pageSize).size(pageSize); // 3. Send the request SearchResponse response = (request, ); // 4. Parsing the response return handleResponse(response); }
9. Highlighting queries
@GetMapping("/testHighlight/{name}") void testHighlight(@PathVariable("name") String name) throws Exception { // 1. Prepare the Request SearchRequest request = new SearchRequest("Index name"); // 2. Prepare the DSL // 2. ().query(("name", name)); // 2.2. Highlighting ().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false)); // 3. Send the request SearchResponse response = (request, ); // 4. Parsing the response handleResponse2(response); }
10. Public parsing
private List<HotelDoc> handleResponse(SearchResponse response) throws Exception { // Get all the contents of the hit SearchHits searchHits = (); // Get the total number of hits long count = ().value; ("The number of hits is:"+ count); // Get an array of hit document objects SearchHit[] hits = (); List<HotelDoc> docList = new ArrayList<>(); for (SearchHit hit : hits) { // Parsing each hit object to get the corresponding document data String json = (); // HotelDoc hotelDoc = (json, ); ((json, )); } //destroy(); return docList; } private void handleResponse2(SearchResponse response) { // 4. Parsing the response SearchHits searchHits = (); // 4.1. Getting the total number of entries long total = ().value; ("Total searches found" + total + "Article data"); // 4.2. Document arrays SearchHit[] hits = (); // 4.3. Iteration for (SearchHit hit : hits) { // Get the document source String json = (); // Deserialization HotelDoc hotelDoc = (json, ); // Getting Highlighted Results Map<String, HighlightField> highlightFields = (); if ( !(highlightFields) ) { // Get highlighting results by field name HighlightField highlightField = ("name"); if (highlightField != null) { // Get the highlighted value String name = ()[0].string(); // Override non-highlighted results (name); } } ("hotelDoc = " + hotelDoc); } }
This article on SpringBoot elasticsearch query operation (RestHighLevelClient case study) of the article is introduced to this, more SpringBoot elasticsearch query operation content please search my previous posts or continue to browse the following related articles I hope that everyone! I hope you will support me more in the future!