- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下架构来表示可以具有多种变体的产品(例如:T恤的尺寸):
{
"mappings": {
"properties": {
"id": {"type": "keyword"},
"name": {"type": "text"},
"variants": {
"type": "nested",
"properties": {
"inventory": {"type": "long"},
"customizations": {"type": "object"},
"customizations.name": {"type": "keyword"},
"customizations.value": {"type": "keyword"}
}
}
}
}
}
然后,我可以使用它插入如下所示的产品数据:
{
"id": "prod-1",
"name": "Shirt Design 1",
"variants": [
{"inventory": 78, "customizations": [{"name": "size", "value": "L"}, {"name": "color", "value": "blue"}]},
{"inventory": 78, "customizations": [{"name": "size", "value": "M"}, {"name": "color", "value": "blue"}]},
{"inventory": 89, "customizations": [{"name": "size", "value": "S"}, {"name": "color", "value": "blue"}]}
]
}
{
"id": "prod-2",
"name": "Shirt Design 2",
"variants": [
{"inventory": 78, "customizations": [{"name": "size", "value": "L"}, {"name": "color", "value": "green"}]},
{"inventory": 78, "customizations": [{"name": "size", "value": "M"}, {"name": "color", "value": "green"}]}
]
}
筛选/查询此索引时,我希望能够根据构成产品的自定义来显示构面。这些自定义项是用户提交的,因此不在我的控制范围内,但其想法是能够显示以下过滤器:
☐ Size:
- S (1)
- M (2)
- L (2)
☐ Color:
- blue (1)
- green (1)
现在,我可以使用以下查询按自定义名称正确存储分区:
{
"size": 0,
"aggs": {
"skus": {
"nested": {
"path": "variants"
},
"aggs": {
"customization_names": {
"terms": {
"field": "variants.customizations.name"
}
}
}
}
}
}
这给了我以下几个方面:
"buckets": [
{
"doc_count": 2,
"key": "color"
},
{
"doc_count": 2,
"key": "size"
}
],
试图进行子聚合以获取下面的实际自定义列表是我遇到的困难。我试过了:
{
"size": 0,
"aggs": {
"skus": {
"nested": {
"path": "variants"
},
"aggs": {
"customization_names": {
"terms": {
"field": "variants.customizations.name"
},
"aggs": {
"sub": {
"reverse_nested": {},
"aggs": {
"customization_values": {
"terms": {
"field": "variants.customizations.value"
}
}
}
}
}
}
}
}
}
}
不返回任何子桶:
buckets": [
{
"doc_count": 4,
"key": "color",
"sub": {
"doc_count": 2,
"customization_values": {
"buckets": [],
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0
}
}
},
{
"doc_count": 4,
"key": "size",
"sub": {
"doc_count": 2,
"customization_values": {
"buckets": [],
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0
}
}
}
],
如果我不使用
reverse_nested
而不是空的子桶,那么我会在其中获得所有可能的值,因此,例如,我将
red and blue
作为
size
子桶的一部分。
customizations
作为key => value的映射,但是也无法使其工作。但是,“自定义”的格式在这里有些可自定义。
// mapping:
"customizations.facet_code": {"type": "keyword"}
// data:
"customizations": [{"name": "size", "value": "M", "facet_code": "{name:size,value:M}"],
然后,我可以根据
facet_code
正确进行存储,并且我的应用程序可以将其反序列化以再次将事物重新组合在一起。
最佳答案
执行此操作的“正确”方法是也将customizations
设置为nested
类型,而不是object
。也就是说:
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text"
},
"variants": {
"type": "nested",
"properties": {
"inventory": {
"type": "long"
},
"customizations": {
"type": "nested", <-- This
"properties": {
"name": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
}
}
}
}
}
}
}
该查询将是
{
"size": 0,
"aggs": {
"skus": {
"nested": {
"path": "variants.customizations"
},
"aggs": {
"customization_names": {
"terms": {
"field": "variants.customizations.name"
},
"aggs": {
"customization_values": {
"terms": {
"field": "variants.customizations.value"
}
}
}
}
}
}
}
}
产生所有必需的方面:
{
...
"aggregations":{
"skus":{
"doc_count":10,
"customization_names":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"color",
"doc_count":5,
"customization_values":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"blue",
"doc_count":3
},
{
"key":"green",
"doc_count":2
}
]
}
},
{
"key":"size",
"doc_count":5,
"customization_values":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"L",
"doc_count":2
},
{
"key":"M",
"doc_count":2
},
{
"key":"S",
"doc_count":1
}
]
}
}
]
}
}
}
}
关于elasticsearch - 如何使用Elastic对嵌套对象进行子聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63677930/
我有一个 Cassandra 集群,里面有 4 个表和数据。 我想使用聚合函数(sum,max ...)发出请求,但我在这里读到这是不可能的: http://www.datastax.com/docu
我有以下两张表 Table: items ID | TITLE 249 | One 250 | Two 251 | Three 我投票给这些: Table: votes VID | IID | u
这个问题在这里已经有了答案: Update MongoDB field using value of another field (12 个答案) 关闭 3 年前。 我想根据另一个“源”集合的文档中
我的收藏包含以下文件。我想使用聚合来计算里面有多少客户,但我遇到了一些问题。我可以获得总行数,但不能获得总(唯一)客户。 [{ _id: "n001", channel: "Kalip
我有下表 Id Letter 1001 A 1001 H 1001 H 1001 H 1001 B 1001 H 1001 H 1001
得到一列的表 ABC。 “创建”的日期列。所以样本值就像; created 2009-06-18 13:56:00 2009-06-18 12:56:00 2009-06-17 14:02:0
我有一个带有数组字段的集合: {[ name:String buyPrice:Int sellPrice:Int ]} 我试图找到最低和最高买入/卖出价格。在某些条目中,买入或卖出价格为零
我有以下问题: 在我的 mongo db 中,我有以下结构: { "instanceId": "12", "eventId": "0-1b", "activityType":
下面给出的是我要在其上触发聚合查询的 Elasticsearch 文档。 { "id": 1, "attributes": [ { "fieldId": 1,
我正在使用 Django 的 aggregate query expression总计一些值。最终值是一个除法表达式,有时可能以零作为分母。如果是这种情况,我需要一种方法来逃避,以便它只返回 0。 我
我正在学习核心数据,特别是聚合。 当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数。 目前我正在这样做: NSExpression *ex = [NSExpression expr
我需要有关 Delphi 中的 ClientDatasets 的一些帮助。 我想要实现的是一个显示客户的网格,其中一列显示每个客户的订单数量。我将 ClientDataset 放在表单上并从 Delp
我的集合有 10M 个文档,并且有一个名为 movieId 的字段;该文档具有以下结构: { "_id" : ObjectId("589bed43e3d78e89bfd9b779"), "us
这个问题已经有答案了: What is the difference between association, aggregation and composition? (21 个回答) 已关闭 9
我在 elasticsearch 中有一些类似于这些示例的文档: { "id": ">", "list": [ "a", "b", "c" ] } { "id"
我正在做一些聚合。但是结果完全不是我所期望的,似乎它们没有聚合索引中与我的查询匹配的所有文档,在这种情况下 - 它有什么好处? 例如,首先我做这个查询: {"index":"datalayer","t
假设我在 ES 中有这些数据。 | KEY | value | |:-----------|------------:| | A |
可能在我的文档中,我有一个被分析的文本字段。我只是在ElasticSearch AggregationAPI中迷路了。我需要2种不同情况的支持: 情况A)结果是带有计数标记(条款)的篮子下降。 情况B
我正在为网上商店构建多面过滤功能,如下所示: Filter on Brand: [ ] LG (10) [ ] Apple (5) [ ] HTC (3) Filter on OS: [ ] Andr
我有一个父/子关系并且正在搜索 child 。 是否可以在父属性上创建聚合? 例如parent 是 POST,children 是 COMMENT。如果父项具有“类别”属性,是否可以搜索 COMMEN
我是一名优秀的程序员,十分优秀!