看,灰机...

Nest 分组数据问题 c#

Elasticsearch | 作者 fztalj | 发布于2021年09月08日 | 阅读数:1560

程序代码:
var result = await _clientService.Client.SearchAsync<CarsModel>(s => s.Index("cars")
.From(0)
.Aggregations(ag => ag
.Terms("vendorID_group", t => t.Field(f => f.price))//分组
.ValueCount("Count", vc => vc.Field(fd => fd.price))//总数
.Sum("vendorPrice_Sum", su => su.Field(fd => fd.price))//求和
.Max("vendorPrice_Max", m => m.Field(fd => fd.price))//最大值
.Min("vendorPrice_Min", m => m.Field(fd => fd.price))//最小值
.Average("vendorPrice_Avg", avg => avg.Field(fd => fd.price))//平均值

)
);


聚合后的“vendorID_group”字段Items项为空数组
返回数据如下:
{
"Aggregations":{
"vendorID_group":{
"AfterKey":null,
"BgCount":0,
"DocCount":0,
"DocCountErrorUpperBound":0,
"Items":[
{

},
{

},
{

},
{

},
{

},
{

},
{

}
],
"Meta":null,
"SumOtherDocCount":0,
"Interval":null,
"AutoInterval":null
},
"vendorPrice_Avg":{
"Value":26500,
"ValueAsString":null,
"Meta":null
},
"vendorPrice_Min":{
"Value":10000,
"ValueAsString":null,
"Meta":null
},
"vendorPrice_Max":{
"Value":80000,
"ValueAsString":null,
"Meta":null
},
"vendorPrice_Sum":{
"Value":212000,
"ValueAsString":null,
"Meta":null
},
"Count":{
"Value":8,
"ValueAsString":null,
"Meta":null
}
},
"Clusters":null,
"HitsMetadata":{
"Hits":[
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"n9JEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":10000,
"color":"red",
"make":"honda",
"sold":"2014-10-28T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"oNJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":20000,
"color":"red",
"make":"honda",
"sold":"2014-11-05T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"odJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":30000,
"color":"green",
"make":"ford",
"sold":"2014-05-18T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"otJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":15000,
"color":"blue",
"make":"toyota",
"sold":"2014-07-02T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"o9JEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":12000,
"color":"green",
"make":"toyota",
"sold":"2014-08-19T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"pNJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":20000,
"color":"red",
"make":"honda",
"sold":"2014-11-05T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"pdJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":80000,
"color":"red",
"make":"bmw",
"sold":"2014-01-01T00:00:00"
},
"Type":"transactions",
"Version":0
},
{
"Explanation":null,
"Fields":null,
"Highlight":{

},
"Id":"ptJEWXsBnk5zco8MXLZ9",
"Index":"cars",
"InnerHits":{

},
"MatchedQueries":[

],
"Nested":null,
"PrimaryTerm":null,
"Routing":null,
"Score":1,
"SequenceNumber":null,
"Sorts":[

],
"Source":{
"price":25000,
"color":"blue",
"make":"ford",
"sold":"2014-02-12T00:00:00"
},
"Type":"transactions",
"Version":0
}
],
"MaxScore":1,
"Total":{
"Relation":0,
"Value":8
}
},
"NumberOfReducePhases":0,
"PointInTimeId":null,
"Profile":null,
"ScrollId":null,
"Shards":{
"Failed":0,
"Failures":[

],
"Successful":1,
"Total":1
},
"Suggest":{

},
"TerminatedEarly":false,
"TimedOut":false,
"Took":3,
"ApiCall":{
"AuditTrail":[
{
"Event":17,
"Node":null,
"Path":null,
"Ended":"2021-09-08T07:28:08.4270284Z",
"Started":"2021-09-08T07:28:08.2163354Z",
"Exception":null
},
{
"Event":18,
"Node":{
"ClientNode":false,
"DeadUntil":"0001-01-01T00:00:00",
"FailedAttempts":0,
"HoldsData":true,
"HttpEnabled":true,
"Id":null,
"IngestEnabled":false,
"IsAlive":true,
"IsResurrected":false,
"MasterEligible":true,
"MasterOnlyNode":false,
"Name":null,
"Settings":{

},
"Uri":""
},
"Path":"",
"Ended":"2021-09-08T07:28:08.4267075Z",
"Started":"2021-09-08T07:28:08.2219095Z",
"Exception":null
},
{
"Event":10,
"Node":{
"ClientNode":false,
"DeadUntil":"0001-01-01T00:00:00",
"FailedAttempts":0,
"HoldsData":true,
"HttpEnabled":true,
"Id":null,
"IngestEnabled":false,
"IsAlive":true,
"IsResurrected":false,
"MasterEligible":true,
"MasterOnlyNode":false,
"Name":null,
"Settings":{

},
"Uri":""
},
"Path":"cars/_search?typed_keys=true",
"Ended":"2021-09-08T07:28:08.5718968Z",
"Started":"2021-09-08T07:28:08.4345123Z",
"Exception":null
}
],
"ThreadPoolStats":null,
"TcpStats":null,
"DebugInformation":"",
"DeprecationWarnings":[

],
"ProductName":null,
"HttpMethod":1,
"HttpStatusCode":200,
"OriginalException":null,
"RequestBodyInBytes":null,
"ResponseBodyInBytes":null,
"ResponseMimeType":"application/json; charset=UTF-8",
"Success":true,
"SuccessOrKnownError":true,
"Uri":"",
"BuildDebugInformationPrefix":null,
"ConnectionConfiguration":{

}
}
}
已邀请:

要回复问题请先登录注册