首页 > 解决方案 > 使用 Elasticsearch 进行分面导航的最佳方式是什么?

问题描述

我正在尝试使用 Elasticsearch 实现分面导航,并且我遇到了使用带有过滤器的聚合来实现它的方法。

我正在使用的 ES 版本:7.10.2

https://medium.com/swlh/faceted-navigation-for-e-commerce-with-elasticsearch-dbf82bdeb7d4

问题:在上述文章中提供的方法中,

我们需要为使用除自身之外的所有其他过滤器过滤的每个方面创建一个特定的聚合

例如:我在 4 个方面进行聚合,如颜色、尺寸、品牌和风格,如果我为品牌应用了过滤器:“Prada”和风格:“Evening”,那么查询如下:

{
  "query": {
    "match": {
      "Category": "Dress"
    }
  },
  "aggs": {
    "Color": {
      "aggs": {
        "Color": {
          "terms": {
            "field": "Color",
            "size": 10
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "terms": {
                "Brand": [
                  "Prada"
                ]
              }
            },
            {
              "terms": {
                "Style": [
                  "Evening"
                ]
              }
            }
          ]
        }
      }
    },
    "Size": {
      "aggs": {
        "Size": {
          "terms": {
            "field": "Size",
            "size": 5
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "terms": {
                "Brand": [
                  "Prada"
                ]
              }
            },
            {
              "terms": {
                "Style": [
                  "Evening"
                ]
              }
            }
          ]
        }
      }
    },
    "Brand": {
      "aggs": {
        "Brand": {
          "terms": {
            "field": "Brand",
            "size": 10
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "terms": {
                "Style": [
                  "Evening"
                ]
              }
            }
          ]
        }
      }
    },
    "Style": {
      "aggs": {
        "Style": {
          "terms": {
            "field": "Style",
            "size": 10
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "terms": {
                "Brand": [
                  "Prada"
                ]
              }
            }
          ]
        }
      }
    }
  },
  "post_filter": {
    "bool": {
      "filter": [
        {
          "terms": {
            "Brand": [
              "Prada"
            ]
          }
        },
        {
          "terms": {
            "Style": [
              "Evening"
            ]
          }
        }
      ]
    }
  }
}

但是在上面的查询中,您可以看到每个聚合都应用了过滤器,这很好,因为我现在只有 2 个过滤器,但是当我有 10 个或更多过滤器时,这个查询会变得非常大,因为每个聚合将应用 10 个过滤器。

有没有办法优化这个?或任何其他比这更好的方法?

任何帮助深表感谢:)

标签: elasticsearchelastic-stack

解决方案


推荐阅读