4. 聚合查询
假设存在一个order索引,存储了每一笔布料销售订单,里面包含了布料颜色字段color
4.1根据color进行分组统计数量
GET /order/_search { "size" : 0, // 设置size=0的意思就是,仅返回聚合查询结果,不返回普通query查询结果。 "aggs" : { // 聚合查询语句的简写 "popular_colors" : { // 给聚合查询取个名字,叫popular_colors "terms" : { // 聚合类型为,terms,terms是桶聚合的一种,类似SQL的group by的作用,根据字段分组,相同字段值的文档分为一组。 "field" : "color" // terms聚合类型的参数,这里需要设置分组的字段为color,根据color分组 } } } }
4.2查询满足某一条件的聚合数量
GET /order/_search { "size" : 0, // 设置size=0的意思就是,仅返回聚合查询结果,不返回普通query查询结果。 "aggs" : { // 聚合查询语句的简写 "popular_colors" : { // 给聚合查询取个名字,叫popular_colors "terms" : { // 聚合类型为,terms,terms是桶聚合的一种,类似SQL的group by的作用,根据字段分组,相同字段值的文档分为一组。 "field" : "color" // terms聚合类型的参数,这里需要设置分组的字段为color,根据color分组 } } } "query": { "term": { "color": "red" //查询颜色是red的订单数量 } }
4.3多分组聚合查询
比如需要先按A进行分组,分组后在按B进行分组,再按C进行分组,最后进行统计项
POST /index_name/_search { "size":0, "query":{ "bool":{ "must":[ { "term":{ "storeId":{//精确查询条件字段 "value":"123458234",//对应的查询条件value "boost":1 } } }, { "range":{//范围条件查询 "startTime":{ //from大于 to小于 "from":null, "to":"2021-09-09", "include_lower":true, "include_upper":false, "boost":1 } } }, { "multi_match":{//分词模糊查询 "query":"超人",//查询条件value "fields":[ "liveNameText",//某一字段符合 "talentNameText"//某一字段符合 ], "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 } } ], "adjust_pure_negative":true, "boost":1 } }, "aggregations":{ "liveStartTime":{ "terms":{ "field":"liveStartTime",//第一分组条件 "size":10, "min_doc_count":1, "shard_min_doc_count":0, "show_term_doc_count_error":false, "order":[ { "_count":"desc" }, { "_key":"asc" } ] }, "aggregations":{ "talentId":{ "terms":{ "field":"talentId",//第二分组条件 "size":10, "min_doc_count":1, "shard_min_doc_count":0, "show_term_doc_count_error":false, "order":[ { "_count":"desc" }, { "_key":"asc" } ] }, "aggregations":{ "talentName":{ "terms":{ "field":"talentName",//第三分组 "size":10, "min_doc_count":1, "shard_min_doc_count":0, "show_term_doc_count_error":false, "order":[ { "_count":"desc" }, { "_key":"asc" } ] }, "aggregations":{ "liveId":{ "terms":{ "field":"liveId",//第四 "size":10, "min_doc_count":1, "shard_min_doc_count":0, "show_term_doc_count_error":false, "order":[ { "_count":"desc" }, { "_key":"asc" } ] }, "aggregations":{ "liveName":{ "terms":{ "field":"liveName",//第五 "size":10, "min_doc_count":1, "shard_min_doc_count":0, "show_term_doc_count_error":false, "order":[ { "_count":"desc" }, { "_key":"asc" } ] }, "aggregations":{ "spuCount":{ "value_count":{ "field":"spuNo"//统计数量 } }, "doudianGoodsIdCount":{ "value_count":{ "field":"doudianGoodsId"//如上 } } } } } } } } } } } } } }
5. 查询text字段分词后的情况
GET /{your indexName}/_doc/{your search id}/_termvectors?fields={your field}
6. 删除索引
DELETE /indexName
7. 删除某索引下的所有数据
POST indexName/_delete_by_query
{
"query": {"match_all": {}}
}