• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

elasticsearch连接PHP

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php


namespace app\lib;


use Elasticsearch\ClientBuilder;

class Es
{
    private $url = "127.0.0.1";
    private $port = "9200";
    private $client = null;

    public function __construct()
    {
        $params = [
            $this->url.":".$this->port
        ];
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }
    //创建索引
    public function createIndex(string $name = "",int $number_of_shards = 5,int $number_of_replicas = 0){
        $params["index"] = $name;
        $params["body"]["settings"]["number_of_shards"] = $number_of_shards;//是数据分片数,默认为5,有时候设置为3
        $params["body"]["settings"]["number_of_replicas"] = $number_of_replicas; //是数据备份数,如果只有一台机器,设置为0
        $result = $this->client->indices()->create($params);
        return $result;  //返回值 Array ( [acknowledged] => 1 [shards_acknowledged] => 1 [index] => person )
    }

    //删除索引
    public function deleteIndex(string $index = ""){
        $params["index"] = $index;
        $result = $this->client->indices()->delete($params);
        return $result; //返回值 Array ( [acknowledged] => 1 )
    }

    //创建文档
    public function addDocument(string $index = "" ,string $type = "" ,string $id = "" ,array $body = []){
        $params = [
            "index" => $index,
            "type"=> $type,
            "id" => $id ,
            "body" => $body
        ];
        $result = $this->client->index($params);
        return $result;

        /*Array
        (
            [_index] => person
            [_type] => person_type
            [_id] => 1
            [_version] => 1
            [result] => created
            [_shards] => Array
                (
                    [total] => 1
                    [successful] => 1
                    [failed] => 0
                )
            [_seq_no] => 0
            [_primary_term] => 1
        )*/
    }

    //更新文档
    public function editDocument(string $index = "" ,string $type = "" ,string $id = "" ,array $doc = []){
        $params = [
            "index" => $index,
            "type"=> $type,
            "id" => $id ,
            "body" => $doc
        ];
        $result = $this->client->update($params);
        return $result;
    }

    //删除文档
    public function delDocument(string $index = "" ,string $type = "" ,string $id = "" ){
        $params = [
            "index" => $index,
            "type"=> $type,
            "id" => $id ,
        ];
        $result = $this->client->delete($params);
        return $result;
    }

    //获取文档
    public function getDocument(string $index = "" ,string $type = "" ,string $id = ""){
        $params = [
            "index" => $index,
            "type"=> $type,
            "id" => $id ,
        ];
        $result = $this->client->get($params);
        return $result;
    }
    //查询文档
    public function searchDocument(string $index = "" ,string $type = "" ,int $from = 0 ,int $size = 10,array $order = ['order' => 'id']){
        $params = [
            "index" => $index,
            "type"=> $type,
            "from" => $from ,
            "size" => $size ,
            //"sort" => ['_score' => $order]
        ];
        //相当于sql语句:  select * from hp_product where prodcut_name like '茶'  limit 0,100 order by id desc;
        $params['body']['query']['match']['title'] = '张';
        $result = $this->client->search($params);
        print_r($result);

        //相当于sql语句: select * from hp_product where product_name like '茶'  and product_id = 20 limit 200,10;
        // $params['body']['query']['bool']['must'] = array(
        //     array('match' => array('product_name' => '茶')),
        //     array('match' => array('product_id' => 20))
        //    );
        // $params['size'] = 10;
        // $params['from'] = 200;
        //
        //
        // 当于sql语句:select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
        // $params['body']['query']['bool']['should'] = array(
        //        array('match' => array('product_name' => '茶')),
        //        array('match' => array('product_id' => 20))
        //      );
        //$params['size'] = 10;
        //$params['from'] = 200;
        //
        //
        // 当于sql语句: select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
        // $params['body']['query']['bool']['must_not'] = array(
        //        array('match' => array('product_name' => '茶')),
        //        array('match' => array('product_id' => 20))
        //      );
        //$params['size'] = 10;
        //$params['from'] = 200;
        //
        //
        //当于sql语句:select * from hp_product where id>=20 and id<30  limit 200,10;
        // $params['body']['query']['range'] = array(
        //        'id' => array('gte' => 20,'lt' => 30);
        //      );
        //$params['size'] = 10;
        //$params['from'] = 200;
    }

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP运算符优先级运算符分类发布时间:2022-07-10
下一篇:
PHP5.2+APACHE2.2+mysql4.1.21+BugFree1.0的安装发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap