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 trước của cả hai bênPhiên bản trước
web_server:install-and-configure-elasticsearch-on-centos-7 [2016/10/14 14:32] – [3. PHP kết nối Elasticsearch] vuthaoweb_server:install-and-configure-elasticsearch-on-centos-7 [2016/10/14 15:16] (hiện tại) vuthao
Dòng 82: Dòng 82:
 </code> </code>
    
 +Truy cập thử vào địa chỉ:
 http://10.0.0.124:9200/_plugin/head/  http://10.0.0.124:9200/_plugin/head/ 
  
-Tham khảo+Để tích hợp tìm kiếm của NukeViet sử dụng elasticsearch xem chi thêm tại: http://wiki.nukeviet.vn/web_server:use-elasticsearch-in-nukeviet
  
-===== 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 
- 
-Để sử dụng cần cài thêm thư viện elasticsearch cho PHP thông qua composer  
-<code> 
-composer require elasticsearch/elasticsearch 
-</code> 
- 
-{{:web_server:mysql_elasticsearch.png | }} 
-====== 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