获取所有存储桶以进行总的弹性搜索 [英] Get all the buckets for a aggregate elastic search

查看:49
本文介绍了获取所有存储桶以进行总的弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取所有可用于特定集合的存储桶.是否有任何查询或端点来获取存储桶?以下是我的地图.如果我使用任何过滤器查询,那么相关的存储桶就会出现,但是我希望所有存储桶在前端显示它具有操作.示例:如果我们有2条记录,一条记录的类别为主席,另一条记录在表中.如果我选择椅子,则返回的桌数为零,但应显示为桌数为1.因此用户可以同时选择两者.

I want to get all the buckets available for a particular aggregate. Is there any query or endpoint to get the buckets? Below is my Mapping. If I query with any filter then the related buckets are coming up, but I want all the buckets to show it on the frontend to have or operations. Example: If we have 2 records, one is with category as chair and the other is in the table. If I select a chair it is returning table count is zero but it should show as table count as 1. So user can select both.

MyMapping:

MyMapping:

{
"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 部分中设置过滤器,而是执行

A possible solution would be not to set the filter in the query section of your payload but rather perform filtered aggregations and use the top_hits to get the _sources of the matched docs.

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

Long story short, if you apply a query, it'll of course affect your aggregations. So the trick is to not apply any query (either match_all or remove the whole query object) and perform the queries in the sub-aggregations as follows:

使用您的 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 子聚合,即:

You can get rid of the top_hits sub-aggregations if you're just interested in the counts and not the underlying docs, i.e.:

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
          }
        }
      }
    }
  }
}

这篇关于获取所有存储桶以进行总的弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆