首页 > 代码库 > Elasticsearch的PHP的API使用(一)

Elasticsearch的PHP的API使用(一)

前提:在服务器上安装Elasticsearch  (host:192.168.1.10)  

        http://192.168.1.10:9200?_search?pretty

 

1:安装PHP的Elasticsearch的的扩展(使用composer方法)

  1.1 下载composer.phar包(见composer安装软件  http://www.cnblogs.com/amuge/p/5998985.html)

      1.2 composer.json  

          {

    "require": {
        "elasticsearch/elasticsearch""~2.0@beta"
      }
    }

  1.3 php composer.phar install 会自动寻找composer.json这个配置文件,下载此的扩展。当下载完成后会在当前运行的目录下新添一个vendor目录(就表示成功下载elasticsearch-php扩展)

 

2:将vendor目录拷到项目的第三方的扩展目录下(比如:我是用YII框架。我将vendor拷到 application/protected/vendor/elasticsearch目录下)

 

3:测试

require_once( __DIR__ . ‘/../vendor/elasticsearch/autoload.php‘);
$hosts = array(‘192.168.1.10‘);
$client = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$client = $this->getElasticClient();
$params = array(
    ‘index‘ => ‘website‘,
    ‘type‘ => ‘blog‘,
    ‘body‘ => array(
        ‘query‘ => array(
            ‘match‘ => array(
                ‘_id‘ => 1,
           )
        )
    )
);
$rtn = $client->search($params);
echo ‘<pre>‘;
print_r($rtn);
echo ‘</pre>‘;
die(‘FILE:‘ . __FILE__ . ‘; LINE:‘ . __LINE__);

 

4:结果

技术分享

 

 

官方文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html

 

Elasticsearch的PHP的API使用(一)