【发布时间】:2021-07-02 14:19:11
【问题描述】:
我正在使用弹性搜索来获取按类别分组的产品并对结果进行聚合...... 如果我使用 categoryid(numeric) 作为字段,它的给出结果但是当我尝试给出类别名称时,它给出 Unsuccessful(400)
请看打击码sn-p
我正在获取文档计数。我可以从同一个请求中获取文档数据吗?
ISearchResponse<Products> results;
results = _client.Search<Products>(s => s
//.Size(int.MaxValue)
.Query(q => q
.Bool(b => b
.Should(
bs => bs.Prefix(p => p.cat_name, "heli"),
bs => bs.Prefix(p => p.pr_name, "heli")
)
)
)
.Aggregations(a => a
.Terms("catname", t => t
.Field(f => f.categoryid)
.Size(int.MaxValue)
.Aggregations(agg => agg
.Max("maxprice", av => av.Field(f2 => f2.price))
.Average("avgprice", av => av.Field(f3 => f3.price))
.Max("maxweidht", av => av.Field(f2 => f2.weight))
.Average("avgweight", av => av.Field(f3 => f3.weight))
)
)
)
);
映射模型:
{
"product_catalog": {
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"cat_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"categoryid": {
"type": "long"
},
"createdon": {
"type": "date"
},
"fulldescription": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"height": {
"type": "float"
},
"length": {
"type": "float"
},
"pr_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"price": {
"type": "long"
},
"productid": {
"type": "long"
},
"shortdescription": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sku": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updatedon": {
"type": "date"
},
"weight": {
"type": "float"
},
"width": {
"type": "float"
}
}
}
}
}
谁能指导如何使用类别名称进行分组。
【问题讨论】:
-
你能分享索引映射吗?在您的情况下,字段
catname可能是文本类型,默认情况下,您不能在此类字段上执行术语 aggs,但要确保很高兴看到您的映射。 -
谢谢@Rob,我已经更新了相关映射。
标签: c# elasticsearch nest