作者信息
视频教程分类
推荐视频教程
  • Laraval 9 学习正当时—保姆级教程,想学不会都难!Laraval 9 学习正当时—保姆级教程,想学不会都难!
  • 千万级数据并发解决方案(理论+实战)千万级数据并发解决方案(理论+实战)
  • Laravel基础与实战(模块化)Laravel基础与实战(模块化)
  • UNI-APP开发(仿饿了么)UNI-APP开发(仿饿了么)
  • 首页 >后端开发 >php教程 > 正文

    PHP中使用Elasticsearch的方法

    原创2022-05-13 11:07:545115 关注公众号:每天精选资源文章推送
    这篇文章主要介绍了关于PHP中使用Elasticsearch的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

    课程推荐→:《elasticsearch全文搜索实战》(实战视频)

    来自课程《千万级数据并发解决方案(理论+实战)》

    PHP中使用Elasticsearch

    composer require elasticsearch/elasticsearch

    会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的elasticsearch版本!

    Using version ^5.3 for elasticsearch/elasticsearch
    ./composer.json has been updated
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Package operations: 4 installs, 0 updates, 0 removals
      - Installing react/promise (v2.7.0): Downloading (100%)         
      - Installing guzzlehttp/streams (3.0.0): Downloading (100%)         
      - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%)         
      - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%)         
    Writing lock file
    Generating autoload files

    简单使用

    <?php
    
    class MyElasticSearch
    {
        private $es;
        // 构造函数
        public function __construct()
        {
            include('../vendor/autoload.php');
            $params = array(
                '127.0.0.1:9200'
            );
            $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build();
        }
    
        public function search() {
            $params = [
                'index' => 'megacorp',
                'type' => 'employee',
                'body' => [
                    'query' => [
                        'constant_score' => [ //非评分模式执行
                            'filter' => [ //过滤器,不会计算相关度,速度快
                                'term' => [ //精确查找,不支持多个条件
                                    'about' => '谭'
                                ]
                            ]
    
                        ]
                    ]
                ]
            ];
    
            $res = $this->es->search($params);
    
            print_r($res);
        }
    }
    <?php
    require "./MyElasticSearch.php";
    
    $es = new MyElasticSearch();
    
    $es->search();

    执行结果

    Array
    (
        [took] => 2
        [timed_out] => 
        [_shards] => Array
            (
                [total] => 5
                [successful] => 5
                [skipped] => 0
                [failed] => 0
            )
    
        [hits] => Array
            (
                [total] => 1
                [max_score] => 1
                [hits] => Array
                    (
                        [0] => Array
                            (
                                [_index] => megacorp
                                [_type] => employee
                                [_id] => 3
                                [_score] => 1
                                [_source] => Array
                                    (
                                        [first_name] => 李
                                        [last_name] => 四
                                        [age] => 24
                                        [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。
                                        [interests] => Array
                                            (
                                                [0] => 英雄联盟
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )

    下面是官方的一些样例:

    初始化

    require '../vendor/autoload.php';
    use Elasticsearch\ClientBuilder;
    $client = ClientBuilder::create()->build();

    增加配置

    $hosts = [
        '127.0.01:9200',         // IP + Port
    ];
    
    $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
    ->setHosts($hosts)      // Set the hosts
    ->build();              // Build the client object

    $hosts = [
        '127.0.01:9200',         // IP + Port
    ];
    
    $clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder
    $clientBuilder->setHosts($hosts);           // Set the hosts
    $client = $clientBuilder->build();          // Build the client object

    插入一个文档

    // Index 一个文档
    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id',
        'body' => ['testField' => 'abc']
    ];
    
    $response = $client->index($params);
    print_r($response);

    获取一个文档

    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id'
    ];
    
    $response = $client->get($params);
    print_r($response);

    查询一个文档

    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'body' => [
            'query' => [
                'match' => [
                    'testField' => 'abc'
                ]
            ]
        ]
    ];
    
    $response = $client->search($params);
    print_r($response);

    删除一个文档

    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id'
    ];
    
    $response = $client->delete($params);
    print_r($response);

    结果如下

    Array
    (
        [_index] => my_index
        [_type] => my_type
        [_id] => my_id
        [_version] => 3
        [result] => deleted
        [_shards] => Array
            (
                [total] => 2
                [successful] => 1
                [failed] => 0
            )
    
        [_seq_no] => 2
        [_primary_term] => 1
    )

    删除一个索引

    $deleteParams = [
        'index' => 'my_index'
    ];
    $response = $client->indices()->delete($deleteParams);
    print_r($response);

    创建一个索引

    $params = [
        'index' => 'my_index',
        'body' => [
            'settings' => [
                'number_of_shards' => 2,
                'number_of_replicas' => 0
            ]
        ]
    ];
    
    $response = $client->indices()->create($params);
    print_r($response);

    以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

    相关推荐:

    PHP数据结构基础之栈

    PHP操作Beanstalkd的方法及参数注释

    以上就是PHP中使用Elasticsearch的方法的详细内容,更多请关注php中文网其它相关文章!

    高并发千万级数据库系统解决方案

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:Elasticsearch
  • 相关文章

    相关视频


    专题推荐