php中的Elasticsearch聚合

zap*_*p92 3 php elasticsearch

我正在编写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}
         }
        }
      }
Run Code Online (Sandbox Code Playgroud)

我正在得到正确的计数.我如何在PHP代码中编写这些类型的查询?由于我是elasticsearch的新手,任何帮助都会有所帮助

Jug*_*ngh 6

从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);
Run Code Online (Sandbox Code Playgroud)

聚合与搜索并行执行,因此只需指定搜索查询,您将获得搜索命中元素以及aggs元素