Запрос по форме
Ищет документы, содержащие поля, индексированные с помощью типа shape.
Требует shape Картирование.
Запрос поддерживает два способа определения целевой формы: путем предоставления полного определения формы или путем ссылки на имя или идентификатор формы, предварительно индексированной в другом индексе. Оба формата определены ниже с примерами.
Определение формы в строке
Аналогично запросу geo_shape, запрос shape использует GeoJSON или Well-Known Text (WKT) для представления форм.
Учитывая следующий индекс:
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 ]
}
} Следующий запрос найдет точку, используя расширение GeoJSON Elasticsearch:
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"
}
}
}
} Предварительно индексированная форма
Запрос также поддерживает использование формы, которая уже была индексирована в другом индексе. Это особенно полезно, когда у вас есть предварительно определенный список форм, полезный для вашего приложения, и вы хотите сослаться на него с помощью логического имени (например, Новая Зеландия), а не предоставлять его координаты каждый раз. В этой ситуации необходимо только указать:
-
id— Идентификатор документа, содержащего предварительно индексированную форму. -
index— Название индекса, в котором находится предварительно индексированная форма. По умолчанию — shapes. -
path— Поле, указанное в качестве пути, содержащего предварительно индексированную форму. По умолчанию — shape. -
routing— Маршрутизация документа формы, если требуется.
Следующий пример использования фильтра с предварительно индексированной формой:
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"
}
}
}
}
} Пространственные отношения
Ниже приведен полный список операторов пространственных отношений:
-
INTERSECTS— (по умолчанию) Возвращает все документы, чьё полеshapeпересекает геометрию запроса. -
DISJOINT— Возвращает все документы, чьё полеshapeне имеет ничего общего с геометрией запроса. -
WITHIN— Возвращает все документы, чьё полеshapeнаходится внутри геометрии запроса. -
CONTAINS— Возвращает все документы, чьё полеshapeсодержит геометрию запроса.
Игнорирование неотображённого поля
При установке в true, параметр ignore_unmapped проигнорирует неотображенное поле и не найдёт ни одного документа для этого запроса. Это может быть полезно при запросе к нескольким индексам, которые могут иметь различные отображения. При установке в false (значение по умолчанию) запрос выдаст исключение, если поле не отображается.
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/query-dsl-shape-query.html