Shape query
Shape query
Queries documents that contain fields indexed using the shape
type.
Requires the shape Mapping.
The query supports two ways of defining the target shape, either by providing a whole shape definition, or by referencing the name, or id, of a shape pre-indexed in another index. Both formats are defined below with examples.
Inline Shape Definition
Similar to the geo_shape
query, the shape
query uses GeoJSON or Well Known Text (WKT) to represent shapes.
Given the following index:
resp = client.indices.create(
index="example",
mappings={
"properties": {
"geometry": {
"type": "shape"
}
}
},
)
print(resp)
resp1 = client.index(
index="example",
id="1",
refresh="wait_for",
document={
"name": "Lucky Landing",
"geometry": {
"type": "point",
"coordinates": [
1355.400544,
5255.530286
]
}
},
)
print(resp1)
response = client.indices.create(
index: 'example',
body: {
mappings: {
properties: {
geometry: {
type: 'shape'
}
}
}
}
)
puts response
response = client.index(
index: 'example',
id: 1,
refresh: 'wait_for',
body: {
name: 'Lucky Landing',
geometry: {
type: 'point',
coordinates: [
1355.400544,
5255.530286
]
}
}
)
puts response
const response = await client.indices.create({
index: "example",
mappings: {
properties: {
geometry: {
type: "shape",
},
},
},
});
console.log(response);
const response1 = await client.index({
index: "example",
id: 1,
refresh: "wait_for",
document: {
name: "Lucky Landing",
geometry: {
type: "point",
coordinates: [1355.400544, 5255.530286],
},
},
});
console.log(response1);
PUT /example
{
"mappings": {
"properties": {
"geometry": {
"type": "shape"
}
}
}
}
PUT /example/_doc/1?refresh=wait_for
{
"name": "Lucky Landing",
"geometry": {
"type": "point",
"coordinates": [ 1355.400544, 5255.530286 ]
}
}
The following query will find the point using the Elasticsearch’s envelope
GeoJSON extension:
resp = client.search(
index="example",
query={
"shape": {
"geometry": {
"shape": {
"type": "envelope",
"coordinates": [
[
1355,
5355
],
[
1400,
5200
]
]
},
"relation": "within"
}
}
},
)
print(resp)
const response = await client.search({
index: "example",
query: {
shape: {
geometry: {
shape: {
type: "envelope",
coordinates: [
[1355, 5355],
[1400, 5200],
],
},
relation: "within",
},
},
},
});
console.log(response);
GET /example/_search
{
"query": {
"shape": {
"geometry": {
"shape": {
"type": "envelope",
"coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
},
"relation": "within"
}
}
}
}
Pre-Indexed Shape
The Query also supports using a shape which has already been indexed in another index. This is particularly useful for when you have a pre-defined list of shapes which are useful to your application and you want to reference this using a logical name (for example New Zealand) rather than having to provide their coordinates each time. In this situation it is only necessary to provide:
id
- The ID of the document that containing the pre-indexed shape.index
- Name of the index where the pre-indexed shape is. Defaults to shapes.path
- The field specified as path containing the pre-indexed shape. Defaults to shape.routing
- The routing of the shape document if required.
The following is an example of using the Filter with a pre-indexed shape:
resp = client.indices.create(
index="shapes",
mappings={
"properties": {
"geometry": {
"type": "shape"
}
}
},
)
print(resp)
resp1 = client.index(
index="shapes",
id="footprint",
document={
"geometry": {
"type": "envelope",
"coordinates": [
[
1355,
5355
],
[
1400,
5200
]
]
}
},
)
print(resp1)
resp2 = client.search(
index="example",
query={
"shape": {
"geometry": {
"indexed_shape": {
"index": "shapes",
"id": "footprint",
"path": "geometry"
}
}
}
},
)
print(resp2)
response = client.indices.create(
index: 'shapes',
body: {
mappings: {
properties: {
geometry: {
type: 'shape'
}
}
}
}
)
puts response
response = client.index(
index: 'shapes',
id: 'footprint',
body: {
geometry: {
type: 'envelope',
coordinates: [
[
1355,
5355
],
[
1400,
5200
]
]
}
}
)
puts response
response = client.search(
index: 'example',
body: {
query: {
shape: {
geometry: {
indexed_shape: {
index: 'shapes',
id: 'footprint',
path: 'geometry'
}
}
}
}
}
)
puts response
const response = await client.indices.create({
index: "shapes",
mappings: {
properties: {
geometry: {
type: "shape",
},
},
},
});
console.log(response);
const response1 = await client.index({
index: "shapes",
id: "footprint",
document: {
geometry: {
type: "envelope",
coordinates: [
[1355, 5355],
[1400, 5200],
],
},
},
});
console.log(response1);
const response2 = await client.search({
index: "example",
query: {
shape: {
geometry: {
indexed_shape: {
index: "shapes",
id: "footprint",
path: "geometry",
},
},
},
},
});
console.log(response2);
PUT /shapes
{
"mappings": {
"properties": {
"geometry": {
"type": "shape"
}
}
}
}
PUT /shapes/_doc/footprint
{
"geometry": {
"type": "envelope",
"coordinates": [ [ 1355.0, 5355.0 ], [ 1400.0, 5200.0 ] ]
}
}
GET /example/_search
{
"query": {
"shape": {
"geometry": {
"indexed_shape": {
"index": "shapes",
"id": "footprint",
"path": "geometry"
}
}
}
}
}
Spatial Relations
The following is a complete list of spatial relation operators available:
INTERSECTS
- (default) Return all documents whoseshape
field intersects the query geometry.DISJOINT
- Return all documents whoseshape
field has nothing in common with the query geometry.WITHIN
- Return all documents whoseshape
field is within the query geometry.CONTAINS
- Return all documents whoseshape
field contains the query geometry.
Ignore Unmapped
When set to true
the ignore_unmapped
option will ignore an unmapped field and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to false
(the default value) the query will throw an exception if the field is not mapped.