美文网首页
PHP操作ElasticSearch

PHP操作ElasticSearch

作者: Cesium中文网 | 来源:发表于2018-08-27 10:33 被阅读0次

    参考链接:
    http://blog.coinidea.com/elasticsearch-1275.html

    官方API:
    可以下载官方的PHP ElasticSearch API

    阅读文档进行相关操作。
    示例代码:

    static public function initSearchClient() {
        if (null ==  BaseFrontController::$client) {
            $hosts['hosts'] = array(
                "host" => '127.0.0.1',
                "port" => '9200',
                'scheme' => 'http'
            );
            BaseFrontController::$client = \Elasticsearch\ClientBuilder::create()
                ->setSSLVerification(false)
                ->setHosts($hosts)
                ->build();
        }
    }
     
    // Elastic search
    static public function actionSearch($area, $keyword, $start, $end, $size=100, $agg = null) {
        BaseFrontController::initSearchClient();
        $params['index'] = BaseFrontController::getSearchIndex();
        $params['type'] = 'raw';
        $params['body']["size"] = $size;
        // your aggs
        if (null != $agg) {
            $params['body']["aggs"] = $agg;
        }
        $params['body']["query"]["filtered"]['filter']["and"] = [
            ["term"=>['Your area' => $area]], // your area 
            ["range"=>["Your time" => array("gte"=>$start, "lte"=>$end)]] // your time
        ];
     
        if ($keyword != '') {
            $params['body']["query"]["filtered"]['query']['bool']['should']['match_phrase'] = array();// your query word
        }
     
        $response = BaseFrontController::$client->search($params);
        return $response;
    }
    

    参考链接:
    http://blog.coinidea.com/elasticsearch-1275.html

    相关文章

      网友评论

          本文标题:PHP操作ElasticSearch

          本文链接:https://www.haomeiwen.com/subject/skrziftx.html