0

我有一个在我开始开发之前设计的数据模型。模型具有键值对的集合(它位于嵌套对象中)。我正在尝试过滤此集合上的特定键,如下所示:

{
   "created":"2015-09-07",
   "collection":[
      {
         "key":"a",
         "value":12
      },
      {
         "key":"b",
         "value":21
      },
      {
         "key":"c",
         "value":36
      }
   ]
}

例如:我正在尝试获取其键为“a”的值的总和,我尝试了如下查询

{
  "aggs": {
    "allMembers": {
      "filter": {
        "term": {
          "doc.collection.key": "a"
        }
      },
      "aggs": {
        "rev": {
          "sum": {
            "field": "doc.collection.value"
          }
        }
      }
    }
  },
  "query": {}
}

此查询返回每个文档中所有值的总和。

我认为这是正常的,目前的数据模型无法做到这一点。我搜索了类似的案例,但找不到。我认为 ElasticSearch 并不打算这样做。但其他团队成员并不相信。

我想也许我应该在程序中执行此操作,将整个文档转换为另一个对象并通过查询获取集合的总和。但在这种情况下,我为什么要使用弹性搜索,我可以使用 nosql 数据库或 redis 来做到这一点。

总和聚合器中的脚本字段可以帮助我吗?有没有办法在当前数据模型的弹性搜索中做到这一点?或者我应该在程序上做吗?

文件的映射

{
"smyrna": {
    "mappings": {
        "_default_": {
            "_source": {
                "includes": [
                    "meta.*"
                ]
            },
            "properties": {
                "meta": {
                    "type": "object",
                    "include_in_all": false
                }
            }
        },
        "couchbaseDocument": {
            "_source": {
                "includes": [
                    "meta.*",
                    "doc.*"
                ]
            },
            "properties": {
                "doc": {
                    "properties": {
                        "body": {
                            "properties": {
                                "data": {
                                    "type": "nested",
                                    "properties": {

                                        "products": {
                                            "properties": {
                                                "categories": {
                                                    "properties": {
                                                        "categoryId": {
                                                            "type": "long"
                                                        },
                                                        "categoryName": {
                                                            "type": "string"
                                                        }
                                                    }
                                                },
                                                "currency": {
                                                    "type": "string"
                                                },
                                                "endQuantity": {
                                                    "type": "long"
                                                },
                                                "itemCode": {
                                                    "type": "string"
                                                },
                                                "modelCode": {
                                                    "type": "long"
                                                },
                                                "modelName": {
                                                    "type": "string",
                                                    "analyzer": "keyword"
                                                },
                                                "name": {
                                                    "type": "string"
                                                },
                                                "quantity": {
                                                    "type": "long"
                                                },
                                                "totalPrice": {
                                                    "type": "long"
                                                },
                                                "trademark": {
                                                    "properties": {
                                                        "manufacturerName": {
                                                            "type": "string"
                                                        },
                                                        "trademarkName": {
                                                            "type": "string"
                                                        }
                                                    }
                                                },
                                                "unitPrice": {
                                                    "type": "long"
                                                }
                                            }
                                        },

                                        "totalAmount": {
                                            "type": "long"
                                        },
                                        "uri": {
                                            "type": "string"
                                        },
                                        "userId": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        },
                        "created": {
                            "type": "date",
                            "format": "dateOptionalTime"
                        },
                        "header": {
                            "properties": {
                                "appKey": {
                                    "type": "string"
                                },
                                "channel": {
                                    "type": "string"
                                },
                                "client": {
                                    "type": "string"
                                }
                            }
                        },
                        "id": {
                            "type": "string"
                        },
                        "new in 2.0": {
                            "type": "string"
                        },
                        "ownerAge": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}

}

实际上我正在尝试计算每个 modelCode 的总和

4

1 回答 1

0

我认为您应该在存储文档之前明确定义映射。

{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "couchbaseDocument": {
      "properties": {
        "created": {
          "type": "date"
        },
        "collection": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "string",
              "index": "not_analyzed"
            },
            "value": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}

实际上我正在尝试计算每个 modelCode 的总和

假设modelCodecollection's key,这个嵌套聚合会给你结果:

{
  "size": 0,
  "aggs": {
    "collection": {
      "nested": {
        "path": "collection"
      },
      "aggs": {
        "keys": {
          "terms": {
            "field": "collection.key",
            "order": {
              "value_sum": "desc"
            }
          },
          "aggs": {
            "value_sum": {
              "sum": {
                "field": "collection.value"
              }
            }
          }
        }
      }
    }
  }
}

示例响应:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "collection": {
            "doc_count": 7,
            "keys": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "c",
                        "doc_count": 2,
                        "value_sum": {
                            "value": 79
                        }
                    },
                    {
                        "key": "a",
                        "doc_count": 3,
                        "value_sum": {
                            "value": 46
                        }
                    },
                    {
                        "key": "b",
                        "doc_count": 1,
                        "value_sum": {
                            "value": 21
                        }
                    },
                    {
                        "key": "f",
                        "doc_count": 1,
                        "value_sum": {
                            "value": 20
                        }
                    }
                ]
            }
        }
    }
}
于 2015-10-20T07:49:11.300 回答