Es condition query

QueryBuilders.termQueryThe method can be used to construct a term query. Its basic usage is as follows:

QueryBuilder queryBuilder = QueryBuilders.termQuery("field", "value");

Among them, the first parameter represents the name of the field to be queried, and the second parameter represents the value to be queried.

If you want to query multiple conditions, you can useBoolQueryBuilderto combine multipleTermQueryBuilder, as shown below:

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.termQuery("field1", "value1"))
        .must(QueryBuilders.termQuery("field2", "value2"));

The above code will construct a bool query, which contains two term queries, respectively querying field1 for value1 and field2Document data forvalue2. In bool query, use must to indicate that the query conditions must be met at the same time. You can also use should to indicate that any one condition is met. You can also use < a i=7> means the condition must not be met. mustNot

rangeQueries can be used to query values ​​within a certain range. Its basic usage is as follows:

QueryBuilder queryBuilder = QueryBuilders.rangeQuery("field").from("value1").to("value2");

Among them, the first parameter represents the name of the field to be queried, from represents the starting value, and to represents the end value. Here value1 and value2 can be any value, including numerical values, dates, strings, etc. If you only want to query a value greater than or equal to a certain value or less than or equal to a certain value, you can use the gte and lte methods, as shown below:

QueryBuilder queryBuilder = QueryBuilders.rangeQuery("field").gte("value1").lte("value2");

Heregte means greater than or equal tovalue1, and lte means less than or equal tovalue2 .

If you want to query documents within a certain value range, you can use the includeLower and includeUpper methods, as follows:

QueryBuilder queryBuilder = QueryBuilders.rangeQuery("field").from("value1").to("value2").includeLower(true).includeUpper(false);

HereincludeLower(true) means that it includes the lower bound (that is, greater than or equal tovalue1), and includeUpper(false) means that it does not include the upper bound (that is, it is greater than or equal to ). less thanvalue2).

matchQuery is used to perform text matching query on a certain field. It can perform word segmentation, compare the query string and the segmented text, and return the corresponding document if they match.

The following is a simplematchquery example:

QueryBuilder queryBuilder = QueryBuilders.matchQuery("field", "query_string");

Among them, the first parameter represents the field name to be queried, and the second parameter represents the string to be queried. By default, match queries use OR logic, that is, if any term in the query string matches one or more terms in the document, the document will be matched successfully. . If you wish to use AND logic, you can use the operator method as follows:

QueryBuilder queryBuilder = QueryBuilders.matchQuery("field", "query_string").operator(Operator.AND);

In the above example, Operator.AND is used to specify that the matching condition is AND logic, that is, only when all terms in the query string match terms in the document, The document will be matched successfully.

matchThe query also supports fuzzy matching function. You can use thefuzziness method to set the similarity threshold, as follows:

QueryBuilder queryBuilder = QueryBuilders.matchQuery("field", "query_string").fuzziness(Fuzziness.AUTO);

HereFuzziness.AUTO means using the automatically calculated similarity threshold.

prefixThe query is used to query the prefix matching of a certain field. For example, if you want to query all documents starting with a certain word, you can use the prefix query.

The following is a simpleprefixquery example:

QueryBuilder queryBuilder = QueryBuilders.prefixQuery("field", "prefix_string");

Among them, the first parameter represents the field name to be queried, and the second parameter represents the prefix string to be queried.

prefix query supports a variety of parameter settings. For example, you can use the boost method to set query weight, and the rewrite method to set query rewrite rules. In addition, it should be noted that since the query is not segmented, the query string should be a complete word or phrase, not part of a term. prefix

SearchHits<Product> shopping = elasticsearchTemplate.search(new NativeSearchQueryBuilder().withQuery(构造器名称).build(), Product.class, IndexCoordinates.of("shopping"));
List<Product> collect = shopping.stream().map(SearchHit::getContent).collect(Collectors.toList());

Highlight query

//Set the query condition matchQuery to use the default word segmenter
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);

//Set highlighting
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("title");
highlightBuilder.preTags("<em>"); // Prefix of highlighted tags
highlightBuilder.postTags("</em>"); // Highlight tag suffix

// perform search
SearchHits<Product> searchHits = elasticsearchTemplate.search(
        new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withHighlightBuilder(highlightBuilder)
                .build(),
        Product.class,
        IndexCoordinates.of("shopping")
);

// process result
List<Product> products = new ArrayList<>();
for (SearchHit<Product> searchHit : searchHits) {
    Product product = searchHit.getContent();
    // Get the highlighted field
    Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
    if (highlightFields.containsKey("title")) {
        List<String> titleHighlights = highlightFields.get("title");
        if (!titleHighlights.isEmpty()) {
            String titleHighlight = titleHighlights.get(0);
            product.setTitle(titleHighlight);
        }
    }
    products.add(product);
}

Guess you like

Origin blog.csdn.net/qq_56921846/article/details/134803287