【问题标题】:How to filter a collection in elasticsearch document如何过滤elasticsearch文档中的集合
【发布时间】:2015-12-14 00:48:06
【问题描述】:

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

{
   "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 并不打算这样做。但其他团队成员并不相信。

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

sum 聚合器中的脚本字段可以帮助我吗? 有没有办法在当前数据模型的弹性搜索中做到这一点? 还是我应该在节目上做?

文档的映射

{
"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 的总和

【问题讨论】:

  • 您能否分享您的索引映射,这些文档在其中被索引?您可以通过命令GET _mapping来检索它。
  • 我将我的映射添加到问题

标签: c# elasticsearch nest elasticsearch-aggregation


【解决方案1】:

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

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

实际上,我正在尝试计算每个总价格的总和 型号代码

假设modelCodecollectionkey,这个nested aggregation 会给你结果:

{
  "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-05-06
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2021-08-04
    • 2017-03-02
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多