【问题标题】:Nested Aggregation in Nest Elastic SearchNest Elastic Search 中的嵌套聚合
【发布时间】:2020-09-30 04:47:43
【问题描述】:

在我的 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

【问题讨论】:

  • 您有机会查看我的回答吗?期待您的反馈:)
  • 谢谢!!我已经发布了 Sol。

标签: elasticsearch nest


【解决方案1】:
.Query(query => query.Bool(bQuery => bQuery.Filter(
                    fQuery => fQuery.Terms(ter => ter.Field(f => f.CityId).Terms(cityId))
                )))
                .Aggregations(agg => agg.Terms("group_by_rootId", st => st.Field(o => o.RootId)
                        .Order(TermsOrder.CountDescending)
                        .Aggregations(childAgg => childAgg.Min("min_price_in_group", m =>m.Field(p=>p.Price))
                                .TopHits("stocks", t11 => t11
                                .Source(sfd => sfd.Includes(fd => fd.Fields(Constants.IncludedFieldsFromElastic)))
                                .Size(1)
                            )
                        )
                    )
                )
                .Size(_popularStocksCount)
                .From(0)
                .Take(0);

【讨论】:

  • 很高兴您能够在 Nest 中转换我的搜索查询?如果这对您有用请不要忘记投票并接受我的回答?
【解决方案2】:

我不知道 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
            }
          }
        }
      ]
    }
  }

【讨论】:

  • @Arvind 感谢您接受我的回答,如果您也可以为我的回答投票,那就太好了?+1 为您的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 2016-02-10
  • 2018-01-14
  • 1970-01-01
相关资源
最近更新 更多