首页 » 大数据 » 正文

[ElasticSearch]PHP操作ElasticSearch

官方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();
    }
}</p>

<p>// 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'][&quot;size&quot;] = $size;
    // your aggs
    if (null != $agg) {
        $params['body'][&quot;aggs&quot;] = $agg;
    }
    $params['body'][&quot;query&quot;][&quot;filtered&quot;]['filter'][&quot;and&quot;] = [
        [&quot;term&quot;=&gt;['Your area' =&gt; $area]], // your area 
        [&quot;range&quot;=&gt;[&quot;Your time&quot; =&gt; array(&quot;gte&quot;=&gt;$start, &quot;lte&quot;=&gt;$end)]] // your time
    ];</p>

<pre><code>if ($keyword != '') {
    $params['body'][&amp;quot;query&amp;quot;][&amp;quot;filtered&amp;quot;]['query']['bool']['should']['match_phrase'] = array();// your query word
}

$response = BaseFrontController::$client-&amp;gt;search($params);
return $response;
</code></pre>

<p>}

本文共 2 个回复

  • yuantong 2018/05/02 15:14

    相当精彩的博客,羡慕哦!

  • risheng 2018/05/09 16:44

    一个博客能让人流连忘返,讲真,靠的是实力!

发表评论