笛卡尔质心聚合编辑

一种指标聚合,用于计算点和形状字段的所有坐标值的加权 质心

示例

response = client.indices.create(
  index: 'museums',
  body: {
    mappings: {
      properties: {
        location: {
          type: 'point'
        }
      }
    }
  }
)
puts response

response = client.bulk(
  index: 'museums',
  refresh: true,
  body: [
    {
      index: {
        _id: 1
      }
    },
    {
      location: 'POINT (491.2350 5237.4081)',
      city: 'Amsterdam',
      name: 'NEMO Science Museum'
    },
    {
      index: {
        _id: 2
      }
    },
    {
      location: 'POINT (490.1618 5236.9219)',
      city: 'Amsterdam',
      name: 'Museum Het Rembrandthuis'
    },
    {
      index: {
        _id: 3
      }
    },
    {
      location: 'POINT (491.4722 5237.1667)',
      city: 'Amsterdam',
      name: 'Nederlands Scheepvaartmuseum'
    },
    {
      index: {
        _id: 4
      }
    },
    {
      location: 'POINT (440.5200 5122.2900)',
      city: 'Antwerp',
      name: 'Letterenhuis'
    },
    {
      index: {
        _id: 5
      }
    },
    {
      location: 'POINT (233.6389 4886.1111)',
      city: 'Paris',
      name: 'Musée du Louvre'
    },
    {
      index: {
        _id: 6
      }
    },
    {
      location: 'POINT (232.7000 4886.0000)',
      city: 'Paris',
      name: "Musée d'Orsay"
    }
  ]
)
puts response

response = client.search(
  index: 'museums',
  size: 0,
  body: {
    aggregations: {
      centroid: {
        cartesian_centroid: {
          field: 'location'
        }
      }
    }
  }
)
puts response
PUT /museums
{
  "mappings": {
    "properties": {
      "location": {
        "type": "point"
      }
    }
  }
}

POST /museums/_bulk?refresh
{"index":{"_id":1}}
{"location": "POINT (491.2350 5237.4081)", "city": "Amsterdam", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "POINT (490.1618 5236.9219)", "city": "Amsterdam", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "POINT (491.4722 5237.1667)", "city": "Amsterdam", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "POINT (440.5200 5122.2900)", "city": "Antwerp", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "POINT (233.6389 4886.1111)", "city": "Paris", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "POINT (232.7000 4886.0000)", "city": "Paris", "name": "Musée d'Orsay"}

POST /museums/_search?size=0
{
  "aggs": {
    "centroid": {
      "cartesian_centroid": {
        "field": "location" 
      }
    }
  }
}

cartesian_centroid 聚合指定用于计算质心的字段,该字段必须是 形状 类型。

上述聚合演示了如何计算所有博物馆文档的 location 字段的质心。

上述聚合的响应

{
  ...
  "aggregations": {
    "centroid": {
      "location": {
        "x": 396.6213124593099,
        "y": 5100.982991536458
      },
      "count": 6
    }
  }
}

cartesian_centroid 聚合与其他桶聚合组合为子聚合时,它会更有趣。

示例

POST /museums/_search?size=0
{
  "aggs": {
    "cities": {
      "terms": { "field": "city.keyword" },
      "aggs": {
        "centroid": {
          "cartesian_centroid": { "field": "location" }
        }
      }
    }
  }
}

上面的示例使用 cartesian_centroid 作为 词条 桶聚合的子聚合,用于查找每个城市中博物馆的中心位置。

上述聚合的响应

{
  ...
  "aggregations": {
    "cities": {
      "sum_other_doc_count": 0,
      "doc_count_error_upper_bound": 0,
      "buckets": [
        {
          "key": "Amsterdam",
          "doc_count": 3,
          "centroid": {
            "location": {
              "x": 490.9563293457031,
              "y": 5237.16552734375
            },
            "count": 3
          }
        },
        {
          "key": "Paris",
          "doc_count": 2,
          "centroid": {
            "location": {
              "x": 233.16944885253906,
              "y": 4886.0556640625
            },
            "count": 2
          }
        },
        {
          "key": "Antwerp",
          "doc_count": 1,
          "centroid": {
            "location": {
              "x": 440.5199890136719,
              "y": 5122.2900390625
            },
            "count": 1
          }
        }
      ]
    }
  }
}

shape 字段上的笛卡尔质心聚合编辑

形状的质心度量比点的质心度量更细致。包含形状的特定聚合桶的质心是桶中最高维度形状类型的质心。例如,如果一个桶包含由多边形和线组成的形状,则线不会对质心度量做出贡献。每种形状的质心的计算方式都不同。通过 处理器摄入的包络线和圆被视为多边形。

几何类型 质心计算

[多]点

所有坐标的等权平均值

[多]线串

每个线段质心的加权平均值,其中每个线段的权重是其长度(单位与坐标相同)

[多]多边形

多边形所有三角形的所有质心的加权平均值,其中三角形由每两个连续顶点和起点构成。孔的权重为负。权重表示三角形的面积,以坐标单位的平方计算

几何集合

所有具有最高维度的底层几何的质心。如果有多边形、线和/或点,则忽略线和/或点。如果有线和点,则忽略点

示例

response = client.indices.create(
  index: 'places',
  body: {
    mappings: {
      properties: {
        geometry: {
          type: 'shape'
        }
      }
    }
  }
)
puts response

response = client.bulk(
  index: 'places',
  refresh: true,
  body: [
    {
      index: {
        _id: 1
      }
    },
    {
      name: 'NEMO Science Museum',
      geometry: 'POINT(491.2350 5237.4081)'
    },
    {
      index: {
        _id: 2
      }
    },
    {
      name: 'Sportpark De Weeren',
      geometry: {
        type: 'Polygon',
        coordinates: [
          [
            [
              496.5305328369141,
              5239.347642069457
            ],
            [
              496.6979026794433,
              5239.172175893484
            ],
            [
              496.9425201416015,
              5239.238958618537
            ],
            [
              496.7944622039794,
              5239.420969150824
            ],
            [
              496.5305328369141,
              5239.347642069457
            ]
          ]
        ]
      }
    }
  ]
)
puts response

response = client.search(
  index: 'places',
  size: 0,
  body: {
    aggregations: {
      centroid: {
        cartesian_centroid: {
          field: 'geometry'
        }
      }
    }
  }
)
puts response
PUT /places
{
  "mappings": {
    "properties": {
      "geometry": {
        "type": "shape"
      }
    }
  }
}

POST /places/_bulk?refresh
{"index":{"_id":1}}
{"name": "NEMO Science Museum", "geometry": "POINT(491.2350 5237.4081)" }
{"index":{"_id":2}}
{"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 496.5305328369141, 5239.347642069457 ], [ 496.6979026794433, 5239.1721758934835 ], [ 496.9425201416015, 5239.238958618537 ], [ 496.7944622039794, 5239.420969150824 ], [ 496.5305328369141, 5239.347642069457 ] ] ] } }

POST /places/_search?size=0
{
  "aggs": {
    "centroid": {
      "cartesian_centroid": {
        "field": "geometry"
      }
    }
  }
}
{
  ...
  "aggregations": {
    "centroid": {
      "location": {
        "x": 496.74041748046875,
        "y": 5239.29638671875
      },
      "count": 2
    }
  }
}