php 中的 Elasticsearch 聚合

Elasticsearch aggregations in php

我正在编写 Elasticsearch 聚合查询来查找可用的总数:

  GET zap/_search
  {
   "aggregations": {
   "Brand_Name_Count": {
     "terms": {"field": "brand_name", "size" : 0}
         },
   "Stock_Status_Count" : {
      "terms" : { "field" : "stock_status", "size" : 50}
         },
   "Category_Id_Count" : {
       "terms" : { "field" : "category_id", "size" : 50}
         }
        }
      }

而且我得到了正确的计数。 我如何在 php 代码中编写这些类型的查询? 由于我是 elasticsearch 的新手,任何帮助都会有所帮助 提前致谢

借鉴 github 的想法。 agg(和搜索)PHP 语法遵循 JSON API 1:1。所以你可以把你的聚合放在上面,然后把它翻译成 PHP 数组,像这样:

$myQuery = [];  // Your query goes here

$params = [
    'index' => 'zap',
    'body' => [
        'aggs' => [
            'Brand_Name_Count' => [
                'terms' => [
                    'field' => 'brand_name',
                    'size' => 0
                ]
            ],
            'Stock_Status_Count' => [
                'terms' => [
                    'field' => 'stock_status',
                    'size' => 50
                ]
            ],
            'Category_Id_Count' => [
                'terms' => [
                    'field' => 'category_id',
                    'size' => 50
                ]
            ]
        ],
        'query' => $myQuery
    ]
];

$results = $client->search($params);

聚合与搜索并行执行,因此只需指定您的搜索查询,您就会得到搜索命中元素和聚合元素