首页 > 技术文章 > tp5.0 中elasticsearch的基本使用

gj210623 2021-09-28 18:10 原文

  1. composer require elasticsearch/elasticsearch

     

  2. 配置php.ini 的 sys_temp_dir 改为 php的tmp文件下
  3. 在Elasticsearch中存储数据的行为就叫做索引(indexing)

    在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)

  4. 创建索引
  5. $es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    $params = [
        'index' => 'test_index'
    ];
    $r = $es->indices()->create($params);
    dump($r);die;
  6. 添加文档
  7. $es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    $params = [
        'index' => 'test_index',
        'type' => 'test_type',
        'id' => 100,
        'body' => ['id'=>100, 'title'=>'PHP从入门到精通', 'author' => '张三']
    ];
    
    $r = $es->index($params);
    dump($r);die;
  8. 修改文档
  9. $es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    $params = [
        'index' => 'test_index',
        'type' => 'test_type',
        'id' => 100,
        'body' => [
            'doc' => ['id'=>100, 'title'=>'ES从入门到精通', 'author' => '张三']
        ]
    ];
    
    $r = $es->update($params);
    dump($r);die;
  10. 删除文档
  11. $es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    $params = [
        'index' => 'test_index',
        'type' => 'test_type',
        'id' => 100,
    ];
    
    $r = $es->delete($params);
    dump($r);die;

推荐阅读