【发布时间】:2018-01-26 00:53:52
【问题描述】:
您好,我有以下弹性产品映射 我正在尝试从产品规范中的名称/值数据创建聚合我认为我需要实现的是嵌套聚合但我正在努力实现
"mappings": {
"product": {
"properties": {
"productSpecification": {
"properties": {
"productSpecificationId": {
"type": "long"
},
"specificationId": {
"type": "long"
},
"productId": {
"type": "long"
},
"name": {
"fielddata": true,
"type": "text"
},
"value": {
"fielddata": true,
"type": "text"
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"value": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"reviewRatingCount": {
"type": "integer"
},
"productId": {
"type": "integer"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"dispatchTimeInDays": {
"type": "integer"
},
"productCode": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
我现在已经更改了下面的代码,我取得了一些成功
.Aggregations(a => a
.Terms("level1",t => t
.Field(f=> f.ProductSpecification.First().Name)
.Aggregations(snd => snd
.Terms("level2", f2 => f2.Field(f3 => f3.ProductSpecification.First().Value))
)))
通过使用此代码,我现在返回名称值
var myagg = response.Aggs.Terms("level1");
if(response.Aggregations != null)
{
rtxAggs.Clear();
rtxAggs.AppendText(Environment.NewLine);
foreach(var bucket in myagg.Buckets)
{
rtxAggs.AppendText(bucket.Key);
}
}
我想不通的是如何获得子聚合值
【问题讨论】:
-
当您希望在属于映射为
nested类型的字段的字段上进行聚合时,需要嵌套聚合。在您的示例中,属于productSpecification. 的任何字段 -
嗨@RussCam 我已更新问题以包含我的代码
-
@RussCam 进一步更新提供了一些进展
-
您可以从每个
bucket获取子聚合,例如bucket.Terms("level2")内foreach。你见过elastic.co/guide/en/elasticsearch/client/net-api/current/… 吗? -
在 Cast 你看到这个@RussCam 我正在寻找匹配我在多个字段上的查询但想要以不同方式提升字段我读到的所有内容似乎都建议使用 .OnFieldsWithBoost 但最新版本的巢似乎没有支持这个
标签: c# elasticsearch nest