Свойства
Тип отображения, object поля и nested поля содержат подполя, называемые properties. Эти свойства могут иметь любой тип данных, включая object и nested. Свойства можно добавить:
- явно, определив их при создании индекса.
- явно, определив их при добавлении или обновлении типа отображения с помощью API обновления отображения.
- динамически, просто индексируя документы, содержащие новые поля.
Ниже приведен пример добавления properties к типу отображения, object поля и nested поля:
resp = client.indices.create(
index="my-index-000001",
mappings={
"properties": {
"manager": {
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "text"
}
}
},
"employees": {
"type": "nested",
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "text"
}
}
}
}
},
)
print(resp)
resp1 = client.index(
index="my-index-000001",
id="1",
document={
"region": "US",
"manager": {
"name": "Alice White",
"age": 30
},
"employees": [
{
"name": "John Smith",
"age": 34
},
{
"name": "Peter Brown",
"age": 26
}
]
},
)
print(resp1) response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
properties: {
manager: {
properties: {
age: {
type: 'integer'
},
name: {
type: 'text'
}
}
},
employees: {
type: 'nested',
properties: {
age: {
type: 'integer'
},
name: {
type: 'text'
}
}
}
}
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
region: 'US',
manager: {
name: 'Alice White',
age: 30
},
employees: [
{
name: 'John Smith',
age: 34
},
{
name: 'Peter Brown',
age: 26
}
]
}
)
puts response const response = await client.indices.create({
index: "my-index-000001",
mappings: {
properties: {
manager: {
properties: {
age: {
type: "integer",
},
name: {
type: "text",
},
},
},
employees: {
type: "nested",
properties: {
age: {
type: "integer",
},
name: {
type: "text",
},
},
},
},
},
});
console.log(response);
const response1 = await client.index({
index: "my-index-000001",
id: 1,
document: {
region: "US",
manager: {
name: "Alice White",
age: 30,
},
employees: [
{
name: "John Smith",
age: 34,
},
{
name: "Peter Brown",
age: 26,
},
],
},
});
console.log(response1); PUT my-index-000001
{
"mappings": {
"properties": {
"manager": {
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
},
"employees": {
"type": "nested",
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
}
}
}
}
PUT my-index-000001/_doc/1
{
"region": "US",
"manager": {
"name": "Alice White",
"age": 30
},
"employees": [
{
"name": "John Smith",
"age": 34
},
{
"name": "Peter Brown",
"age": 26
}
]
} | Свойства в определении отображения верхнего уровня. | |
| Свойства в поле объекта | |
| Свойства в поле | |
| Пример документа, соответствующего вышеуказанному отображению. |
Настройка properties разрешает иметь разные настройки для полей с одинаковым именем в одном индексе. Новые свойства могут быть добавлены к существующим полям с помощью API обновления отображения.
Точечная нотация
В запросах, агрегациях и т.д. к внутренним полям можно обратиться, используя точечную нотацию:
resp = client.search(
index="my-index-000001",
query={
"match": {
"manager.name": "Alice White"
}
},
aggs={
"Employees": {
"nested": {
"path": "employees"
},
"aggs": {
"Employee Ages": {
"histogram": {
"field": "employees.age",
"interval": 5
}
}
}
}
},
)
print(resp) response = client.search(
index: 'my-index-000001',
body: {
query: {
match: {
'manager.name' => 'Alice White'
}
},
aggregations: {
"Employees": {
nested: {
path: 'employees'
},
aggregations: {
"Employee Ages": {
histogram: {
field: 'employees.age',
interval: 5
}
}
}
}
}
}
)
puts response const response = await client.search({
index: "my-index-000001",
query: {
match: {
"manager.name": "Alice White",
},
},
aggs: {
Employees: {
nested: {
path: "employees",
},
aggs: {
"Employee Ages": {
histogram: {
field: "employees.age",
interval: 5,
},
},
},
},
},
});
console.log(response); GET my-index-000001/_search
{
"query": {
"match": {
"manager.name": "Alice White"
}
},
"aggs": {
"Employees": {
"nested": {
"path": "employees"
},
"aggs": {
"Employee Ages": {
"histogram": {
"field": "employees.age",
"interval": 5
}
}
}
}
}
} Полный путь к внутреннему полю должен быть указан.
© 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/properties.html