es查询语法
程序员文章站
2022-07-06 15:46:35
...
语法
/**
* ES聚合方法,按照聚合结果排序
* @param $index
* @param $aggsField
* @param array $timeRange
* @param string $aggsFieldChild
* @param int $num
* @param array $condition
* @param array $mustExist
* @param string $distinctField
* @param array $include
* @param array $aggsOrder
* @param array $filterFields
* @return mixed
* @throws Beidou_Util_Exception
*/
public function multinestAggs($index, $aggsField, $timeRange = [], $aggsFieldChild = '', $num = 10, $condition =
[], $mustExist = [], $distinctField = "", $include = [], $aggsOrder = Service_Data_Statistics_EsConstant::DEFAULT_ORDER,
$filterFields = [])
{
if (empty($index) || empty($aggsField)) {
throw new Beidou_Util_Exception(
Beidou_Util_ExceptionCodes::PARAM_ERROR,
"索引、聚合字段必传"
);
}
// 时间范围
if (!empty($timeRange)) {
$query['bool']['filter'][]['range']['logTime'] = array(
'gte' => $timeRange['startTime'],
'lte' => $timeRange['endTime']
);
}
// 过滤需要的字段
if (!empty($filterFields)) {
$query['bool']['filter'][]['terms'] = $filterFields;
}
// 索引范围
$params['index'] = $index;
// 筛选条件
if (!empty($condition)) {
foreach ($condition as $key => $value) {
$query['bool']['must'][]['match_phrase'] = array(
$key => $value
);
}
}
//
// $query['bool']['filter'][]['terms'] = ["httpRefer.keyword" => Service_Data_Statistics_EsConstant::BEIDOU_INCLUDE];
// 结果筛选
if (!empty($mustExist)) {
foreach ($mustExist as $value) {
$query['bool']['must'][]['exists'] = array(
'field' => $value
);
}
}
if (!empty($query)) {
$params['query'] = $query;
}
// 包含子查询
if (!empty($aggsFieldChild)) {
$params['aggs']['groupByCondition'] = [
'terms' => [
'field' => $aggsField,
'size' => $num
],
'aggs' => [
'groupByChild' => [
'terms' => [
'field' => $aggsFieldChild,
'size' => $num,
'order' => $aggsOrder,
]
]
]
];
// 对子查询去重;得到uv值
if (!empty($distinctField)) {
$params['aggs']['groupByCondition']['aggs']['groupByChild']['aggs'] = [
'distinct_name' => [
'cardinality' => [
'field' => $distinctField,
]
]
];
}
} else {
// 聚合字段
$params['aggs']['groupByCondition']['terms'] = [
'field' => $aggsField,
'size' => $num,
'order' => $aggsOrder
];
// 去重;得到uv值
if (!empty($distinctField)) {
$params['aggs']['groupByCondition']['aggs'] = [
'distinct_name' => [
'cardinality' => [
'field' => $distinctField,
]
]
];
}
}
// 去除部分列
if (!empty($include)) {
$params['aggs']['groupByCondition']['terms']['include'] = $include;
}
// 详细内容
$params['size'] = 0;
$ret = $this->esClient->search($params);
return $ret;
}
/**
* 统计直接通过服务器发起的请求
* @param $index
* @param $aggsField
* @param $aggsFieldChild
* @param $num
* @param $mustExist
* @param $aggsOrder
* @return mixed
* @throws Beidou_Util_Exception
*/
public function nginxLogAggs($index, $aggsField, $aggsFieldChild, $num, $mustExist, $aggsOrder)
{
if (empty($index) || empty($aggsField)) {
throw new Beidou_Util_Exception(
Beidou_Util_ExceptionCodes::PARAM_ERROR,
"索引、聚合字段必传"
);
}
// 索引范围
$params['index'] = $index;
// 结果筛选
if (!empty($mustExist)) {
foreach ($mustExist as $value) {
$query['bool']['must'][]['exists'] = array(
'field' => $value
);
}
}
if (!empty($query)) {
$params['query'] = $query;
}
// 包含子查询
if (!empty($aggsFieldChild)) {
$params['aggs']['groupByCondition'] = [
'terms' => [
'field' => $aggsField,
'size' => $num
],
'aggs' => [
'groupByChild' => [
'terms' => [
'field' => $aggsFieldChild,
'size' => $num,
'order' => $aggsOrder,
]
]
]
];
} else {
// 聚合字段
$params['aggs']['groupByCondition']['terms'] = [
'field' => $aggsField,
'size' => $num,
'order' => $aggsOrder
];
}
// 详细内容
$params['size'] = 0;
$ret = $this->esClient->search($params);
return $ret;
}