笛卡尔边界聚合

编辑

一种指标聚合,用于计算包含 形状字段所有值的空间边界框。

示例

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",
    query={
        "match": {
            "name": "musée"
        }
    },
    aggs={
        "viewport": {
            "cartesian_bounds": {
                "field": "location"
            }
        }
    },
)
print(resp2)
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: {
    query: {
      match: {
        name: 'musée'
      }
    },
    aggregations: {
      viewport: {
        cartesian_bounds: {
          field: 'location'
        }
      }
    }
  }
)
puts response
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,
  query: {
    match: {
      name: "musée",
    },
  },
  aggs: {
    viewport: {
      cartesian_bounds: {
        field: "location",
      },
    },
  },
});
console.log(response2);
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
{
  "query": {
    "match": { "name": "musée" }
  },
  "aggs": {
    "viewport": {
      "cartesian_bounds": {
        "field": "location"    
      }
    }
  }
}

cartesian_bounds 聚合指定用于获取边界的字段,该字段必须是 形状类型。

geo_bounds 聚合的情况不同,没有设置 wrap_longitude 的选项。这是因为笛卡尔空间是欧几里得空间,不会自身环绕。因此,边界的最小 x 值将始终小于或等于最大 x 值。

以上聚合演示了如何计算所有名称匹配 “musée” 的文档的位置字段的边界框。

以上聚合的响应

{
  ...
  "aggregations": {
    "viewport": {
      "bounds": {
        "top_left": {
          "x": 232.6999969482422,
          "y": 4886.111328125
        },
        "bottom_right": {
          "x": 233.63890075683594,
          "y": 4886.0
        }
      }
    }
  }
}

shape 字段上的笛卡尔边界聚合

编辑

笛卡尔边界聚合也支持 cartesian_shape 字段。

示例

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={
        "viewport": {
            "cartesian_bounds": {
                "field": "geometry"
            }
        }
    },
)
print(resp2)
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: {
      viewport: {
        cartesian_bounds: {
          field: 'geometry'
        }
      }
    }
  }
)
puts response
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: {
    viewport: {
      cartesian_bounds: {
        field: "geometry",
      },
    },
  },
});
console.log(response2);
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": {
    "viewport": {
      "cartesian_bounds": {
        "field": "geometry"
      }
    }
  }
}
{
  ...
  "aggregations": {
    "viewport": {
      "bounds": {
        "top_left": {
          "x": 491.2349853515625,
          "y": 5239.4208984375
        },
        "bottom_right": {
          "x": 496.9425048828125,
          "y": 5237.408203125
        }
      }
    }
  }
}