ElasticSearch-PHP的API使用(二)

1:索引内的一个文档的创建(相当表记录的添加)

比如:要添加一条记录 INSERT INTO blog(title,content,add_time) VALUES('ElasticSearch-PHP之使用二','有关于ElasticSearch在PHP下的扩展使用方法之谈','2016-11-18')

require_once( __DIR__ . '/../vendor/elasticsearch/autoload.php');
$hosts = Yii::app()->params['extra']['elasticsearch']['hosts'];  //array('192.168.1.10')     
$client =  ElasticsearchClientBuilder::create()->setHosts($hosts)->build();
$params = array(
            'index' => 'website',
            'type' => 'blog',
            'id' => 7,
            'body' => array(
                'title' => 'ElasticSearch-PHP之使用二',
                'content' => '有关于ElasticSearch在PHP下的扩展使用方法之谈',
                'create_time' => '2016-11-18 08:00:00',
            ),
        );
$resp = $client->index($params);
echo '<pre>';
print_r($resp);
echo '</pre>';
die('FILE:' . __FILE__ . '; LINE:' . __LINE__);

2:数据查询一(get)

        $client = $this->getElasticClient();
        $params = array(
            'index' => $this->_index,
            'type' => $this->_type,
            'id' => Yii::app()->request->getParam('id', 1),
        );
        try {
            $resp = $client->get($params);
        } catch (Exception $ex) {
            $resp = $ex->getMessage();
        }

3:数据查询二(search) 

        $client = $this->getElasticClient();
        $params = array(
            'index' => $this->_index,
            'type' => $this->_type,
            'body' => array(
                'query' => array(
                    'match' => array(
                        'title' => 'elasticsearch php extends'
                    ),
                ),
            ),
        );
        try {
            $resp = $client->search($params);
        } catch (Exception $ex) {
            $resp = $ex->getMessage();
        }

        echo '<pre>';
        print_r($resp);
        echo '</pre>';
        die('FILE:' . __FILE__ . '; LINE:' . __LINE__);

 

原文地址:https://www.cnblogs.com/amuge/p/6076232.html