【问题标题】:Aggregate only matched nested object values in ElasticSearch在 ElasticSearch 中仅聚合匹配的嵌套对象值
【发布时间】:2015-10-02 04:38:59
【问题描述】:

我只需要对匹配查询的嵌套对象的值求和。看起来 ElasticSearch 确定与查询匹配的文档,然后对所有嵌套对象求和。从下面的大纲中,我想搜索 nestedobjects.objtype="A" 并仅返回匹配嵌套对象的 objvalue 的总和,我想获得值 4。这可能吗?如果有,怎么做?

这是映射

{
  "myindex": {
    "mappings": {
      "mytype": {
        "properties": {
           "nestedobjects": {
             "type": "nested",
             "include_in_parent": true,
             "properties": {
               "objtype": {
                 "type": "string"
               },
               "objvalue": {
                 "type": "integer"
               }
             }
           }
         }
       }
     }
   }
 }

这是我的文件

PUT /myindex/mytype/1
{
  "nestedobjects": [
    { "objtype": "A", "objvalue": 1 },
    { "objtype": "B", "objvalue": 2 }
  ]
}
PUT /myindex/mytype/2
{
  "nestedobjects": [
    { "objtype": "A", "objvalue": 3 },
    { "objtype": "B", "objvalue": 3 }
  ]
}

这是我的查询代码。

POST allscriptshl7/_search?search_type=count
{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "nestedobjects.objtype:A"
        }
      }
    }
  },
  "aggregations": {
    "my_agg": {
      "sum": {
        "field": "nestedobjects.objvalue"
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    由于两个(外部)文档都匹配其中一个内部文档与查询匹配的条件,因此返回两个外部文档,并针对属于这些外部文档的所有内部文档计算聚合。呼。

    无论如何,我认为这似乎可以满足您的需求,使用filter aggregation

    POST /myindex/_search?search_type=count
    {
       "aggs": {
          "nested_nestedobjects": {
             "nested": {
                "path": "nestedobjects"
             },
             "aggs": {
                "filtered_nestedobjects": {
                   "filter": {
                      "term": {
                         "nestedobjects.objtype": "a"
                      }
                   },
                   "aggs": {
                      "my_agg": {
                         "sum": {
                            "field": "nestedobjects.objvalue"
                         }
                      }
                   }
                }
             }
          }
       }
    }
    ...
    {
       "took": 4,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 0,
          "hits": []
       },
       "aggregations": {
          "nested_nestedobjects": {
             "doc_count": 4,
             "filtered_nestedobjects": {
                "doc_count": 2,
                "my_agg": {
                   "value": 4,
                   "value_as_string": "4.0"
                }
             }
          }
       }
    }
    

    这是我用来测试它的一些代码:

    http://sense.qbox.io/gist/c1494619ff1bd0394d61f3d5a16cb9dfc229113a

    顺便说一句,问题结构很好。

    【讨论】:

    • 没有真正的解决方案:取决于过滤器的预知值,不适用于 histogram、date_histogram 或具有不可预测键的术语。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2021-12-14
    相关资源
    最近更新 更多