gpt4 book ai didi

elasticsearch - 在bucket_selector之上使用bucket_sort

转载 作者:行者123 更新时间:2023-12-03 02:26:55 26 4
gpt4 key购买 nike

flex 存储的数据是父子关系,其中用户是 parent ,该用户可以有n个事件记录,记录的状态为state,可以取消启动,转换中,完成

我想获取具有5个以上处于完成状态的事件的用户,为此我使用了bucket_selector,然后我想以分页的方式获取此信息。我试图在bucket_selector上使用bucket_sort,但是它抛出错误。

我的查询如下:

{
"size": 0,
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"range": {
"doc_creation_timestamp": {
"from": 1550658823000,
"to": 1585218823622,
"include_lower":false,
"include_upper": false

}
}
}
]
}
},
{
"term": {
"eventName": {
"value": "delivered"
}
}
}
]
}
}
]
}
},
"aggregations": {
"PERFORMED_DELIVERED": {
"terms": {
"field": "user-details.user_id.keyword"
},
"aggregations": {
"theTop": {
"bucket_selector": {
"buckets_path": {
"doc_count": "_count"
},
"script": {
"source": "params.doc_count>=3",
"lang": "painless"
},
"gap_policy": "skip"
}
}
}
}
}
}


得到以下结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 22,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"PERFORMED_DELIVERED": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "tyygghgf34567",
"doc_count": 7
},
{
"key": "gtredfg3456",
"doc_count": 6
},
{
"key": "ytr4453w76y",
"doc_count": 5
},
{
"key": "fsert4wwqasd",
"doc_count": 4
}
]
}
}
}



映射:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"user": {
"type": "nested"
},
"event": {
"type": "nested"
},
"user_relations": {
"type": "join",
"relations": {
"user": "event"
}
}
}
}
}

用户(父)文档:
{
"vehicle_color": "black",
"driver_type": "REGULAR",
"app_version": "11.0.2",
"activation_date": 1583298778169,
"doc_updation_timestamp": 1583298778169,
"vehicle_model": "gemini",
"mobile_no": "7643456",
"last_name": "G",
"driver_company": "DD",
"doc_type": "user",
"city_of_interest": "BANGALORE",
"middle_name": "A",
"doc_creation_timestamp": 1583298778169,
"market_name": "market",
"user_relations": {
"name": "user"
},
"user_id": "testuser1",
"onboarding_state": "YT",
"first_name": "testuser1",
"onboarding_date": 1583298778169,
"email": "mark@gmail.com",
"doc_timestamp": 1583916522000,
"status": "ACTIVE"
}

事件(子)文档:

{
"eventID": "gsffg",
"doc_updation_timestamp": 1584079904792,
"tripId": "oi898-97-ghj-gtre43-qw233e43e",
"orderId-0-id": "65432415",
"doc_creation_timestamp": 1584079904792,
"user-details": {
"last_name": "G",
"vehicle_model": "gemini",
"mobile_no": "7643456",
"doc_type": "user",
"doc_timestamp": 1583916522000,
"city_of_nterest": "BANGALORE",
"driver_type": "REGULAR",
"app_version": "11.0.2",
"user_relations": {
"name": "user"
},
"first_name": "testuser1",
"market_name": "market",
"onboarding_date": 1583298778169,
"middle_name": "A",
"activation_date": 1583298778169,
"onboarding_state": "YT",
"user_id": "testuser1",
"vehicle_color": "black",
"email": "talk@gmail.com",
"doc_creation_timestamp": 1583298778169,
"doc_updation_timestamp": 1583298778169,
"status": "ACTIVE"
},
"user_relations": {
"parent": "testuser1",
"name": "event"
},
"estDeliveryTime-0-caption": 1579068000000,
"carrier": "TESTCARRIER",
"tenantId": 7,
"tripEndDateTime": 1579158943000,
"eventName": "ACCEPTED",
"tripStartDateTime": 1579155343000
}

我想对这个结果进行分页。请帮助我。

现在,我已经使用has_child和min和max child解决了这个问题。这解决了我用子对象数量进行分页和过滤的问题。但是我担心查询的性能:
{

"query": {
"bool": {
"should": [
{
"has_child": {
"query": {
"term": {
"eventName": {
"value": "delivered",
"boost": 1
}
}
},
"type": "event",
"score_mode": "none",
"inner_hits": {
"_source": [
"tripId",
"eventName",
"tripStartDateTime"
]
},
"min_children": 1,
"max_children": 10,
"ignore_unmapped": false,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}

最佳答案

我已经使用以下方法实现了聚集的分页:

{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"range": {
"doc_creation_timestamp": {
"from": 1559977347000,
"to": 1585897347091,
"include_lower": false,
"include_upper": false,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"term": {
"eventName": {
"value": "delivered",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"range": {
"doc_creation_timestamp": {
"from": 1559977347000,
"to": 1585897347138,
"include_lower": false,
"include_upper": false,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"term": {
"eventName": {
"value": "accepted",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"group_by_user": {
"terms": {
"field": "user-details.user_id.keyword",
"size": 1000,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": {
"_key": "asc"
},
"include": {
"partition": 0,
"num_partitions": 2
}
},
"aggregations": {
"group_by_eventName": {
"terms": {
"field": "eventName.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
}

您可以在以下内容中阅读有关include的信息: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

关于elasticsearch - 在bucket_selector之上使用bucket_sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60880130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com