Cartesian-centroid aggregation
ポイントおよび形状フィールドのすべての座標値から加重された 重心 を計算するメトリック集約です。
例:
Python
resp = client.indices.create(
index="museums",
mappings={
"properties": {
"location": {
"type": "point"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="museums",
refresh=True,
operations=[
{
"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"
}
],
)
print(resp1)
resp2 = client.search(
index="museums",
size="0",
aggs={
"centroid": {
"cartesian_centroid": {
"field": "location"
}
}
},
)
print(resp2)
Ruby
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
Js
const response = await client.indices.create({
index: "museums",
mappings: {
properties: {
location: {
type: "point",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "museums",
refresh: "true",
operations: [
{
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",
},
],
});
console.log(response1);
const response2 = await client.search({
index: "museums",
size: 0,
aggs: {
centroid: {
cartesian_centroid: {
field: "location",
},
},
},
});
console.log(response2);
Console
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 集約は、重心を計算するために使用するフィールドを指定します。これは Point または Shape タイプでなければなりません。 |
上記の集約は、すべての博物館の文書の位置フィールドの重心を計算する方法を示しています。
Console-Result
{
...
"aggregations": {
"centroid": {
"location": {
"x": 396.6213124593099,
"y": 5100.982991536458
},
"count": 6
}
}
}
cartesian_centroid
集約は、他のバケット集約にサブ集約として組み合わせると、より興味深くなります。
例:
Python
resp = client.search(
index="museums",
size="0",
aggs={
"cities": {
"terms": {
"field": "city.keyword"
},
"aggs": {
"centroid": {
"cartesian_centroid": {
"field": "location"
}
}
}
}
},
)
print(resp)
Js
const response = await client.search({
index: "museums",
size: 0,
aggs: {
cities: {
terms: {
field: "city.keyword",
},
aggs: {
centroid: {
cartesian_centroid: {
field: "location",
},
},
},
},
},
});
console.log(response);
Console
POST /museums/_search?size=0
{
"aggs": {
"cities": {
"terms": { "field": "city.keyword" },
"aggs": {
"centroid": {
"cartesian_centroid": { "field": "location" }
}
}
}
}
}
上記の例では、博物館の各都市の中央位置を見つけるために、cartesian_centroid
を terms バケット集約のサブ集約として使用しています。
Console-Result
{
...
"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
}
}
]
}
}
}
Cartesian Centroid Aggregation on shape fields
形状の重心メトリックは、ポイントのそれよりも複雑です。形状を含む特定の集約バケットの重心は、バケット内の最高次元の形状タイプの重心です。たとえば、バケットにポリゴンとラインからなる形状が含まれている場合、ラインは重心メトリックに寄与しません。各形状タイプの重心は異なる方法で計算されます。 Circle を介して取り込まれたエンベロープと円はポリゴンとして扱われます。
ジオメトリタイプ | 重心計算 |
---|---|
[Multi]Point | すべての座標の等重み平均 |
[Multi]LineString | 各セグメントの重心の加重平均で、各セグメントの重みは座標と同じ単位での長さです |
[Multi]Polygon | ポリゴンのすべての三角形の重心の加重平均で、三角形は2つの連続した頂点と開始点によって形成されます。 穴は負の重みを持ちます。重みは三角形の面積を座標の単位の平方で計算したものを表します |
GeometryCollection | 最高次元のすべての基礎となるジオメトリの重心です。ポリゴンとラインおよび/またはポイントがある場合、ラインおよび/またはポイントは無視されます。 ラインとポイントがある場合、ポイントは無視されます |
例:
Python
resp = client.indices.create(
index="places",
mappings={
"properties": {
"geometry": {
"type": "shape"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="places",
refresh=True,
operations=[
{
"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
]
]
]
}
}
],
)
print(resp1)
resp2 = client.search(
index="places",
size="0",
aggs={
"centroid": {
"cartesian_centroid": {
"field": "geometry"
}
}
},
)
print(resp2)
Ruby
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
Js
const response = await client.indices.create({
index: "places",
mappings: {
properties: {
geometry: {
type: "shape",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "places",
refresh: "true",
operations: [
{
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],
],
],
},
},
],
});
console.log(response1);
const response2 = await client.search({
index: "places",
size: 0,
aggs: {
centroid: {
cartesian_centroid: {
field: "geometry",
},
},
},
});
console.log(response2);
Console
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"
}
}
}
}
Console-Result
{
...
"aggregations": {
"centroid": {
"location": {
"x": 496.74041748046875,
"y": 5239.29638671875
},
"count": 2
}
}
}