多词值聚合问题

vlju58qv  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(289)

我正在构建基于 Elasticsearch . 我的产品有“品牌”字段。例如,我有两个品牌-汤米牛仔裤和汤米希尔菲格。当我尝试使用以下查询聚合结果时

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

我期待两个结果在括号里-汤米希尔菲格和汤米牛仔裤与结果计数,但在我的情况下,它是这样的东西

[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
                            )

                    )

            )

    )

我怎样才能解决这个问题?

lrpiutwd

lrpiutwd1#

最后,我明白了我的问题。这是一个有效的例子(基于nishant saini的回答)

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
                        )

                )

        )

)
qpgpyjmq

qpgpyjmq2#

这可以通过 brand 类型字段 text 再加上一个子字段 keyword 类型为 keyword . 那你需要用 term 字段查询 brand 筛选结果并在字段上聚合 brand.keyword 因此,Map将是:

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

更新以征求意见:旧版本es(2.x)的Map:

{
  "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"
      }
    }
  }
}

相关问题