获取聚合弹性搜索的所有存储桶

Get all the buckets for a aggregate elastic search

我想获取可用于特定聚合的所有存储桶。是否有任何查询或端点来获取存储桶? 下面是我的映射。如果我使用任何过滤器进行查询,则会出现相关的存储桶,但我希望所有存储桶都在前端显示它具有或操作。 示例:如果我们有 2 条记录,一条是类别为 chair,另一条在 table 中。如果我 select 一把椅子,它返回 table 计数为零,但它应该显示为 table 计数为 1。因此用户可以 select 两者。

我的映射:

{
"properties": {
  "australiasellable": {
    "type": "boolean"
  },
  "avgRating": {
    "type": "float"
  },
  "categories": {
    "type": "nested"
  },
  "category": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "categorycode": {
    "type": "text",
    "fielddata": true
  },
  "categoryname": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "colour": {
    "type": "text",
    "fielddata": true
  },
  "commercialuse": {
    "type": "boolean"
  },
  "customisable": {
    "type": "boolean"
  },
  "depth": {
    "type": "float"
  },
  "freedelivery": {
    "type": "boolean"
  },
  "height": {
    "type": "float"
  },
  "listprice": {
    "type": "float"
  },
  "location": {
    "type": "geo_point"
  },
  "material": {
    "type": "text",
    "fielddata": true
  },
  "materialcode": {
    "type": "text",
    "fielddata": true
  },
  "message": {
    "type": "geo_point"
  },
  "numberOfRating": {
    "type": "long"
  },
  "online": {
    "type": "boolean"
  },
  "outdooruse": {
    "type": "boolean"
  },
  "productid": {
    "type": "long"
  },
  "productimageurl": {
    "type": "text",
    "fielddata": true
  },
  "productname": {
    "type": "text",
    "fielddata": true
  },
  "producttypecode": {
    "type": "text",
    "fielddata": true
  },
  "sellercode": {
    "type": "text",
    "fielddata": true
  },
  "sellerdescription": {
    "type": "text",
    "fielddata": true
  },
  "shortdescription": {
    "type": "text",
    "fielddata": true
  },
  "sku": {
    "type": "text",
    "fielddata": true
  },
  "state": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "stylecode": {
    "type": "text",
    "fielddata": true
  },
  "warrantycode": {
    "type": "text",
    "fielddata": true
  },
  "weight": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },
  "width": {
    "type": "float"
  }
}

}

此致, 斯里尼瓦斯

一个可能的解决方案是不在负载的 query 部分设置过滤器,而是执行 filtered aggregations and use the top_hits 以获取匹配文档的 _source

长话短说,如果您应用 query,它当然会影响您的聚合。所以诀窍是不应用任何查询(match_all 或删除整个 query 对象)并在子聚合中执行查询,如下所示:

使用您的 category 字段:

GET your_index/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "actual_query_agg": {
      "filter": {
        "term": {
          "category.keyword": {
            "value": "chair"
          }
        }
      },
      "aggs": {
        "actual_query_agg_top_hits": {
          "top_hits": {
            "_source": [
              "category"
            ],
            "size": 10
          }
        }
      }
    },
    "excluding_my_query_filtered_agg": {
      "filter": {
        "bool": {
          "must_not": {
            "term": {
              "category.keyword": "chair"
            }
          }
        }
      },
      "aggs": {
        "by_other_categories_agg": {
          "terms": {
            "field": "category.keyword",
            "size": 10
          },
          "aggs": {
            "categorized_other_docs_agg_top_hits": {
              "top_hits": {
                "_source": [
                  "category"
                ],
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

如果您只对计数而不是底层文档感兴趣,则可以删除 top_hits 子聚合,即:

GET your_index/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "actual_query_agg": {
      "filter": {
        "term": {
          "category.keyword": {
            "value": "chair"
          }
        }
      }
    },
    "excluding_my_query_filtered_agg": {
      "filter": {
        "bool": {
          "must_not": {
            "term": {
              "category.keyword": "chair"
            }
          }
        }
      },
      "aggs": {
        "by_other_categories_agg": {
          "terms": {
            "field": "category.keyword",
            "size": 10
          }
        }
      }
    }
  }
}