Агрегация фильтрации
Агрегация по одной корзине, которая сужает набор документов до тех, что соответствуют запросу запроса.
Пример:
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) 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 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" } }
}
}
}
} В предыдущем примере вычисляется средняя цена всех продаж, а также средняя цена всех продаж футболок.
Ответ:
{
"aggregations": {
"avg_price": { "value": 140.71428571428572 },
"t_shirts": {
"doc_count": 3,
"avg_price": { "value": 128.33333333333334 }
}
}
} Использование верхнего уровня query для ограничения всех агрегаций
Для ограничения документов, на которых выполняются все агрегации в поиске, используйте верхний уровень query. Это быстрее, чем одна агрегация filter с под-агрегациями.
Например, используйте это:
resp = client.search(
index="sales",
size="0",
filter_path="aggregations",
query={
"term": {
"type": "t-shirt"
}
},
aggs={
"avg_price": {
"avg": {
"field": "price"
}
}
},
)
print(resp) response = client.search(
index: 'sales',
size: 0,
filter_path: 'aggregations',
body: {
query: {
term: {
type: 't-shirt'
}
},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
}
)
puts response 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" } }
}
} Вместо этого:
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) 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 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 для нескольких фильтров
Для группировки документов с помощью нескольких фильтров используйте filters агрегацию. Это быстрее, чем несколько filter агрегаций.
Например, используйте это:
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) 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 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" } }
}
}
}
} Вместо этого:
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) 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 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" } }
}
}
}
}
© 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/search-aggregations-bucket-filter-aggregation.html