Elasticsearch - 如何获取嵌套文档集的最小值/最大值/平均值

时间:2015-03-04 22:30:37

标签: elasticsearch

考虑到Elasticsearch中的以下映射和文档,我如何获得一组符合特定条件的嵌套文档的min / max / avg?例如,我怎样才能让它们成为狗的最小年龄?我的过滤器会找到有狗的正确人,但是我如何制作min然后根据正确的嵌套文档进行计算。

(1)映射

{
   "myIndex": {
      "mappings": {
         "person": {
            "properties": {
               "name": {
                  "type": "string"
               },
               "pets": {
                  "type": "nested",
                  "properties": {
                     "age": {
                        "type": "long"
                     },
                     "name": {
                        "type": "string"
                     },
                     "type": {
                        "type": "string"
                     }
                  }
               }
            }
         }
      }
   }
}

(2)数据

{
  "name": "bob",
  "pets": [
    {
      "type": "dog",
      "name": "wolfie",
      "age": 20
    },
    {
      "type": "cat",
      "name": "kitty",
      "age": 6
    }
  ]
}

{
  "name": "bill",
  "pets": [
    {
      "type": "fish",
      "name": "goldie",
      "age": 2
    },
    {
      "type": "cat",
      "name": "meowie",
      "age": 18
    }
  ]
}

(3)查询和聚合

{
  "query": {
    "filtered": {
      "filter": {
        "nested": {
          "path": "pets",
          "filter" : {
            "terms": {
              "pets.type": ["dog"]
            }
          }
        }
      }
    }
  },
  "aggs": {
    "minage": {
      "nested": {
        "path": "pets"
      },
      "aggs": {
        "minage": {
          "min": {
            "field": "age"
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为您可以使用filter aggregation和嵌套过滤器的join option组合获得所需内容。

此代码对我有用:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
       "person": {
            "properties": {
               "name": {
                  "type": "string"
               },
               "pets": {
                  "type": "nested",
                  "properties": {
                     "age": {
                        "type": "long"
                     },
                     "name": {
                        "type": "string"
                     },
                     "type": {
                        "type": "string"
                     }
                  }
               }
            }
         }
   }
}

PUT /test_index/person/1
{
  "name": "bob",
  "pets": [
    {
      "type": "dog",
      "name": "wolfie",
      "age": 20
    },
    {
      "type": "cat",
      "name": "kitty",
      "age": 6
    }
  ]
}

PUT /test_index/person/2
{
  "name": "bill",
  "pets": [
    {
      "type": "fish",
      "name": "goldie",
      "age": 2
    },
    {
      "type": "cat",
      "name": "meowie",
      "age": 18
    }
  ]
}

PUT /test_index/person/3
{
  "name": "john",
  "pets": [
    {
      "type": "dog",
      "name": "oldie",
      "age": 25
    }
  ]
}

POST /test_index/_search?search_type=count
{
   "aggs": {
      "minage_1": {
         "nested": {
            "path": "pets"
         },
         "aggs": {
            "minage_2": {
               "filter": {
                  "nested": {
                     "path": "pets",
                     "filter": {
                        "terms": {
                           "pets.type": [
                              "dog"
                           ]
                        }
                     },
                     "join": false
                  }
               },
               "aggs": {
                  "min_age_3": {
                     "min": {
                        "field": "age"
                     }
                  }
               }
            }
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "minage_1": {
         "doc_count": 5,
         "minage_2": {
            "doc_count": 2,
            "min_age_3": {
               "value": 20
            }
         }
      }
   }
}