php-elasticsearch客戶端基本使用

php-elasticsearch客戶端基本使用

標籤(空格分隔): php,elasticsearch

官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_searching_documents
官方中文文檔(已過時):https://www.elastic.co/guide/cn/elasticsearch/php/current/_configuration.html

開始

本頁將指導您完成 PHP 客戶端的安裝過程,向您展示如何實例化客戶端,以及如何使用它執行基本的 Elasticsearch 操作。

安裝

composer require elasticsearch/elasticsearch

實例化客戶端

require 'vendor/autoload.php';

$client = Elastic\Elasticsearch\ClientBuilder::create()->build();

創建索引

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'my_index',
];

$response = $client->indices()->create($params);

// response print
array(3) {
  ["acknowledged"]=>
  bool(true)
  ["shards_acknowledged"]=>
  bool(true)
  ["index"]=>
  string(8) "my_index"
}

插入一條數據

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$response = $client->index([
    'index' => 'my_index',
    'body' => [
        'name' => 'Taylor',
        'age' => 32,
        'address' => "1101 S Main St APT 203 Milpitas CA 95035",
        'sex' => '女',
        'hobby' => ["唱歌", "跳舞", "談戀愛", "走秀"]
    ]
]);
// response print
array(8) {
  ["_index"]=>
  string(8) "my_index"
  ["_type"]=>
  string(4) "_doc"
  ["_id"]=>
  string(20) "uH5JtIkBC78u-UjcI9s-"
  ["_version"]=>
  int(1)
  ["result"]=>
  string(7) "created"
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(2)
    ["successful"]=>
    int(1)
    ["failed"]=>
    int(0)
  }
  ["_seq_no"]=>
  int(0)
  ["_primary_term"]=>
  int(1)
}

插入多條

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = ['body' => []];
for ($i = 1; $i < 6;$i++) {
    $params['body'][] = [
        'index' =>  [
            '_index' => 'my_index',
            '_id' => $i
        ]
    ];
    $params['body'][] = [
        'name' => "Swift".$i,
        'age' => rand(10, 35),
        'address' => "1101 S Main St APT 203 Milpitas CA 95035",
        'sex' => "女",
        'hobby' => ["唱歌", "跳舞", "談戀愛", "走秀"]
    ];
}

$response = $client->bulk($params);
// response print 新增5條數據
array(3) {
  ["took"]=>
  int(8)
  ["errors"]=>
  bool(false)
  ["items"]=>
  array(5) {
        [0]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "1"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(1)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [1]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "2"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(2)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [2]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "3"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(3)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [3]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "4"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(4)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [4]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "5"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(5)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
  }
}

更新數據

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    'index' => 'my_index',
    'id'    => 'uH5JtIkBC78u-UjcI9s-',
    'body'  => [
        'doc' => [
            'address' => '美國'
        ]
    ]
];

// Update doc at /my_index/_doc/my_id
$response = $client->update($params);
// response print
array(8) {
  ["_index"]=>
  string(8) "my_index"
  ["_type"]=>
  string(4) "_doc"
  ["_id"]=>
  string(20) "uH5JtIkBC78u-UjcI9s-"
  ["_version"]=>
  int(2)
  ["result"]=>
  string(7) "updated"
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(2)
    ["successful"]=>
    int(1)
    ["failed"]=>
    int(0)
  }
  ["_seq_no"]=>
  int(6)
  ["_primary_term"]=>
  int(1)
}

刪除數據

$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);

刪除索引

$params = ['index' => 'my_index'];

$response = $client->indices()->delete($params);

查詢所有

$params = [
    'index' => 'my_index',
];

$response = $client->search($params);

查詢一條

$params = [
    'index' => 'my_index',
    'id'    => 'uH5JtIkBC78u-UjcI9s-',
];

$response = $client->get($params);

match查詢

$client = ClientBuilder::create()->setHosts($hosts)->build();

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'name' => 'taylor'
            ]
        ]
    ]
]);

term查詢

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'term' => [
                'name' => 'taylor'
            ]
        ]
    ]
]);

range查詢

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'range' => [
                'age' => [
                    'gte' => 10,
                    'lte' => 20
                ]
            ]
        ]
    ]
]);

bool組合查詢

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'bool' => [
                'filter' => [
                    'term' => [ 'address' => 'abc' ]
                ],
                'should' => [
                    'match' => [ 'name' => 'xyz' ]
                ]
            ]
        ]
    ]
];

$results = $client->search($params);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章