ES|QL マルチバリューフィールド
ES|QL はマルチバリューフィールドからの読み取りが可能です:
Python
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)
Js
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);
Console
POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 1] }
{ "index" : {} }
{ "a": 2, "b": 3 }
POST /_query
{
"query": "FROM mv | LIMIT 2"
}
マルチバリューフィールドは JSON 配列として返されます:
Console-Result
{
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"}
],
"values": [
[1, [1, 2]],
[2, 3]
]
}
マルチバリューフィールド内の値の相対的な順序は未定義です。通常は昇順になりますが、それに依存しないでください。
重複値
keyword
のような一部のフィールドタイプは、書き込み時に重複値を削除します:
Python
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)
Js
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);
Console
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"
}
Console-Result
{
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "keyword"}
],
"values": [
[1, ["bar", "foo"]],
[2, "bar"]
]
}
しかし、long
のような他のタイプは重複を削除しません。
Python
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)
Js
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);
Console
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"
}
Console-Result
{
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "long"}
],
"values": [
[1, [1, 2, 2]],
[2, [1, 1]]
]
}
これはすべてストレージ層でのことです。重複した long
を保存し、それを文字列に変換すると、重複は残ります:
Python
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)
Js
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);
Console
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"
}
Console-Result
{
"columns": [
{ "name": "a", "type": "long"},
{ "name": "b", "type": "keyword"}
],
"values": [
[1, ["1", "2", "2"]],
[2, ["1", "1"]]
]
}
関数
特に文書化されていない限り、関数はマルチバリューフィールドに適用されると null
を返します。
Python
resp = client.bulk(
index="mv",
refresh=True,
operations=[
{
"index": {}
},
{
"a": 1,
"b": [
2,
1
]
},
{
"index": {}
},
{
"a": 2,
"b": 3
}
],
)
print(resp)
Ruby
response = client.bulk(
index: 'mv',
refresh: true,
body: [
{
index: {}
},
{
a: 1,
b: [
2,
1
]
},
{
index: {}
},
{
a: 2,
b: 3
}
]
)
puts response
Js
const response = await client.bulk({
index: "mv",
refresh: "true",
operations: [
{
index: {},
},
{
a: 1,
b: [2, 1],
},
{
index: {},
},
{
a: 2,
b: 3,
},
],
});
console.log(response);
Console
POST /mv/_bulk?refresh
{ "index" : {} }
{ "a": 1, "b": [2, 1] }
{ "index" : {} }
{ "a": 2, "b": 3 }
Python
resp = client.esql.query(
query="FROM mv | EVAL b + 2, a + b | LIMIT 4",
)
print(resp)
Js
const response = await client.esql.query({
query: "FROM mv | EVAL b + 2, a + b | LIMIT 4",
});
console.log(response);
Console
POST /_query
{
"query": "FROM mv | EVAL b + 2, a + b | LIMIT 4"
}
Console-Result
{
"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]
]
}
この制限を回避するには、フィールドを次のいずれかの単一値に変換します:
Python
resp = client.esql.query(
query="FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4",
)
print(resp)
Js
const response = await client.esql.query({
query: "FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4",
});
console.log(response);
Console
POST /_query
{
"query": "FROM mv | EVAL b=MV_MIN(b) | EVAL b + 2, a + b | LIMIT 4"
}
Console-Result
{
"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]
]
}