dougu1952
2015-03-13 11:12
浏览 147
已采纳

php中的Elasticsearch聚合

I am writing Elasticsearch aggregations queries to find the total count available:

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

And I am getting the count properly. How do i write these type of queries in php code?? As i am new to elasticsearch any help would be helpful Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

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

   GET zap / _search 
 {
“”聚合“:{
”Brand_Name_Count“:{
”terms“:{”field“:”brand_name“,”size“:0} 
},\  n“Stock_Status_Count”:{
“terms”:{“field”:“stock_status”,“size”:50} 
},
“Category_Id_Count”:{
“terms”:{“field”:“  category_id“,”size“:50} 
} 
} 
} 
   
 
 

我正在计算正确的数量。 如何编写这些类型 php代码中的查询?? 作为我对elasticsearch的新手,任何帮助都会有所帮助 谢谢提前

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dt2015 2015-03-13 11:18
    已采纳

    Taking idea from github. The agg (and search) PHP syntax follows the JSON API 1:1. So you can take your aggregation above and just translate it into PHP arrays like so:

                $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);
    

    Aggregations are executed in parallel to searches, so just specify your search query and you'll get back a search hits element as well as the aggs element

    打赏 评论

相关推荐 更多相似问题