【发布时间】:2015-12-03 00:10:24
【问题描述】:
我在 Elastic Search 中有下一个字段映射(在 YML 中定义):
my_analyzer:
type: custom
tokenizer: keyword
filter: lowercase
products_filter:
type: "nested"
properties:
filter_name: {"type" : "string", analyzer: "my_analyzer"}
filter_value: {"type" : "string" , analyzer: "my_analyzer"}
每个文档都有很多过滤器,看起来像:
"products_filter": [
{
"filter_name": "Rahmengröße",
"filter_value": "33,5 cm"
}
,
{
"filter_name": "color",
"filter_value": "gelb"
}
,
{
"filter_name": "Rahmengröße",
"filter_value": "39,5 cm"
}
,
{
"filter_name": "Rahmengröße",
"filter_value": "45,5 cm"
}]
我试图获取每个过滤器的唯一过滤器名称列表和唯一过滤器值列表。
我的意思是,我想获得如下结构:
拉蒙格勒:
39,5 厘米
45,5 厘米
33,5 厘米
颜色:
凝胶
为了得到它,我尝试了几种聚合变体,例如:
{
"aggs": {
"bla": {
"terms": {
"field": "products_filter.filter_name"
},
"aggs": {
"bla2": {
"terms": {
"field": "products_filter.filter_value"
}
}
}
}
}
}
而且这个请求是错误的。
它将返回唯一过滤器名称的列表,每个过滤器名称都将包含所有过滤器值的列表。
"bla": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 103,
"buckets": [
{
"key": "color",
"doc_count": 9,
"bla2": {
"doc_count_error_upper_bound": 4,
"sum_other_doc_count": 366,
"buckets": [
{
"key": "100",
"doc_count": 5
}
,
{
"key": "cm",
"doc_count": 5
}
,
{
"key": "unisex",
"doc_count": 5
}
,
{
"key": "11",
"doc_count": 4
}
,
{
"key": "160",
"doc_count": 4
}
,
{
"key": "22",
"doc_count": 4
}
,
{
"key": "a",
"doc_count": 4
}
,
{
"key": "alu",
"doc_count": 4
}
,
{
"key": "aluminium",
"doc_count": 4
}
,
{
"key": "aus",
"doc_count": 4
}
]
}
}
,
另外我尝试使用反向嵌套聚合,但它对我没有帮助。
所以我认为我的尝试存在一些逻辑错误?
【问题讨论】:
-
绝对是 2 个不同的问题。在第一种情况下 - ES 带空格的行为问题,在我的问题中 - 嵌套对象的子聚合问题。
-
如果你对elasticsearch有更多的了解,那也是同样的问题。您的问题是您的文本在令牌级别进行了分析和拆分。您要么不分析文本并拥有
raw字段,要么使用keyword分析器对其进行索引。 -
我根据您显示的示例添加了分析器。结果几乎一样。我的请求中可能有一些逻辑错误吗?
-
我将尽快发布数据和查询应该是什么样子的答案。
标签: elasticsearch aggregate-functions