Поля ES|QL с несколькими значениями
ES|QL умеет читать данные из полей с несколькими значениями:
resp = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
2,
1
]
},
{
"index": {}
},
{
"a": 2,
"b": 3
}
],
)
print(resp)
resp1 = client.esql.query(
query="FROM mv | LIMIT 2",
)
print(resp1) const response = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: [2, 1],
},
{
index: {},
},
{
a: 2,
b: 3,
},
],
});
console.log(response);
const response1 = await client.esql.query({
query: "FROM mv | LIMIT 2",
});
console.log(response1); POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 1] }
{ "index" : {} }
{ "a": 2, "b": 3 }
POST /_query
{
"query": "FROM mv | LIMIT 2"
} Поля с несколькими значениями возвращаются в виде JSON-массива:
{
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"}
],
"values": [
[1, [1, 2]],
[2, 3]
]
} Относительный порядок значений в поле с несколькими значениями не определён. Часто они упорядочены по возрастанию, но не полагайтесь на это.
Дублирующие значения
Некоторые типы полей, например, keyword, удаляют дублирующие значения при записи:
resp = client.indices.create(
index="mv",
mappings={
"properties": {
"b": {
"type": "keyword"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
"foo",
"foo",
"bar"
]
},
{
"index": {}
},
{
"a": 2,
"b": [
"bar",
"bar"
]
}
],
)
print(resp1)
resp2 = client.esql.query(
query="FROM mv | LIMIT 2",
)
print(resp2) const response = await client.indices.create({
index: "mv",
mappings: {
properties: {
b: {
type: "keyword",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: ["foo", "foo", "bar"],
},
{
index: {},
},
{
a: 2,
b: ["bar", "bar"],
},
],
});
console.log(response1);
const response2 = await client.esql.query({
query: "FROM mv | LIMIT 2",
});
console.log(response2); PUT /mv
{
"mappings": {
"properties": {
"b": {"type": "keyword"}
}
}
}
POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": ["foo", "foo", "bar"] }
{ "index" : {} }
{ "a": 2, "b": ["bar", "bar"] }
POST /_query
{
"query": "FROM mv | LIMIT 2"
} И ES|QL видит это удаление:
{
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "keyword"}
],
"values": [
[1, ["bar", "foo"]],
[2, "bar"]
]
} Но другие типы, например, long, не удаляют дубликаты.
resp = client.indices.create(
index="mv",
mappings={
"properties": {
"b": {
"type": "long"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
2,
2,
1
]
},
{
"index": {}
},
{
"a": 2,
"b": [
1,
1
]
}
],
)
print(resp1)
resp2 = client.esql.query(
query="FROM mv | LIMIT 2",
)
print(resp2) const response = await client.indices.create({
index: "mv",
mappings: {
properties: {
b: {
type: "long",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: [2, 2, 1],
},
{
index: {},
},
{
a: 2,
b: [1, 1],
},
],
});
console.log(response1);
const response2 = await client.esql.query({
query: "FROM mv | LIMIT 2",
});
console.log(response2); PUT /mv
{
"mappings": {
"properties": {
"b": {"type": "long"}
}
}
}
POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 2, 1] }
{ "index" : {} }
{ "a": 2, "b": [1, 1] }
POST /_query
{
"query": "FROM mv | LIMIT 2"
} И ES|QL тоже это видит:
{
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"}
],
"values": [
[1, [1, 2, 2]],
[2, [1, 1]]
]
} Всё это происходит на уровне хранения. Если вы сохраняете дубликаты `long` и затем преобразуете их в строки, дубликаты сохранятся:
resp = client.indices.create(
index="mv",
mappings={
"properties": {
"b": {
"type": "long"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
2,
2,
1
]
},
{
"index": {}
},
{
"a": 2,
"b": [
1,
1
]
}
],
)
print(resp1)
resp2 = client.esql.query(
query="FROM mv | EVAL b=TO_STRING(b) | LIMIT 2",
)
print(resp2) const response = await client.indices.create({
index: "mv",
mappings: {
properties: {
b: {
type: "long",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: [2, 2, 1],
},
{
index: {},
},
{
a: 2,
b: [1, 1],
},
],
});
console.log(response1);
const response2 = await client.esql.query({
query: "FROM mv | EVAL b=TO_STRING(b) | LIMIT 2",
});
console.log(response2); PUT /mv
{
"mappings": {
"properties": {
"b": {"type": "long"}
}
}
}
POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 2, 1] }
{ "index" : {} }
{ "a": 2, "b": [1, 1] }
POST /_query
{
"query": "FROM mv | EVAL b=TO_STRING(b) | LIMIT 2"
} {
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "keyword"}
],
"values": [
[1, ["1", "2", "2"]],
[2, ["1", "1"]]
]
}
null в списке
null значения в списке не сохраняются на уровне хранения:
resp = client.index(
index="mv",
refresh=True,
document={
"a": [
2,
None,
1
]
},
)
print(resp)
resp1 = client.esql.query(
query="FROM mv | LIMIT 1",
)
print(resp1) const response = await client.index({
index: "mv",
refresh: "true",
document: {
a: [2, null, 1],
},
});
console.log(response);
const response1 = await client.esql.query({
query: "FROM mv | LIMIT 1",
});
console.log(response1); POST /mv/_doc?refresh
{ "a": [2, null, 1] }
POST /_query
{
"query": "FROM mv | LIMIT 1"
} {
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
],
"values": [
[[1, 2]],
]
} Функции
Если не указано иное в документации, функции будут возвращать null при применении к полю с несколькими значениями.
resp = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
2,
1
]
},
{
"index": {}
},
{
"a": 2,
"b": 3
}
],
)
print(resp) response = client.bulk(
index: 'mv',
refresh: true,
body: [
{
index: {}
},
{
a: 1,
b: [
2,
1
]
},
{
index: {}
},
{
a: 2,
b: 3
}
]
)
puts response const response = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: [2, 1],
},
{
index: {},
},
{
a: 2,
b: 3,
},
],
});
console.log(response); POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 1] }
{ "index" : {} }
{ "a": 2, "b": 3 } resp = client.esql.query(
query="FROM mv | EVAL b + 2, a + b | LIMIT 4",
)
print(resp) const response = await client.esql.query({
query: "FROM mv | EVAL b + 2, a + b | LIMIT 4",
});
console.log(response); POST /_query
{
"query": "FROM mv | EVAL b + 2, a + b | LIMIT 4"
} {
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"},
{ "name": "b + 2", "type": "long"},
{ "name": "a + b", "type": "long"}
],
"values": [
[1, [1, 2], null, null],
[2, 3, 5, 5]
]
} Обход этого ограничения путём преобразования поля в одно значение с использованием одного из:
resp = client.esql.query(
query="FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4",
)
print(resp) const response = await client.esql.query({
query: "FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4",
});
console.log(response); POST /_query
{
"query": "FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4"
} {
"took": 28,
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"},
{ "name": "b + 2", "type": "long"},
{ "name": "a + b", "type": "long"}
],
"values": [
[1, 1, 3, 2],
[2, 3, 5, 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/esql-multivalued-fields.html