【问题标题】:ElasticSearch - how to exclude filter from aggregations?ElasticSearch - 如何从聚合中排除过滤器?
【发布时间】:2015-11-25 23:46:08
【问题描述】:

我使用 3 个过滤器过滤了查询:"query": "iphone", color:5, category:10, brand:25

如何获取每个品牌中具有color:5category:10 的产品数量?

在 Solr 中我是这样做的:

fq={!tag=tag_facet_brand}facet_brand:"564"&facet.field={!ex=tag_facet_brand}facet_brand

如何从聚合上下文中排除 1 个过滤器? (我不能使用全局,因为我丢失了query:iphone,我不能使用 post_filter - 因为性能原因)。

【问题讨论】:

  • 您找到解决方案了吗?
  • 是的,经过一段时间的生产和实验,“全局”的解决方案:{},- 是我发现的最好的可能性。它的运行速度不是很快,但使用缓存它具有可接受的性能。

标签: elasticsearch facets


【解决方案1】:

我们刚刚从 SOLR 迁移,我们正面临同样的问题。

您可以尝试全局存储桶,然后为每个存储桶添加 query filter

"filter" : {
            "query" : {
                "query_string" : {
                    "query" : "iPhone"
                }
            }
        }

你应该以这样的结尾:

{
    "size": 10,
    "query": {
        "query_string": {
            "query": "iphone"
        }
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "brand": "564"
                    }
                },
                {
                    "term": {
                        "color": "5"
                    }
                },
                {
                    "term": {
                        "category": "10"
                    }
                }
            ]
        }
    },
    "aggs": {
        "all": {
            "global": {},
            "aggs": {
                "keyword": {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "query": {
                                        "query_string": {
                                            "query": "iphone"
                                        }
                                    }
                                },
                                {
                                    "term": {
                                        "color": "5"
                                    }
                                },
                                {
                                    "term": {
                                        "category": "10"
                                    }
                                }
                            ]
                        }
                    },
                    "aggs": {
                        "brand": {
                            "terms": {
                                "field": "brand"
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

  • "global": {}, - 在这种情况下不好,因为我有 20 个 aggs,所以请求大小非常增长,而且对开发人员不友好。也必须要求 DRY 原则。
  • 我们也有很多 aggs。我们仍然是 DRY,但是在生成请求的代码中...... foreach agg,您添加了每个过滤器,除了与 agg 相关的过滤器。
【解决方案2】:

除了@Lionel Sengkouvanh 的回答,这里是java API 的等价物:

SearchRequestBuilder request = client
  .prepareSearch(INDEX_NAME)
  .setQuery(theQuery)
  ...
  .addAggregation(
     AggregationBuilders.global("all").subAggregation(      
       AggregationBuilders.filter("keyword").filter(filter).subAggregation(
         AggregationBuilders.terms("brand").field("brand")  
       )
     )
  );

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 2015-09-17
    • 2014-02-02
    • 2018-09-14
    • 2021-11-06
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多