Hướng dẫn dùng php indexing trong PHP
Phần này trình bày cách sử dụng elasticsearch-php là thư viện để PHP kết nối làm việc với Show
Nên sử dụng Composer để cài đặt elasticsearch-php, gõ lệnh sau: composer require "elasticsearch/elasticsearch" Sau khi thư viện đã tích hợp vào dự án, các file PHP độc lập muốn nạp thư viện này sử cần thêm vào đoạn mã require 'vendor/autoload.php'; Như vậy đã sẵn sàng để dùng
Cấu hình kết nối PHP tới ElasticsearchĐối tượng dùng để kết nối tương tác với ES sinh ra từ lớp //Cấu hình kết nối đến ES $hosts = [ [ 'host' => 'localhost', //yourdomain.com 'port' => '9200', 'scheme' => 'http', //https // 'path' => '/elastic', // 'user' => 'username', //nếu ES cần user/pass // 'pass' => 'password!#$?*abc' ], ]; //Tạo đối tượng Client $client = ClientBuilder::create() ->setHosts($hosts) ->build(); Sau khi có Tạo / xóa một IndexVí dụ cần tạo một ES index đặt tên là $params = [ 'index' => 'article' ]; //Kiểm tra xem Index đã tồn tại không $indexExist = $client->indices()->exists($params); if (!$indexExist) { try { //Thực hiện tạo Index $response = $client->indices()->create($params); } catch (Exception $e) { //Lỗi tạo Index $res = json_decode($e->getMessage()); echo $res->error->reason; } } else { echo "Index {$params['index']} đã có rồi!"; } Nếu muốn xóa một Index thì thực hiện như sau: $params = [ 'index' => 'article' ]; //Kiểm tra xem Index đã tồn tại không $indexExist = $client->indices()->exists($params); if ($indexExist) { $response = $client->indices()->delete($params); echo "Đã xóa"; } else { echo "Index {$params['index']} không có"; } Chỉ mục, cập nhật, xóa DocumentChỉ mục (tạo, cập nhật) một Document $params = [ 'index' => 'article', //Index lưu Document 'id' => '1', //Nếu thiếu id thì ID tự sinh 'type' => 'article_type', //'timestamp' => time(), // Thiết lập timestamp nếu cần thiết 'body' => [ 'testkey' => 'testvalue'] //Dữ liệu Document ]; $response = $client->index($params); Lập chỉ mục nhiều Document $params = [ 'body' => [ [ 'index' => ['_index' => 'article', '_type' => 'article_type', ] ], [ 'testkey1' => 'value1', 'testkey2' => 'testkey2'], [ 'index' => ['_index' => 'article', '_type' => 'article_type', ] ], [ 'testkey1' => 'value11', 'testkey2' => 'testkey22'] ] ]; $responses = $client->bulk($params); Đọc một Document từ Index $params = [ 'index' => 'article', 'type' => 'article_type', 'id' => '1' ]; $response = $client->get($params); Cập nhật Document $params = [ 'index' => 'article', 'type' => 'article_type', 'id' => '1', 'body' => [ 'doc' => [ 'testkey1' => 'valueabc', 'new_field' => 'abc' ] ] ]; $response = $client->update($params); Xóa một Document trong Index $params = [ 'index' => 'article', 'type' => 'article_type', 'id' => '1' ]; $response = $client->delete($params); Tìm kiếm với ESTìm kiếm với $params = [ 'index' => 'article', 'type' => 'article_type', 'body' => [ 'query' => [ 'match' => [ 'testkey' => 'testvalue' ] ] ] ]; $results = $client->search($params); Tìm kiếm phức tạp hơn với logic $params = [ 'index' => 'article', 'type' => 'article_type', 'body' => [ 'query' => [ 'bool' => [ 'filter' => [ 'term' => [ 'testkey1' => 'abc' ] ], 'should' => [ ['match' => [ 'testkey1' => 'value1' ]], ['match' => [ 'testkey2' => 'value2' ]] ] ] ] ] ]; $results = $client->search($params); Kỹ thuật scroll để lấy hết kết quả tìm kiếmKỹ thuật scroll giống như sử dụng con trỏ cursor trong truy vấn CSDL SQLServer - một cách để paging dữ liệu. Ở phần trên, Truy vấn đầu tiên, thiết lập cho nó thêm một tham số là $params = [ 'index' => 'article', 'type' => 'article_type', 'scroll' => '1m', // Giữ con trỏ tìm kiếm tồn tại 1m để truy vấn trang tiếp theo 'size' => 10000, // mỗi trang 1000 kết quả 'body' => [ 'query' => [ 'match' => [ 'testkey' => 'testvalue' ] ] ] ]; $response = $client->search($params); // Truy vấn đầu tiên while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) { // ** // Viết code xử lý kết quả trả về (đọc) - foreach ($rs['hits']['hits'] as $r) // ** // Khi hoàn thành, lấy scroll_id để lấy kết quả trang tiếp (như 10000 kết quả tiếp theo) $scroll_id = $response['_scroll_id']; // Thực hiện cuộn tới kết quả tiếp theo $response = $client->scroll([ 'scroll_id' => $scroll_id, 'scroll' => '30s' // thiết lập con trỏ tồn tại tiếp 30s ] ); } Mã nguồn tham khảo: elasticsearch-learning |