等于、日期范围、包含

sblx等于002 并且 cjsj日期从2022-01-28到2022-03-11 并且 sjgsdwdm 前包含130502,size 分页显示100条

GET /xq_jdctxxx/_search 
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "sblx": "002"
          }
        },
        {
          "range": {
            "cjsj.keyword": {
              "from": "2022-01-28",
              "to": "2022-03-11"
            }
          }
        },
        {
          "wildcard": {
            "sjgsdwdm": "130502*"
          }
        }
      ]
    }
    
  },
  "size": 100
}

等于、不等于

(sblx等于002并且 dqjd 等于空) 并且( jdchphm不等于空)

GET /xq_jdctxxx/_search 
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "sblx": "002"
          }
        },
        {
          "exists": {
            "field": "dqjd"
          }
        }
      ],
      "must_not": [
        { 
          "exists": {
            "field": "jdchphm"
          }
        }
      ]
    }
  },
  "size": 100
}

分组

group_count是别名可以任意起,分组后返回的名称就是这个
按jdchphm字段分组,分组别名设置为group_count,按jdchphm字段分组(如果字段是text类型的会报错,需要错加.keyword)

{
    "size": 0,
    "query": {
        "bool": {
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "aggregations": {
        "group_count": {
            "terms": {
                "field": "jdchphm.keyword",
                "missing": "0",
                "size": 100000,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            }
        }
    }
}

Java实例

SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = buildQuery(xqJdctxjlEs);
        builder.query(boolQueryBuilder);
        builder.size(0);
        //按jdchphm字段分组,分组别名设置为group_count,按jdchphm字段分组(如果字段是text类型的会报错,需要错加.keyword)
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("group_count").field("jdchphm.keyword");
        aggregationBuilder.size(100000);
        aggregationBuilder.missing("0");
        builder.aggregation(aggregationBuilder);

        builder.query(boolQueryBuilder);
        System.out.println(builder.toString());
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(index);
        searchRequest.source(builder);
        System.out.println(searchRequest.toString());
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        Terms termsJdctxxx = response.getAggregations().get("group_count");
        // bucket1.getKey()为分组的值,bucket1.getDocCount()为分组的数量
        for (Terms.Bucket bucket1 : termsJdctxxx.getBuckets()) {
            Map<String, Object> map = new HashMap<>();
            map.put("cph", bucket1.getKey().toString());
            map.put("sl", bucket1.getDocCount());
        }
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐