Es 聚合查询

mapping如下:

        

{
    "Info": {
        "aliases": {},
        "mappings": {
            "_doc": {
                "properties": {
                    "business_code": {
                        "type": "keyword"
                    },
                    
                    "create_datetime": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss"
                    },
                    
                    
                    "display_item_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "display_plan_id": {
                        "type": "long"
                    },
                    
                    "emp_role": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 255
                            }
                        }
                    },
                    
                    "id": {
                        "type": "long"
                    },
                    "is_pay": {
                        "type": "boolean"
                    },
                    
                    "info_exts": {
                        "type": "nested",
                        "properties": {
                            "create_date_time": {
                                "type": "long"
                            },
                            "create_user": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "deleted": {
                                "type": "long"
                            },
                            "emp_org_code": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "id": {
                                "type": "long"
                            },
                            "is_pay": {
                                "type": "boolean"
                            },
                            "item_id": {
                                "type": "keyword"
                            },
                            "item_name": {
                                "type": "keyword"
                            },
                            "photo_info_id": {
                                "type": "long"
                            },
                            "photo_type": {
                                "type": "keyword"
                            },
                            "type": {
                                "type": "long"
                            },
                            "update_date_time": {
                                "type": "long"
                            },
                            "update_user": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    },
                    
                 
                    "photo_type": {
                        "type": "keyword"
                    },
                    "version": {
                        "type": "integer"
                    },
                    "visit_id": {
                        "type": "long"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1644478887975",
                "number_of_shards": "3",
                "number_of_replicas": "1",
                "uuid": "OTvdVB8hTtGdmW3KG1hKKA",
                "version": {
                    "created": "6020499"
                },
                "provided_name": "Info"
            }
        }
    }
}

需求:时间区间范围内,photo_type 与info_exts.photo_type 两者其中一个符合某值,当info_exts下面的photo_type 为 某值时,查询出info_exts下面的photo_type和item_name,注意,不要取出其他数据。

查询语句:

{
    "timeout":"60s",
    "query":{
        "bool":{
            "must":[
                
                {
                    "bool":{
                        "should":[
                            {
                                "terms":{
                                    "photo_type ":[
                                        "YSL"
                                    ],
                                    "boost":1
                                }
                            },
                            {
                                "nested":{
                                    "query":{
                                        "terms":{
                                            "info_exts.photo_type ":[
                                                "YSL"
                                            ],
                                            "boost":1
                                        }
                                    },
                                    "path":"info_exts",
                                    "ignore_unmapped":false,
                                    "score_mode":"none",
                                    "boost":1
                                }
                            }
                        ],
                        "adjust_pure_negative":true,
                        "boost":1
                    }
                },
                {
                    "range":{
                        "create_datetime":{
                            "from":"2022-02-18 00:00:00",
                            "to":null,
                            "include_lower":true,
                            "include_upper":true,
                            "boost":1
                        }
                    }
                },
                {
                    "range":{
                        "create_datetime":{
                            "from":null,
                            "to":"2022-02-19 00:00:00",
                            "include_lower":true,
                            "include_upper":true,
                            "boost":1
                        }
                    }
                }
            ],
            "adjust_pure_negative":true,
            "boost":1
        }
    },
    "_source":{
        "includes":[
            "info_exts.photo_type ",
            "info_exts.item_name"
        ],
        "excludes":[

        ]
    },
    "version":true,
    "aggregations":{
        "info_exts_nested":{
            "nested":{
                "path":"info_exts"
            },
            "aggregations":{
                "nested_ip_type":{
                    "filter":{
                        "term":{
                            "info_exts.photo_type ":"YSL"
                        }
                    },
                    "aggs":{
                        "by_display_name":{
                            "terms":{
                                "field":"info_exts.item_name",
                                "size":100,
                                "min_doc_count":1,
                                "shard_min_doc_count":0,
                                "show_term_doc_count_error":false,
                                "order":[
                                    {
                                        "_count":"desc"
                                    },
                                    {
                                        "_key":"asc"
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

取值API(查询是用“nested”,取值则用ParsedNested接受参数。用“filter”过滤,取值则用ParsedFilter接收值,最后的水桶聚合,取值则用“ParsedStringTerms”接受。):

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

        Aggregations aggregations = searchResponse.getAggregations();
        ParsedNested parsedNested = aggregations.get("photo_info_exts_nested");

        if(parsedNested != null && parsedNested.getAggregations().get("nested_ip_type") !=null){
            ParsedFilter filterValue = parsedNested.getAggregations().get("nested_ip_type");
            Aggregations aggFilter = filterValue.getAggregations();
            if(aggFilter != null && aggFilter.get("by_display_item_name") != null){
                ParsedStringTerms byDisplayItemName =  aggFilter.get("by_display_item_name");
                return Optional.ofNullable(byDisplayItemName.getBuckets())
                        .orElse(Lists.newArrayList()).stream()
                        .map(Terms.Bucket::getKeyAsString)
                        .sorted((s1, s2) -> Collator.getInstance(Locale.CHINA).compare(s1, s2)) // 中文排序
                        .collect(Collectors.toList());
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值