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 =  Elasticsearch\ClientBuilder::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__);

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM