Nest Elastic Search中的嵌套聚合

问题描述

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

解决方法

.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的语法。添加JSON格式的工作示例。

索引映射:

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

索引数据:

{
    "RootId": 2,"CityId": 11,"RootName": "DEF","listItems": [
        {
            "Price": 60
        },{
            "Price": 90
        },{
            "Price": 80
        }
    ]
}
{
    "RootId": 1,"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,"nested_entries": {
            "doc_count": 3,"min_position": {
              "value": 60.0
            }
          }
        }
      ]
    }
  }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...