elasticsearch 过滤器中的聚合仅返回值(如果存在于所有过滤文档中)?,elasticsearch,e-commerce,filtering,faceted-search,elasticsearch,E Commerce,Filtering,Faceted Search" /> elasticsearch 过滤器中的聚合仅返回值(如果存在于所有过滤文档中)?,elasticsearch,e-commerce,filtering,faceted-search,elasticsearch,E Commerce,Filtering,Faceted Search" />

elasticsearch 过滤器中的聚合仅返回值(如果存在于所有过滤文档中)?

elasticsearch 过滤器中的聚合仅返回值(如果存在于所有过滤文档中)?,elasticsearch,e-commerce,filtering,faceted-search,elasticsearch,E Commerce,Filtering,Faceted Search,对于电子商务过滤系统,必须在整个过滤集上计算ElasticSearch中的聚合 没有过滤器,我们得到: "filters":{ "colour":{ "red":{ "count":5 }, "blue":{ "count":4 } }, "size":{ "L":{ "count":16

对于电子商务过滤系统,必须在整个过滤集上计算ElasticSearch中的聚合

没有过滤器,我们得到:

"filters":{  
    "colour":{  
        "red":{  
            "count":5
        },
        "blue":{  
            "count":4
        }
    },
    "size":{  
        "L":{  
            "count":16
        },
        "M":{  
            "count":15
        }
    }
}
仅过滤红色,我们得到:

"filters":{  
    "colour":{  
        "red":{  
            "count":5
        },
        "blue":{  
            "count":3
        }
    },
    "size":{  
        "L":{  
            "count":1
        },
        "M":{  
            "count":1
        }
    }
}
"filters":{  
    "colour":{  
        "blue":{  
            "count":4
        },
        "red":{  
            "count":3
        }
    }
}
仅对蓝色进行过滤,我们得到:

"filters":{  
    "colour":{  
        "red":{  
            "count":5
        },
        "blue":{  
            "count":3
        }
    },
    "size":{  
        "L":{  
            "count":1
        },
        "M":{  
            "count":1
        }
    }
}
"filters":{  
    "colour":{  
        "blue":{  
            "count":4
        },
        "red":{  
            "count":3
        }
    }
}
蓝色没有尺码,没关系。但是,如果我们将两者结合起来:

"filters":{  
    "colour":{  
        "red":{  
            "count":5
        },
        "blue":{  
            "count":4
        }
    },
    "size":{  
        "L":{  
            "count":1
        },
        "M":{  
            "count":1
        }
尺码根本不应该退回,因为它与两种颜色都不匹配

这是发送给ES的内容:

{  
    "size":1000,
    "fields":[  
        "id",
        "name",
        "price",
        "colour",
        "size"
    ],
    "query":{  
        "filtered":{  
            "query":{  
                "match_all":{}
            },
            "filter":{  
                "bool":{  
                    "must":[  
                        {  
                            "term":{  
                                "categories":4838
                            }
                        },
                        {  
                            "bool":{  
                                "should":[  
                                    {  
                                        "term":{  
                                            "colour":"blue"
                                        }
                                    },
                                    {  
                                        "term":{  
                                            "colour":"red"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "aggs":{  
        "price":{  
            "stats":{  
                "field":"price"
            }
        },
        "discounted":{  
            "terms":{  
                "field":"discounted"
            }
        },
        "stock":{  
            "filter":{  
                "range":{  
                    "stock":{  
                        "gt":0
                    }
                }
            }
        },
        "colour":{  
            "terms":{  
                "field":"colour"
            }
        },
        "size":{  
            "terms":{  
                "field":"size"
            }
        }
    }
}

出什么事了?如何设置仅在与所有文档匹配的筛选子集上进行聚合?

结果与查询一致

{
  "bool": {
    "must": [{
      "term": {
        "categories": 4838
      }
    }, {
      "bool": {
        "should": [{
          "term": {
            "colour": "blue"
          }
        }, {
          "term": {
            "colour": "red"
          }
        }]
      }
    }]
  }
}
您要的是4838类的蓝色或红色物品。在对大小执行聚合时,将为数据集中的每个大小创建存储桶,其中包含类别为4838的蓝色和红色项目。这就是为什么会在结果中得到红色项目的大小

如何设置为仅对与所有文档匹配的筛选子集进行聚合

我不确定是否理解您的问题,因为您的筛选子集同时包含红色和蓝色项。您可以使用子聚合()在结果中按颜色显示大小

更新1:

如果选择了蓝色和红色,并且M仅代表蓝色产品,而L同时代表红色和蓝色,则我只想返回L

因此,您希望大小取决于颜色

"aggs": {
  "price": {
    "stats": {
      "field": "price"
    }
  },
  "discounted": {
    "terms": {
      "field": "discounted"
    }
  },
  "stock": {
    "filter": {
      "range": {
        "stock": {
          "gt": 0
        }
      }
    }
  },
  "colour": {
    "terms": {
      "field": "colour"
    },
    "aggs": {
      "size": {
        "terms": {
          "field": "size"
        }
      }
    }
  }
}
您可以使用子聚合来执行此操作()

更新2:

讨论后的最终答案,保留或查询的结果,但只在聚合中保留和查询的结果

{
  "aggs": {
    "colorsizes": {
      "filters": {
        "filters": [{
          "bool": {
            "must": [{
              "term": {
                "size": "red"
              }
            }, {
              "term": {
                "size": "blue"
              }
            }]
          }
        }]
      },
      "aggs": {
        "size": {
          "terms": {
            "field": "size"
          }
        }
      }
    }
  }
}

好吧,我明白为什么会这样——我只是不想这样。如果选择了蓝色和红色,并且M仅代表蓝色产品,而L同时代表红色和蓝色,则我只想返回L。是否可能?这取决于您的映射。颜色是多值字段吗?或者您是否已将数据反规范化?颜色是一个多值字段,是的。单个产品可以同时具有红色和蓝色变体。感谢您的响应——但这将显示红色和蓝色的产品,这是不正确的:(.您期望得到什么结果?每种颜色的大小?大小和颜色?您可以添加预期结果的json吗?