Агрегирование по проблеме значений из нескольких слов

Я строю многоуровневую навигацию на основе Elasticsearch, У моего продукта есть поле «бренд». Например, у меня есть 2 бренда — Tommy Jeans и Tommy Hilfiger. Когда я пытаюсь объединить результаты с помощью следующего запроса

$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'term'  => [
'brand' => 'tommy'
]
],
'aggs' => [
'brand' => [
'terms' => [
'field' => 'brand',
]
]
]
]
];

Я ожидаю 2 результата в скобках — Томми Хилфигер и Томми Джинс с подсчетом результатов, но в моем случае это примерно так

[aggregations] => Array
(
[brand] => Array
(
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => Array
(
[0] => Array
(
[key] => tommy
[doc_count] => 6
)

[1] => Array
(
[key] => hilfiger
[doc_count] => 4
)

[2] => Array
(
[key] => jeans
[doc_count] => 2
)

)

)

)

Как я могу решить это?

0

Решение

Это может быть достигнуто путем brand поле типа text и добавив подполе к нему сказать keyword с типом как keyword, Тогда вам нужно использовать term запрос по полю brand фильтровать результаты и агрегировать по полю brand.keyword

Таким образом, отображение будет:

{
"mappings": {
"_doc": {
"properties": {
"brand": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"}
}
}
}
}
}
}

Обновление для комментария: Отображение для более старой версии es (2.x):

{
"mappings": {
"_doc": {
"properties": {
"brand": {
"type": "string",
"fields": {
"keyword": {
"type": "string",
"index": "not_analyzed"}
}
}
}
}
}
}

И следующий будет запрос:

{
"query": {
"bool": {
"filter": [
{
"term": {
"brand": "tommy"}
}
]
}
},
"aggs": {
"brand": {
"terms": {
"field": "brand.keyword"}
}
}
}
1

Другие решения

Наконец-то я разобрался в своем выпуске. Это рабочий пример (основываясь на ответе Нишанта Саини)

use Elasticsearch\ClientBuilder;

require 'vendor/autoload.php';

echo '<pre>';

$client = ClientBuilder::create()->build();

$params = [
'index' => 'my_index',
'body' => [
'mappings' => [
'my_type' => [
'properties' => [
'brand' => [
'type'  => 'string',
'fields'    => [
'keyword'   => [
'type'  => 'string',
'index' => 'not_analyzed'
]
]
],
'color' => [
'type'  => 'string',
'fields'    => [
'keyword'   => [
'type'  => 'string',
'index' => 'not_analyzed'
]
]
],
'category' => [
'type'  => 'string',
'fields'    => [
'keyword'   => [
'type'  => 'string',
'index' => 'not_analyzed'
]
]
],
'id' => [
'type'  => 'integer',
]
]
]
]
]
];

$client->indices()->create($params);

$items = [
[
'id'        => 1,
'category'  => 'Jackets',
'brand'     => 'Tommy Hilfiger',
'color'     => 'Red'
],
[
'id'        => 2,
'category'  => 'Jeans',
'brand'     => 'Tommy Jeans',
'color'     => 'Navy'
],
[
'id'        => 3,
'category'  => 'Shirts',
'brand'     => 'Tommy Hilfiger',
'color'     => 'Maroon'
],
[
'id'        => 4,
'category'  => 'Trousers',
'brand'     => 'Tommy Jeans',
'color'     => 'Grey'
],
[
'id'        => 5,
'category'  => 'Shirts',
'brand'     => 'Tommy Hilfiger',
'color'     => 'Grey'
],
[
'id'        => 6,
'category'  => 'Sneakers',
'brand'     => 'Tommy Jeans',
'color'     => 'Grey'
],
[
'id'        => 7,
'category'  => 'Sneakers',
'brand'     => 'Tommy Jeans',
'color'     => 'Grey'
]
];

foreach ($items as $item) {
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => $item['id'],
'body' => [
'brand'             => $item['brand'],
'color'             => $item['color'],
'category'          => $item['category'],
]
];

$client->index($params);
}

$params = [
'index' => 'my_index',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'brand' => 'tommy' ] ],
[ 'match' => [ 'color' => 'grey' ] ]
]
]
],
'aggs' => [
'brands' => [
'terms' => [
'field' => 'brand.keyword',
],
],
'colors' => [
'terms' => [
'field' => 'color.keyword',
]
],
'categories' => [
'terms' => [
'field' => 'category.keyword',
]
]
]
]
];

$response = $client->search($params);
print_r($response);

И результат

Array
(
[brands] => Array
(
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => Array
(
[0] => Array
(
[key] => Tommy Jeans
[doc_count] => 3
)

[1] => Array
(
[key] => Tommy Hilfiger
[doc_count] => 1
)

)

)

[categories] => Array
(
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => Array
(
[0] => Array
(
[key] => Sneakers
[doc_count] => 2
)

[1] => Array
(
[key] => Shirts
[doc_count] => 1
)

[2] => Array
(
[key] => Trousers
[doc_count] => 1
)

)

)

[colors] => Array
(
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => Array
(
[0] => Array
(
[key] => Grey
[doc_count] => 4
)

)

)

)
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector