Elasticsearch 隨機查詢(抽樣查詢)PHP 使用方法

    public function getTopicList($query,$size=1000,$page=0,$field=null,$IS_RANDOM){
        $params = [
            'size'      => $size,
            'index'     => 'mokao_bank_2',
            'type'      => 'mokao_bank_2',
            'body'      => [
                'query' => $query
            ]
        ];

        //查詢條件query如下
        //$query = [
        //    'bool' => [
        //        'must' => [
        //            ['match' => [ 'subject_category_id' => $subject ]],
        //            ['match' => [ 'subject_type_id'     => $topicType ]],
        //            ['match' => [ 'is_hid'              => 0 ]],
        //            ['match' => [ 'is_del'              => 0 ]],
        //        ]
        //    ]
        //];

        if($field){
            $params['_source'] = $field;
        }

        if($page > 0){
            $from = ($page - 1) * $size;
            $params['from'] = $from;
        }
        $params['body']['sort']=[
            '_id'   => [
                'order' => 'asc'
            ]
        ];
        
        //抽樣查詢時排序替換爲如下條件
        if($IS_RANDOM){
            $params['body']['sort'] = [
                '_script' => [
                    'script' => 'Math.random()',
                    'type'   => 'number',
                    'order'  => 'asc'
                ]
            ];
        }
        $return = $this->client->search($params);
        return $return['hits']['hits'];
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章