elasticsearch - Nest Elastic Search 中的嵌套聚合

标签 elasticsearch nest

在我的 Elastic 文档中,我有 CityId、RootId、RootName、Price。现在我必须在具有以下条件的城市中找到前 7 个根。

Name and id of root which has minimum price in a City.
top 7 roots:- roots those have max number of entry in a City.

例如:-

CityId RootId RootName Price
11      1       ABC      90
11      1       ABC      100
11      2       DEF      80
11      2       DEF      90
11      2       DEF      60

CityId =11 的答案:-

RootId RootName Price
2       DEF      60
1       ABC      90

最佳答案

我不知道 Nest 的语法。添加 JSON 格式的工作示例。

索引映射:

{
  "mappings":{
    "properties":{
      "listItems":{
        "type":"nested"
      }
    }
  }
}

指数数据:

{
    "RootId": 2,
    "CityId": 11,
    "RootName": "DEF",
    "listItems": [
        {
            "Price": 60
        },
        {
            "Price": 90
        },
        {
            "Price": 80
        }
    ]
}
{
    "RootId": 1,
    "CityId": 11,
    "RootName": "ABC",
    "listItems": [
        {
            "Price": 100
        },
        {
            "Price": 90
        }
    ]
}

搜索查询:

{
    "size": 0,
    "aggs": {
        "id_terms": {
            "terms": {
                "field": "RootId"
            },
            
            "aggs": {
                "nested_entries": {
                    "nested": {
                        "path": "listItems"
                    },
                    "aggs": {
                        "min_position": {
                            "min": {
                                "field": "listItems.Price"
                            }
                        }
                    }
                }
            }
        }
    }
}

搜索结果:

"aggregations": {
    "id_terms": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1,
          "doc_count": 1,
          "nested_entries": {
            "doc_count": 2,
            "min_position": {
              "value": 90.0
            }
          }
        },
        {
          "key": 2,
          "doc_count": 1,
          "nested_entries": {
            "doc_count": 3,
            "min_position": {
              "value": 60.0
            }
          }
        }
      ]
    }
  }

关于elasticsearch - Nest Elastic Search 中的嵌套聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64131059/

相关文章:

lucene - ElasticSearch 构面计数与总项目不匹配

elasticsearch - 使用Elasticsearch和NEST从ID返回五个以下文档

c# - 使用NEST和C#进行 Elasticsearch 滚动

elasticsearch - 索引清理和保留弹性云

elasticsearch - 在 Elasticsearch 中选择一行

elasticsearch - kibana未显示数据或任何可视化图

elasticsearch - Elasticsearch NEST API 7.8西类牙语分析器

elasticsearch - ES-基于存储桶属性中的值而不是文档值的子存储桶

c# - Elasticsearch Nest 动态聚合

elasticsearch - 嵌套和 Elasticsearch - 更新嵌套对象的映射?