栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

es查询语法

es查询语法

语法

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号