Công cụ thành viên

Công cụ trang web


web_server:install-and-configure-elasticsearch-on-centos-7

Khác biệt

Đây là những khác biệt giữa hai phiên bản của trang.

Liên kết đến bản xem so sánh này

Phiên bản sau
Phiên bản trước
Phiên bản sauPhiên bản sau của cả hai bên
web_server:install-and-configure-elasticsearch-on-centos-7 [2016/09/16 14:50] – được tạo ra vuthaoweb_server:install-and-configure-elasticsearch-on-centos-7 [2016/09/16 15:02] vuthao
Dòng 24: Dòng 24:
 ===== 2. Setup ===== ===== 2. Setup =====
  
-===== Disable selinux =====+==== Disable selinux ====
  
 In /etc/sysconfig/selinux , change the following lines: In /etc/sysconfig/selinux , change the following lines:
Dòng 35: Dòng 35:
 SELinux status: ''disabled'' SELinux status: ''disabled''
  
-===== Cấu hình firewall ===== +==== Cấu hình firewall ====
 <code> <code>
 yum install firewalld  -y yum install firewalld  -y
Dòng 53: Dòng 53:
 10.0.0.99 là IP của máy cần truy cập 10.0.0.99 là IP của máy cần truy cập
  
-===== Cài đặt và kiểm tra phiên bản của java ===== +==== Cài đặt và kiểm tra phiên bản của java ==== 
 <code> <code>
 yum install java wget -y yum install java wget -y
Dòng 86: Dòng 86:
 Tham khảo Tham khảo
  
-===== 4. PHP kết nối Elasticsearch =====+===== 3. PHP kết nối Elasticsearch =====
  
 Tài liệu chi tiết xem tại: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html Tài liệu chi tiết xem tại: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html
Dòng 93: Dòng 93:
 {{:web_server:mysql_elasticsearch.png |}} {{:web_server:mysql_elasticsearch.png |}}
  
 +Để sử dụng cần cài thêm thư viện elasticsearch thông qua composer 
 +<code>
 +composer require elasticsearch/elasticsearch
 +</code>
  
  
 +====== Các đoạn code dùng trong xử lý tìm kiếm với Elasticsearch ======
 +===== 1. Kết nối với Elasticsearch =====
 +<code php>
 +if(isset($db_config['elas_host']))  {
 + $hosts = array( $db_config['elas_host'] . ':' . $db_config['elas_port'] );
 + $client = Elasticsearch\ClientBuilder::create( )->setHosts( $hosts )->setRetries( 0 )->build();
 +}
 +  
 +</code> 
 +  
 +===== 2. Thêm mới 1 row vào Elasticsearch =====
 +<code php>
 +$module_data = 'news';
 +$params = [
 + 'index' => $db_config['elas_index'],
 + 'type' => NV_PREFIXLANG . '_' . $module_data . '_rows',
 + 'id' =>0,
 + 'body' => []
 +];
  
 +//Index a document:
 +$params['id'] = $rowcontent['id'];//gán id=id của rowcontent khi vừa thêm
 +$params['body'] = $rowcontent;//gán body=body của rowcontent
 +$response = $client->index($params);
 +
 +</code> 
 +
 +===== 3. Cập nhật Elasticsearch =====
 +<code php>
 +$params = array( );
 +$params['index'] = $db_config['elas_index'];
 +$params['type'] = NV_PREFIXLANG . '_' . $module_data . '_rows';
 +$params['id'] = $rowcontent['id'];//gan id= id cua rowcontent
 +$params['body']['doc'] = $rowcontent;//gan body=body cua rowcontent phai để dạng này $params['body']['doc'] nó mới cho update
 +$result_search = $client->update( $params );
 +</code>
 +  
 +===== 4. Search =====
 +<code php>
 +// khai bao bien
 +$params = [
 + 'index' => $db_config['elas_index'],
 + 'type' => NV_PREFIXLANG . '_' . $m_values['module_data'] . '_rows'
 +];
 +//bo dau tieng viet
 +$dbkeyword=nv_EncString($dbkeyword);
 +// tìm kiếm
 +$params['body']['query']['bool'] = [
 + 'should' => [
 + 'multi_match' => [ // dung multi_match:tim kiem theo nhieu truong
 + 'query' => $dbkeyword, // tim kiem theo tu khoa
 + 'type' => [
 + 'cross_fields'
 + ],
 + 'fields' => [
 + 'unsigned_title',
 + 'unsigned_hometext',
 + 'unsigned_bodyhtml'
 + ], // tim kiem theo 3 trương mặc định là hoặc
 + 'minimum_should_match' => [
 + '50%'
 + ]
 + ]
 + ]
 +];
 +
 +$params['body']['size']=$limit;
 +$params['body']['from']=($page - 1) * $limit;
 +$response = $client->search($params);
 +// print_r($response);die('test');
 +
 +</code> 
 +  
 +===== 5. Xóa dữ liệu trong Elasticsearch=====
 +
 +<code php>
 +$params = [
 +'index' => $db_config['elas_index'],
 +'type' => NV_PREFIXLANG . '_' . $module_data . '_rows',
 +'id' => $id,
 +];
 +/*Xóa dữ liệu*/
 +$response = $client->delete($params);
 +
 +</code>
web_server/install-and-configure-elasticsearch-on-centos-7.txt · Sửa đổi lần cuối: 2016/10/14 15:16 bởi vuthao