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

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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word