ElasticSearch – Query documents based on JavaRestClient (match, exact, compound query, as well as sorting, paging, highlighting)

Table of Contents

1. Query documents based on JavaRestClient

1.1. Query API demo

1.1.1. Basic query framework

Corresponding format for DSL requests

Parsing the response

1.1.2. Full text search query

1.1.3. Accurate query

1.1.4. Compound query

1.1.5. Sorting and paging

1.1.6, Highlight


1. Query documents based on JavaRestClient

1.1. Query API demo

1.1.1, basic query framework

Next, a match_all query is used to demonstrate the following basic API.

 @Test
    public void testMatchAll() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.matchAllQuery());
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

    /**
     * Process response
     * @param response
     */
    private void handlerResponse(SearchResponse response) {
        //1. Analysis results
        SearchHits hits = response.getHits();
        //Get the total number of items
        long total = hits.getTotalHits().value;
        SearchHit[] hits1 = hits.getHits();
        for(SearchHit searchHit : hits1) {
            //Get source
            String json = searchHit.getSourceAsString();
            System.out.println(json);
        }
    }

It can be seen from the above that the basic steps of query are as follows:

  1. Create a SeaechRequest object and specify the index library.
  2. Request.source() prepares parameters, which is DSL.
    1. Build query conditions through QueryBuilders.
    2. Pass in the query() method of Request.source() to construct a complete query.
  3. Send a request and get the result.
  4. Analysis results (from outside to inside, layer by layer).
Corresponding format of DSL request

The construction of DSL statements is implemented through the Resource in HighLevelRestClient, which includes all functions such as query, sorting, paging, and highlighting.

Among them, query means query, and its query conditions are provided by the tool class of QueryBuilders, which includes various query methods.

Response parsing

Here is the response analysis. You can compare the query results on Kibana to see the API calling relationship.

The match and multi_match queries of full-text search are basically the same as the API called by match_all demonstrated earlier. The difference is the query condition, that is, the query part (the conditions built through QueryBuilders are different).

 @Test
    public void testMatch() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.matchQuery("brand", "Home Inn"));
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

The same is true for multi_match, but it can support multiple parameter queries.

 @Test
    public void testMultiMatch() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.multiMatchQuery("Home Inn", "brand", "name"));
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

operation result:

1.1.3, precise query

Common precision queries include term query and range query, which are also implemented using QueryBuilders.

 @Test
    public void testTerm() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.termQuery("city", "Shanghai"));
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

The same goes for range queries.

 @Test
    public void testRange() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(200)); //Chain call
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

1.1.4, compound query

A Boolean query is a combination of one or more query clauses. Subqueries can be combined in the following ways:

  • must: query condition that must match, similar to “and”.
  • should: Query condition for selective matching, similar to “or”.
  • must_not: must not match and does not participate in scoring, similar to “not”.
  • filter: must match, does not participate in scoring

RestAPI also provides the BoolQueryBuilder conditional construction method to add the above conditions.

 @Test
    public void testBoolQuery() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery();
        booleanQuery.must(QueryBuilders.termQuery("city", "Shanghai"));
        booleanQuery.filter(QueryBuilders.rangeQuery("price").lte("200"));
        request.source().query(booleanQuery); //Chain call
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

1.1.5, Sorting and Paging

Sorting and paging of search results are parameters of the same level as query, and the corresponding API is as follows.

 @Test
    public void testFromSize() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.matchAllQuery());
        //Paging offset=20 size=10
        request.source().from(20).size(10);
        //sort in descending order
        request.source().sort("price", SortOrder.DESC);
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

1.1.6, highlight

The highlighted API includes two parts: request construction DSL statement and result parsing.

The request is constructed as follows:

 @Test
    public void testHighLighter() throws IOException {
        //1. Prepare SearchRequest
        SearchRequest request = new SearchRequest("hotel");
        //2. Prepare parameters
        request.source().query(QueryBuilders.matchQuery("brand", "Home Inn"));
        request.source().highlighter(new HighlightBuilder()
                .field("name")
                .requireFieldMatch(false));
        //3. Send a request and receive a response
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4. Parse the response
        handlerResponse(response);
    }

The response is parsed as follows:

 private void handlerResponse(SearchResponse response) throws JsonProcessingException {
        //1. Analysis results
        SearchHits hits = response.getHits();
        //Get the total number of items
        long total = hits.getTotalHits().value;
        SearchHit[] hits1 = hits.getHits();
        for(SearchHit searchHit : hits1) {
            //Get source
            String json = searchHit.getSourceAsString();
            System.out.println(json);
            //2. Process highlighting
            //Get highlight
            Map<String, HighlightField> highlightFieldMap = searchHit.getHighlightFields();
            if(!CollectionUtils.isEmpty(highlightFieldMap)) {
                //Get the value of the highlighted field
                HighlightField highlightField = highlightFieldMap.get("name");
                if(highlightField != null) {
                    //Take out the first one in the highlighted result array, here is the hotel name
                    String name = highlightField.getFragments()[0].string();
                    //Processing of highlighted fields (printed here for demonstration)
                    System.out.println(name);
                }
            }
        }
    }

After running, you can see the “highlighted” fields printed through sout (finally transmitted to the front end, allowing the front end to process the highlighting. The back end only marks which fields need to be highlighted)