使用“ aggs”在elasticsearch中使用GROUP BY,在PHP中对我不起作用

时间:2019-06-26 07:30:51

标签: php elasticsearch

我是Elasticsearch的新手,我想在Elasticsearch中使用GROUP BY等效项来获取从“过滤器”获得的结果。

我试图使用'ags'来做到这一点,但是我不确定如何做到。

$params = [
'index' => $table_name,
'type' => '_doc',
'size' => 4894,
'body' => [
 'query' => [
  'aggs' => [
   'some_key_to_group_by' => [
    'bool' => [
     'filter' => [
      ['terms' => ['key1' => $array1]],
      ['terms' => ['key2' => $array2]],
      ['terms' => ['key3' => $array3]],
      ['terms' => ['key4' => $array4]]
        ]
       ]
      ]
     ]
    ]
   ]
];

我已经得到了关于任何东西都没有分组的结果, 现在我希望得到一个分组的响应,但是回显的结果是这样的……

{"error":{"root_cause":[{"type":"parsing_exception","reason":"no  
[query] registered for [aggs]","line":1,"col":18}]

1 个答案:

答案 0 :(得分:1)

似乎您正在混合查询和聚合。 aggs部分是query的同胞,而不是孩子的同胞:

这应该有效:

$params = [
'index' => $table_name,
'type' => '_doc',
'size' => 0,
'body' => [
 'query' => [
  'bool' => [
   'filter' => [
    ['terms' => ['key1' => $array1]],
    ['terms' => ['key2' => $array2]],
    ['terms' => ['key3' => $array3]],
    ['terms' => ['key4' => $array4]]
   ]
  ]
 ],
 'aggs' => [
   'some_key_to_group_by' => [
      'terms' => ['field' => 'key1', 'size' => 100]
   ]
 ]
]
];