【问题标题】:Elasticsearch aggregation only on specific entries in an arrayElasticsearch 聚合仅针对数组中的特定条目
【发布时间】:2017-11-16 14:59:57
【问题描述】:

我是 Elasticsearch 新手,不知道如何解决以下问题。 解释我的问题的最简单方法是向您展示一个示例。

以下数组“列表”是我在 Elasticsearch 中所有文件的一部分,但条目各不相同,因此“id”为 42 的“人”可能在我的文件的 50% 中。我要做的是在 Elasticsearch 中的所有文件中获取所有 id 为 42 的人的平均“ranking.position.standard”。

{
"listing": [
    {
        "person": {
            "id": 42
        },
        "ranking": {
            "position": {
                "standard": 2
            }
        }
    },
    {
        "person": {
            "id": 55
        },
        "ranking": {
            "position": {
                "standard": 7
            }
        }
    }
]
}

感谢您的帮助!

【问题讨论】:

  • 我尝试了各种过滤。到目前为止,我所做的只是取回整个文件,如果“列表”数组有一个“id”为 42 的条目。

标签: arrays elasticsearch aggregation


【解决方案1】:

首先,您是否将列表存储为objectnested 数据类型?如果是object,我认为它不会起作用,所以试试下面的例子:

PUT /test
{
  "mappings": {
    "_default_": {
      "properties": {
        "listing": {
          "type": "nested"
        }
      }
    }
  }
}

PUT /test/aa/1
{
  "listing": [
    {
      "person": {
        "id": 42
      },
      "ranking": {
        "position": {
          "standard": 2
        }
      }
    },
    {
      "person": {
        "id": 55
      },
      "ranking": {
        "position": {
          "standard": 7
        }
      }
    }
  ]
}

PUT /test/aa/2
{
  "listing": [
    {
      "person": {
        "id": 42
      },
      "ranking": {
        "position": {
          "standard": 5
        }
      }
    },
    {
      "person": {
        "id": 55
      },
      "ranking": {
        "position": {
          "standard": 6
        }
      }
    }
  ]
}  

GET test/_search
{
  "size": 0,
  "aggs": {
    "nest": {
      "nested": {
        "path": "listing"
      },
      "aggs": {
        "persons": {
          "terms": {
            "field": "listing.person.id",
            "size": 10
          },
          "aggs": {
            "avg_standard": {
              "avg": {
                "field": "listing.ranking.position.standard"
              }
            }
          }
        }
      }
    }
  }
}

这给我带来了以下结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "nest": {
      "doc_count": 4,
      "persons": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 42,
            "doc_count": 2,
            "avg_standard": {
              "value": 3.5
            }
          },
          {
            "key": 55,
            "doc_count": 2,
            "avg_standard": {
              "value": 6.5
            }
          }
        ]
      }
    }
  }
}

这似乎是正确的。

【讨论】:

  • 非常感谢。生成的默认映射:"listing": { "properties": {...} } 但是,您是如何查询它的,以获得准确的结果?
  • @McClane 如果它对您有用,请勾选此项;)
  • 非常感谢!正是我想要的。
  • @McClane 我错过了您的编辑。查询很简单,我没有过滤任何东西,只是聚合数据,首先我必须使用嵌套聚合来访问嵌套数据结构(它们通常存储为单独的文档),然后我进行了术语聚合以获得所有唯一person.id 值,然后我对每个 terms 存储桶进行了另一次聚合以获得平均标准。 :) 所以它是nested -> terms -> avg。三个级别的聚合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
相关资源
最近更新 更多