ネストされた集約
ネストされたドキュメントを集約するための特別な単一バケット集約です。
例えば、製品のインデックスがあり、各製品がリセラーのリストを保持しているとしましょう - 各リセラーは製品の価格を持っています。マッピングは次のようになります:
Python
resp = client.indices.create(
index="products",
mappings={
"properties": {
"resellers": {
"type": "nested",
"properties": {
"reseller": {
"type": "keyword"
},
"price": {
"type": "double"
}
}
}
}
},
)
print(resp)
Ruby
response = client.indices.create(
index: 'products',
body: {
mappings: {
properties: {
resellers: {
type: 'nested',
properties: {
reseller: {
type: 'keyword'
},
price: {
type: 'double'
}
}
}
}
}
}
)
puts response
Js
const response = await client.indices.create({
index: "products",
mappings: {
properties: {
resellers: {
type: "nested",
properties: {
reseller: {
type: "keyword",
},
price: {
type: "double",
},
},
},
},
},
});
console.log(response);
コンソール
PUT /products
{
"mappings": {
"properties": {
"resellers": {
"type": "nested",
"properties": {
"reseller": {
"type": "keyword"
},
"price": {
"type": "double"
}
}
}
}
}
}
resellers はネストされたドキュメントを保持する配列です。 |
次のリクエストは、2つのリセラーを持つ製品を追加します:
Python
resp = client.index(
index="products",
id="0",
refresh=True,
document={
"name": "LED TV",
"resellers": [
{
"reseller": "companyA",
"price": 350
},
{
"reseller": "companyB",
"price": 500
}
]
},
)
print(resp)
Ruby
response = client.index(
index: 'products',
id: 0,
refresh: true,
body: {
name: 'LED TV',
resellers: [
{
reseller: 'companyA',
price: 350
},
{
reseller: 'companyB',
price: 500
}
]
}
)
puts response
Js
const response = await client.index({
index: "products",
id: 0,
refresh: "true",
document: {
name: "LED TV",
resellers: [
{
reseller: "companyA",
price: 350,
},
{
reseller: "companyB",
price: 500,
},
],
},
});
console.log(response);
コンソール
PUT /products/_doc/0?refresh
{
"name": "LED TV",
"resellers": [
{
"reseller": "companyA",
"price": 350
},
{
"reseller": "companyB",
"price": 500
}
]
}
name 属性に対して動的マッピングを使用しています。 |
次のリクエストは、製品が購入できる最小価格を返します:
Python
resp = client.search(
index="products",
size="0",
query={
"match": {
"name": "led tv"
}
},
aggs={
"resellers": {
"nested": {
"path": "resellers"
},
"aggs": {
"min_price": {
"min": {
"field": "resellers.price"
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'products',
size: 0,
body: {
query: {
match: {
name: 'led tv'
}
},
aggregations: {
resellers: {
nested: {
path: 'resellers'
},
aggregations: {
min_price: {
min: {
field: 'resellers.price'
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "products",
size: 0,
query: {
match: {
name: "led tv",
},
},
aggs: {
resellers: {
nested: {
path: "resellers",
},
aggs: {
min_price: {
min: {
field: "resellers.price",
},
},
},
},
},
});
console.log(response);
コンソール
GET /products/_search?size=0
{
"query": {
"match": {
"name": "led tv"
}
},
"aggs": {
"resellers": {
"nested": {
"path": "resellers"
},
"aggs": {
"min_price": {
"min": {
"field": "resellers.price"
}
}
}
}
}
}
上記のように、ネストされた集約は、最上位のドキュメント内のネストされたドキュメントの path
を必要とします。これにより、これらのネストされたドキュメントに対して任意のタイプの集約を定義できます。
コンソール-結果
{
...
"aggregations": {
"resellers": {
"doc_count": 2,
"min_price": {
"value": 350.0
}
}
}
}
特定のリセラーの結果を返すために、filter
サブ集約を使用できます。
Python
resp = client.search(
index="products",
size="0",
query={
"match": {
"name": "led tv"
}
},
aggs={
"resellers": {
"nested": {
"path": "resellers"
},
"aggs": {
"filter_reseller": {
"filter": {
"bool": {
"filter": [
{
"term": {
"resellers.reseller": "companyB"
}
}
]
}
},
"aggs": {
"min_price": {
"min": {
"field": "resellers.price"
}
}
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'products',
size: 0,
body: {
query: {
match: {
name: 'led tv'
}
},
aggregations: {
resellers: {
nested: {
path: 'resellers'
},
aggregations: {
filter_reseller: {
filter: {
bool: {
filter: [
{
term: {
'resellers.reseller' => 'companyB'
}
}
]
}
},
aggregations: {
min_price: {
min: {
field: 'resellers.price'
}
}
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "products",
size: 0,
query: {
match: {
name: "led tv",
},
},
aggs: {
resellers: {
nested: {
path: "resellers",
},
aggs: {
filter_reseller: {
filter: {
bool: {
filter: [
{
term: {
"resellers.reseller": "companyB",
},
},
],
},
},
aggs: {
min_price: {
min: {
field: "resellers.price",
},
},
},
},
},
},
},
});
console.log(response);
コンソール
GET /products/_search?size=0
{
"query": {
"match": {
"name": "led tv"
}
},
"aggs": {
"resellers": {
"nested": {
"path": "resellers"
},
"aggs": {
"filter_reseller": {
"filter": {
"bool": {
"filter": [
{
"term": {
"resellers.reseller": "companyB"
}
}
]
}
},
"aggs": {
"min_price": {
"min": {
"field": "resellers.price"
}
}
}
}
}
}
}
}
コンソール-結果
{
...
"aggregations": {
"resellers": {
"doc_count": 2,
"filter_reseller": {
"doc_count": 1,
"min_price": {
"value": 500.0
}
}
}
}
}