フィルター集約
単一のバケット集約で、クエリに一致するドキュメントのセットを絞り込みます。
例:
Python
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
aggs={
"avg_price": {
"avg": {
"field": "price"
}
},
"t_shirts": {
"filter": {
"term": {
"type": "t-shirt"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
aggregations: {
avg_price: {
avg: {
field: 'price'
}
},
t_shirts: {
filter: {
term: {
type: 't-shirt'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "sales",
size: 0,
filter_path: "aggregations",
aggs: {
avg_price: {
avg: {
field: "price",
},
},
t_shirts: {
filter: {
term: {
type: "t-shirt",
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
},
});
console.log(response);
コンソール
POST /sales/_search?size=0&filter_path=aggregations
{
"aggs": {
"avg_price": { "avg": { "field": "price" } },
"t_shirts": {
"filter": { "term": { "type": "t-shirt" } },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}
前の例では、すべての販売の平均価格とすべてのTシャツ販売の平均価格を計算します。
コンソール-結果
{
"aggregations": {
"avg_price": { "value": 140.71428571428572 },
"t_shirts": {
"doc_count": 3,
"avg_price": { "value": 128.33333333333334 }
}
}
}
すべての集約を制限するためのトップレベルクエリの使用
検索で実行されるすべての集約のドキュメントを制限するには、トップレベルのquery
を使用します。これは、サブ集約を持つ単一のfilter
集約よりも高速です。
たとえば、これを使用します:
Python
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
query={
"term": {
"type": "t-shirt"
}
},
aggs={
"avg_price": {
"avg": {
"field": "price"
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
query: {
term: {
type: 't-shirt'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "sales",
size: 0,
filter_path: "aggregations",
query: {
term: {
type: "t-shirt",
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
});
console.log(response);
コンソール
POST /sales/_search?size=0&filter_path=aggregations
{
"query": { "term": { "type": "t-shirt" } },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
これの代わりに:
Python
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
aggs={
"t_shirts": {
"filter": {
"term": {
"type": "t-shirt"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
aggregations: {
t_shirts: {
filter: {
term: {
type: 't-shirt'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "sales",
size: 0,
filter_path: "aggregations",
aggs: {
t_shirts: {
filter: {
term: {
type: "t-shirt",
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
},
});
console.log(response);
コンソール
POST /sales/_search?size=0&filter_path=aggregations
{
"aggs": {
"t_shirts": {
"filter": { "term": { "type": "t-shirt" } },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}
複数のフィルターのためのフィルター集約の使用
複数のフィルターを使用してドキュメントをグループ化するには、filters
集約を使用します。これは、複数のfilter
集約よりも高速です。
たとえば、これを使用します:
Python
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
aggs={
"f": {
"filters": {
"filters": {
"hats": {
"term": {
"type": "hat"
}
},
"t_shirts": {
"term": {
"type": "t-shirt"
}
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
aggregations: {
f: {
filters: {
filters: {
hats: {
term: {
type: 'hat'
}
},
t_shirts: {
term: {
type: 't-shirt'
}
}
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "sales",
size: 0,
filter_path: "aggregations",
aggs: {
f: {
filters: {
filters: {
hats: {
term: {
type: "hat",
},
},
t_shirts: {
term: {
type: "t-shirt",
},
},
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
},
});
console.log(response);
コンソール
POST /sales/_search?size=0&filter_path=aggregations
{
"aggs": {
"f": {
"filters": {
"filters": {
"hats": { "term": { "type": "hat" } },
"t_shirts": { "term": { "type": "t-shirt" } }
}
},
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}
これの代わりに:
Python
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
aggs={
"hats": {
"filter": {
"term": {
"type": "hat"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"t_shirts": {
"filter": {
"term": {
"type": "t-shirt"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
},
)
print(resp)
Ruby
response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
aggregations: {
hats: {
filter: {
term: {
type: 'hat'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
},
t_shirts: {
filter: {
term: {
type: 't-shirt'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
}
}
)
puts response
Js
const response = await client.search({
index: "sales",
size: 0,
filter_path: "aggregations",
aggs: {
hats: {
filter: {
term: {
type: "hat",
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
t_shirts: {
filter: {
term: {
type: "t-shirt",
},
},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
},
});
console.log(response);
コンソール
POST /sales/_search?size=0&filter_path=aggregations
{
"aggs": {
"hats": {
"filter": { "term": { "type": "hat" } },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
},
"t_shirts": {
"filter": { "term": { "type": "t-shirt" } },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}