An overview of the

We are used to using SQL statements to manipulate data, and we are definitely not used to using non-SQL statements in the middle of the database. Today I’m going to talk about Elasticsearch, and I’m going to forget some of the statements I’ve written, especially when I’m writing complex queries. Therefore, I spent a lot of time summarizing (including json format statements and corresponding Java API writing).

configuration

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId>  </dependency>Copy the code

Import the dependency directly and inject the RestHighLevelClient object to use

Basic increase, deletion and revision

Add the document

1.java api

/** * Add document *@param addDocDto
 * @throws IOException
 */
public void addDoc(AddDocDto addDocDto) throws IOException {

    IndexRequest indexRequest = new IndexRequest(addDocDto.getIndexName());

    indexRequest.source(addDocDto.getData());
    indexRequest.id(addDocDto.getId());
    // Create index
    IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

    log.info("Execution Result :"+JSONUtil.toJsonStr(index));
}
Copy the code
  1. Elasticsearch statement
POST test-index-20220228/_doc
{
  "sex": "w"."balance2": 320."balance": "501"."name": "liujing5"."age": 6
}
Copy the code

For inserting a document, the POST method automatically generates id

Update the document

1.java api

/** * Update document (update only by ID, which is incremental field) *@param updateDocDto
 * @throws IOException
 */
public void updateDoc3(UpdateDocDto updateDocDto) throws IOException {

    UpdateRequest indexRequest = new UpdateRequest(updateDocDto.getIndexName(),updateDocDto.getId());

    // Set the data
    indexRequest.doc(updateDocDto.getData());

    // Update the document
    UpdateResponse update = restHighLevelClient.update(indexRequest, RequestOptions.DEFAULT);

    log.info("Execution Result :"+JSONUtil.toJsonStr(update));
}
Copy the code

2.elasticsearch

POST test-index-20220228/_update/L1gtSX8BX_oAU-6DS9F3
{
  "doc": {
     "sex" : "w"."balance2" : 320."balance" : "501"."name" : "liujing5"."age" : 7}}Copy the code

Update documents according to conditions

1.java api

/** * update the document (update the data according to the condition, delete the old field) *@param updateDocDto
 * @throws IOException
 */
public void updateDoc2(UpdateDocDto updateDocDto) throws IOException {

    UpdateByQueryRequest indexRequest = new UpdateByQueryRequest(updateDocDto.getIndexName());

    // Set the update condition
    Set<String> strings = updateDocDto.getQuery().keySet();
    for (String key:strings){
        QueryBuilder queryBuilder = new TermQueryBuilder(key,updateDocDto.getQuery().get(key));
        indexRequest.setQuery(queryBuilder);
    }
    // Set the update value
    JSONObject data = updateDocDto.getData();

    // Update the value script
    String source = "ctx._source=params";
    Script script = new Script(ScriptType.INLINE, "painless", source, data);
    indexRequest.setScript(script);

    // Update the document
    BulkByScrollResponse index = restHighLevelClient.updateByQuery(indexRequest, RequestOptions.DEFAULT);

    log.info("Execution Result :"+JSONUtil.toJsonStr(index));
}
Copy the code

2.elasticsearch

POST test-index-20220228/_update_by_query
{
  "query": {
    "term": {
      "age": {
        "value" : 7."boost" : 1.0}}},"script": {
    "source":"ctx._source=params"."params": {"aaa":"50003"}}}Copy the code

Description: Update matching documents based on any criteria

Delete the document

1.java api

/** * Delete document (by id) *@param deleteDocDto
 * @throws IOException
 */
public void deleteDoc(DeleteDocDto deleteDocDto) throws IOException {

    DeleteRequest indexRequest = new DeleteRequest(deleteDocDto.getIndexName(),deleteDocDto.getId());

    // Update the document
    DeleteResponse update = restHighLevelClient.delete(indexRequest, RequestOptions.DEFAULT);

    log.info("Execution Result :"+JSONUtil.toJsonStr(update));
}
Copy the code

2.elasticsearch

DELETE test-index-20220228/_doc/L1gtSX8BX_oAU-6DS9F3
Copy the code

Delete documents according to conditions

1.java api

/** * Delete document (based on any criteria) *@param deleteDocDto
 * @throws IOException
 */
public void deleteDoc2(DeleteDocDto deleteDocDto) throws IOException {

    DeleteByQueryRequest indexRequest = new DeleteByQueryRequest(deleteDocDto.getIndexName());

    // Set search criteria
    Set<String> strings = deleteDocDto.getQuery().keySet();
    for (String key:strings){
        QueryBuilder queryBuilder = new TermQueryBuilder(key,deleteDocDto.getQuery().get(key));
        indexRequest.setQuery(queryBuilder);
        log.info(queryBuilder.toString());
    }
    // Update the document
    BulkByScrollResponse response = restHighLevelClient.deleteByQuery(indexRequest, RequestOptions.DEFAULT);

    log.info("Execution Result :"+JSONUtil.toJsonStr(response));
}
Copy the code

2.elasticsearch

POST test-index-20220228/_delete_by_query
{
  "query": {"term" : {
    "age" : {
      "value" : 6."boost" : 1.0}}}}Copy the code

Description: Delete the specified document based on the condition

Nonsegmentation based query (term)

Single-field equivalent query

Mysql select * from user where name = “mysql”

/** * Single-field query (equivalent value) * @param searchDocDto * @throws IOException */ public void Search (searchDocDto searchDocDto) throws IOException {TermQueryBuilder TermQueryBuilder = QueryBuilders. TermQuery (searchDocto.getKey (), searchDocDto.getValue()); / / query searchCommon (searchDocDto getIndexName (), termQueryBuilder); }Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "term": {
      "name": {
        "value": "liujing2"."boost": 1}}}}Copy the code

Note: Can carry out any single field equivalent query operation

Single-field multi-value query

Mysql select * from user where name in (” 三”,” 三”) mysql select * from user where name in (” 三”,” 三”

/** * Single-field query (multi-value equivalent) *@param searchDocDto
 * @throws IOException
 */
public void search2(SearchDocDto searchDocDto) throws IOException {

    // Component query
    TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(searchDocDto.getKey(), searchDocDto.getValues());
    / / query
    searchCommon(searchDocDto.getIndexName(),termsQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "terms": {
      "name": [
        "liujing3"."liujing"]."boost": 1}}}Copy the code

Note: Can carry out any single field multiple values equivalent batch query operation

Word Segmentation Based query (match)

Single-field word segmentation query

Mysql select * from user where name like “%liujjing%

/** * single field query (fuzzy - word segmentation query) *@param searchDocDto
 */
public void search11(SearchDocDto searchDocDto) throws IOException {
    // Match query
    MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(searchDocDto.getKey(), searchDocDto.getValue());

    / / query
    searchCommon(searchDocDto.getIndexName(),matchQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "match": {
      "name": {
        "query": "liujing2"."operator": "OR"."prefix_length": 0."max_expansions": 50."fuzzy_transpositions": true."lenient": false."zero_terms_query": "NONE"."auto_generate_synonyms_phrase_query": true."boost": 1}}}}Copy the code

Note: Can be any field segmentation query

Multi-field word segmentation query

Mysql select * from user where name like “%liujjing%” or… 2.java api

/** * multi-field query (fuzzy multi-field - word segmentation query) *@param searchDocDto
 */
public void search12(SearchDocDto searchDocDto) throws IOException {
    // Match query
    MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(searchDocDto.getValue(),searchDocDto.getFields());

    / / query
    searchCommon(searchDocDto.getIndexName(),multiMatchQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "multi_match": {
      "query": "liujing2 1200"."fields": [
        "The name ^ 1.0"."balance"]."type": "best_fields"."operator": "OR"."slop": 0."prefix_length": 0."max_expansions": 50."zero_terms_query": "NONE"."auto_generate_synonyms_phrase_query": true."fuzzy_transpositions": true."boost": 1}}}Copy the code

Note: Multiple arbitrary fields can be queried at the same time

Multi-field phrase segmentation query

Mysql select * from user where name like “%liujjing hello %

/** * @param searchDocDto */ public void search13(searchDocDto searchDocDto) throws IOException { / / components match the query MatchPhraseQueryBuilder MatchPhraseQueryBuilder = QueryBuilders. MatchPhraseQuery (searchDocDto. GetKey (), searchDocDto.getValue()); / / query searchCommon (searchDocDto getIndexName (), matchPhraseQueryBuilder); }Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "match_phrase": {
      "name": {
        "query": "liujing2 hello"."slop": 0."zero_terms_query": "NONE"."boost": 1}}}}Copy the code

Note: Can be any field word segmentation query

Range and paging queries

Single-field range query

Mysql select * from user where age >20 and age<30 2

/** * Single-field query (range) *@param searchDocDto
 * @throws IOException
 */
public void search3(SearchDocDto searchDocDto) throws IOException {
    // Component query
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(searchDocDto.getKey());
    rangeQueryBuilder.from(searchDocDto.getStartValue());
    rangeQueryBuilder.to(searchDocDto.getEndValue());

    / / query
    searchCommon(searchDocDto.getIndexName(),rangeQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "range": {
      "age": {
        "from": "2"."to": "19"."include_lower": true."include_upper": true."boost": 1}}}}Copy the code

Note: Batch query can be performed in any single field range

Paging query

Mysql select * from user limit 1 5

/** ** paging *@param searchDocDto
 */
public void search14(SearchDocDto searchDocDto) throws IOException {

    SearchRequest searchRequest = new SearchRequest(searchDocDto.getIndexName());
    // Build the query
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    / / paging
    searchSourceBuilder.from(1);
    searchSourceBuilder.size(2);

    searchRequest.source(searchSourceBuilder);

    log.info("Search criteria"+searchSourceBuilder.toString());

    SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    SearchHits hits = search.getHits();

    for(SearchHit searchHit:hits){ log.info(JSONUtil.toJsonStr(searchHit.getSourceAsString())); }}Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  // Query can be paginated using query
  
  "from": 1."size": 5
}
Copy the code

Note: Can be queried with conditional paging

Compound query

should

Mysql select * from user where age=18 mysql select * from user where age=18

/** ** should *@param searchDocDto
 */
public void search4(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    JSONObject query = searchDocDto.getQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.should(termQueryBuilder);
    }
    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "bool": {
      "should": [{"term": {
            "name": {
              "value": "liujing"."boost": 1}}}, {"term": {
            "age": {
              "value": 19."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Note: You can carry out multi-field OR query, as long as the conditions of a ShOUD can be met

must

Mysql select * from user and age=18 mysql select * from user and age=18

/** * (must) *@param searchDocDto
 */
public void search5(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    JSONObject query = searchDocDto.getQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.must(termQueryBuilder);
    }

    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "bool": {
      "must": [{"term": {
            "name": {
              "value": "liujing"."boost": 1}}}, {"term": {
            "age": {
              "value": 19."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Can be multi-field and query, to meet multiple conditions at the same time to return

must not

Mysql select * from user! And age! =18 2.java api

/** * compound multi-field query (must not, as opposed to must, does not count correlation scores) *@param searchDocDto
 */
public void search6(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    JSONObject query = searchDocDto.getQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.mustNot(termQueryBuilder);
    }
    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

{
  "query": {
    "bool": {
      "must_not": [{"term": {
            "name": {
              "value": "liujing"."boost": 1}}}, {"term": {
            "age": {
              "value": 19."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Note: Can carry out multi-field and query, to not meet multiple conditions at the same time to return

filter

Mysql select * from user and age=18 mysql select * from user and age=18

/** * compound multi-field query (filter, no correlation score, same as must) *@param searchDocDto
 */
public void search7(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    JSONObject query = searchDocDto.getQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.filter(termQueryBuilder);
    }
    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

{
  "query": {
    "bool": {
      "filter": [{"term": {
            "name": {
              "value": "liujing"."boost": 1}}}, {"term": {
            "age": {
              "value": 19."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Explanation: Same effect as must, only without grading

Should, must, must_not, composite filter

Mysql select * from ((user = “zhang3” and age=18) and (balance=1000 or balance=2000)

/** * query (should,must,must_not,filter) *@param searchDocDto
 */
public void search8(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    // The component should condition
    JSONObject query = searchDocDto.getShouldQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.should(termQueryBuilder);
    }
    // Component must condition
    JSONObject query1 = searchDocDto.getMustQuery();
    Set<String> strings1 = query1.keySet();

    for(String key:strings1) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query1.get(key));
        boolQueryBuilder.must(termQueryBuilder);
    }
    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

POST test-index-20220228/_search
{
  "query": {
    "bool": {
      "must": [{"term": {
            "sex": {
              "value": "w"."boost": 1}}}, {"term": {
            "name": {
              "value": "liujing2"."boost": 1}}}]."should": [{"term": {
            "balance": {
              "value": 1200."boost": 1}}}, {"term": {
            "age": {
              "value": 5."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Should,must,filter,must not these query conditions are met

Should,must,must_not,filter and bool compound

Mysql select * from ((user = “zhang3” and age=18) and (balance=1000 or balance=2000)) or((….) ) 2.java api

/** * (should,must,must_not,filter) (bool@param searchDocDto
 */
public void search9(SearchDocDto searchDocDto) throws IOException {
    // Component query
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    // The component should condition
    JSONObject query = searchDocDto.getShouldQuery();
    Set<String> strings = query.keySet();

    for(String key:strings) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query.get(key));
        boolQueryBuilder.should(termQueryBuilder);
    }
    // Internal bool
    BoolQueryBuilder boolQueryBuilder1 = QueryBuilders.boolQuery();
    JSONObject query1 = searchDocDto.getInnerBoolQuery();
    Set<String> strings1 = query1.keySet();
    for(String key:strings1) {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(key, query1.get(key));
        boolQueryBuilder1.must(termQueryBuilder);
    }

    boolQueryBuilder.should(boolQueryBuilder1);
    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

GET test-index-20220228/_search
{
  "query": {
    "bool": {
      "should": [{"term": {
            "balance": {
              "value": 1200."boost": 1}}}, {"term": {
            "age": {
              "value": 5."boost": 1}}}, {"bool": {
            "must": [{"term": {
                  "balance": {
                    "value": "50"."boost": 1}}}, {"term": {
                  "age": {
                    "value": 2."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Should,must,filter,must not bool series conditions can be nested again internally

Should,must, must_NOT,filter Can use basic query internally

Mysql select * from (name=” zhangsan “or (age>20 and age<30) or name like “% 3%”

/** * compound multi-field query (should,must,must_not,filter) (term,much,range) *@param searchDocDto
 */
public void search10(SearchDocDto searchDocDto) throws IOException {
    // Component range conditions
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(searchDocDto.getKey());
    rangeQueryBuilder.from(searchDocDto.getStartValue());
    rangeQueryBuilder.to(searchDocDto.getEndValue());

    // The component should condition
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.should(rangeQueryBuilder);

    / / query
    searchCommon(searchDocDto.getIndexName(),boolQueryBuilder);
}
Copy the code

3.elasticsearch

GET test-index-20220228/_search
{
  "query": {
    "bool": {
      "should": [{"range": {
            "name": {
              "from": "1"."to": "19"."include_lower": true."include_upper": true."boost": 1}}}]."adjust_pure_negative": true."boost": 1}}}Copy the code

Should,must,filter,must not internal can use basic query (term,match,range)

Aggregation query

Commonly used polymer

Mysql select count(*) from user where age>20 group by name,age 2

/ * * * polymerization (terms - statistics document number) = = (group (group can be nested, equivalent to more fields group) + count (*) aggregate function) * polymerization (Max) = = (Max (*) aggregate function) * polymerization (min) = = (min (*) aggregate function) * Aggregate (AVG) ==(AVG (*) aggregate function) * aggregate (sum) ==(sum(*) aggregate function) * aggregate (top_hits) == Save aggregate record *@param searchDocDto
 * @throws IOException
 */
public void search15(SearchDocDto searchDocDto) throws IOException {

    SearchRequest searchRequest = new SearchRequest(searchDocDto.getIndexName());
    // Build the query
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    / / the aggregation
    TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg");
    // Set the aggregation field
    ageAgg.field(searchDocDto.getAggField());
    // Set the number of results to display the aggregation distribution
    ageAgg.size(searchDocDto.getAggSize());
    // Set the lower limit for the number of aggregation result documents
    ageAgg.minDocCount(searchDocDto.getAggMinDocCount());
    // Set the sort
    BucketOrder count = BucketOrder.count(true);
    ageAgg.order(count);

    // Set the subaggregation
    AggregatorFactories.Builder builder = AggregatorFactories.builder();
    // Set the average aggregation function
    AvgAggregationBuilder balanceAvgAgg = AggregationBuilders.avg("balanceAvgAgg");
    balanceAvgAgg.field("balance2");
    builder.addAggregator(balanceAvgAgg);

    // Set the maximum aggregate function
    MaxAggregationBuilder maxAgg = AggregationBuilders.max("balanceMaxAgg");
    maxAgg.field("balance2");
    builder.addAggregator(maxAgg);

    // Set the minimum aggregate function
    MinAggregationBuilder minAgg = AggregationBuilders.min("balanceMinAgg");
    minAgg.field("balance2");
    builder.addAggregator(minAgg);

    // Set the aggregate function
    SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("balanceSumAgg");
    sumAggregationBuilder.field("balance2");
    builder.addAggregator(sumAggregationBuilder);

    // Set the record for each value in the aggregation
    TopHitsAggregationBuilder aggRecord = AggregationBuilders.topHits("aggRecord");
    aggRecord.size(1);
    builder.addAggregator(aggRecord);

    ageAgg.subAggregations(builder);

    searchSourceBuilder.aggregation(ageAgg);

    searchRequest.source(searchSourceBuilder);

    log.info("Search criteria"+searchSourceBuilder.toString());

    SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    // Get the aggregation result
    Aggregations aggregations = search.getAggregations();

    for (Aggregation aggr:aggregations.asList()){
        ParsedLongTerms parsedLongTerms=(ParsedLongTerms)aggr;
        List<? extends Terms.Bucket> buckets = parsedLongTerms.getBuckets();
        for(Terms.Bucket bucket:buckets){ log.info(JSONUtil.toJsonStr(bucket)); }}}Copy the code

3.elasticsearch

GET test-index-20220228/_search
{
  "aggregations": {
    "ageAgg": {
      "terms": {
        "field": "age"."size": 10."min_doc_count": 1."shard_min_doc_count": 0."show_term_doc_count_error": false."order": [{"_count": "asc"
          },
          {
            "_key": "asc"}},"aggregations": {
        "balanceAvgAgg": {
          "avg": {
            "field": "balance2"}},"balanceMaxAgg": {
          "max": {
            "field": "balance2"}},"balanceMinAgg": {
          "min": {
            "field": "balance2"}},"balanceSumAgg": {
          "sum": {
            "field": "balance2"}},"aggRecord": {
          "top_hits": {
            "from": 0."size": 1."version": false."seq_no_primary_term": false."explain": false
          }
        }
      }
    }
  }
}
Copy the code

Note: 1. Term aggregation type equivalent to group by can be used to distribute data, of course, can be nested term, equivalent to grouping according to multiple fields

2. Term The aggregation result after aggregation will have a basic number of documents

3. Max,min,sum, AVg are equivalent to some computational aggregation functions

Common Polymerization 2(Polymerization condition filtering)

Mysql select count(*) count from user where age>20 group by name,age having count>1

/** * aggregate (bucket_selector) ==(having in) aggregate function) *@param searchDocDto
 * @throws IOException
 */
public void search17(SearchDocDto searchDocDto) throws IOException {

    SearchRequest searchRequest = new SearchRequest(searchDocDto.getIndexName());
    // Build the query
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // term aggregation
    TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg");
    ageAgg.field(searchDocDto.getAggField());
    searchSourceBuilder.aggregation(ageAgg);

    // Set the subaggregation
    AggregatorFactories.Builder builder = AggregatorFactories.builder();
    / / set bucket_selector
    Map<String, String> bucketsPath = new HashMap<>();
    bucketsPath.put("count"."_count");
    BucketSelectorPipelineAggregationBuilder pipelineAggregationBuilder = new BucketSelectorPipelineAggregationBuilder
            ("filterAgg",bucketsPath, Script.parse("params.count>1"));
    builder.addPipelineAggregator(pipelineAggregationBuilder);

    ageAgg.subAggregations(builder);

    searchRequest.source(searchSourceBuilder);

    log.info("Search criteria"+searchSourceBuilder.toString());

    SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    Aggregations aggregations = search.getAggregations();

    for (Aggregation aggr:aggregations.asList()){
        ParsedLongTerms parsedLongTerms=(ParsedLongTerms)aggr;
        List<? extends Terms.Bucket> buckets = parsedLongTerms.getBuckets();
        for(Terms.Bucket bucket:buckets){ log.info(JSONUtil.toJsonStr(bucket)); }}}Copy the code

3.elasticsearch

GET test-index-20220228/_search
{
  "aggregations": {
    "ageAgg": {
      "terms": {
        "field": "age"."size": 10."min_doc_count": 1."shard_min_doc_count": 0."show_term_doc_count_error": false."order": [{"_count": "desc"
          },
          {
            "_key": "asc"}},"aggregations": {
        "filterAgg": {
          "bucket_selector": {
            "buckets_path": {
              "count": "_count"
            },
            "script": {
              "source": "params.count>1"."lang": "painless"
            },
            "gap_policy": "skip"
          }
        }
      }
    }
  }
}
Copy the code

Note: Bucket_selector type aggregation can be used to conditionally filter the upper aggregation bucket

Grading (follow-up)

conclusion

Should,must,must not filter; 3. Should,must,must not filter can combine bool internally to achieve more complex compound query 4. Aggregations Implement grouping and aggregation function calculation