ElasticSearch-PHP的API使用(二)

本文介绍了如何使用PHP客户端操作Elasticsearch,包括文档的创建、两种查询方式(get和search)的具体实现。通过实例展示了如何插入文档、获取单条记录及进行匹配查询。

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__);

 

 

 

转载于:https://www.cnblogs.com/amuge/p/6076232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值