Elasticsearch调用方法

ElasticSearch全文搜索引擎 ,是一款很好的搜索框架!用来做网站的搜索,可以对数据库压力起到一定的缓解作用!
之前给大家带来的是用curl来实现的大家有兴趣可以看看 
http://www.thinkphp.cn/topic/29297.html
今天这里给大家带来的是官网提供的 PHP API

整合thinkphp3.2


下载文件解压到 

ThinkPHP\Library\Vendor\

<?php
namespace Home\Controller;

use Think\Controller;

class EcController extends Controller
{

    public function _initialize()
    {
        Vendor('Elasticsearch.autoload');
        $params['hosts'] = array(
            '127.0.0.1:9200'
        );
        $this->client = new \Elasticsearch\Client($params);
    }

    public function create_index()
    {
        $indexParams['index'] = 'my_index';
        $indexParams['type'] = 'my_index';
        $indexParams['body']['settings']['number_of_shards'] = 2;
        $indexParams['body']['settings']['number_of_replicas'] = 0;
        $this->client->create($indexParams);
    }

    public function add_document()
    {
        $params = array();
        $params['body'] = array(
            'testField' => 'dfdsfdsf'
        );
        $params['index'] = 'my_index';
        $params['type'] = 'my_index';
        $params['id'] = 'w1231313';
        $ret = $this->client->index($params);
    }

    public function delete_index()
    {
        $deleteParams['index'] = 'my_index';
        $this->client->indices()->delete($deleteParams);
    }

    public function delete_document()
    {
        $deleteParams = array();
        $deleteParams['index'] = 'my_index';
        $deleteParams['type'] = 'my_index';
        $deleteParams['id'] = 'AU4Kmmj-WOmOrmyOj2qf';
        $retDelete = $this->client->delete($deleteParams);
    }
    public function update_document()
    {
        $updateParams = array();
        $updateParams['index'] = 'my_index';
        $updateParams['type'] = 'my_index';
        $updateParams['id'] = 'my_id';
        $updateParams['body']['doc']['asas']  = '111111';
       $response = $this->client->update($updateParams);
         
    }
    public function search()
    {
        $searchParams['index'] = 'my_index';
        $searchParams['type'] = 'my_index';
        $searchParams['from'] = 0;
        $searchParams['size'] = 100;
        $searchParams['sort'] = array(
            '_score' => array(
                'order' => 'desc'
            )
        );
        // $searchParams['body']['query']['match']['testField'] = 'abc';
        $retDoc = $this->client->search($searchParams);
        print_r($retDoc);
    }

    public function get_document()
    {
        $getParams = array();
        $getParams['index'] = 'my_index';
        $getParams['type'] = 'my_index';
        $getParams['id'] = 'AU4Kn-knWOmOrmyOj2qg';
        $retDoc = $this->client->get($getParams);
        print_r($retDoc);
    }
}
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值