Công cụ thành viên

Công cụ trang web


web_server:use-elasticsearch-in-nukeviet

Hướng dẫn tích hợp ElasticSearch vào NukeViet 4

1. Thêm thư viện elasticsearch cho PHP

Mở file composer.json ở thư mục gốc của NukeViet

"vinades/nukeviet": "dev-master",

Thêm xuống dưới đoạn sau

"elasticsearch/elasticsearch": "~2.0",

Sau đó dùng composer để cập nhật thư viện elasticsearch

2. Khai báo thông tin máy chủ elasticsearch

Với module news, vào phần cấu hình của module để khai báo các tham số

Địa chỉ máy chủ cài đặt Elasticsearch 	
Cổng kết nối với máy chủ Elasticseach 	
CSDL lưu trữ tại máy chủ Elasticsearch

3. Hướng dẫn cập nhật dữ liệu module news trước khi tích hợp elasticSearch

Tạo file update-elastic.php ở thư mục gốc của site với nội dung sau:

<?php
 
/**
 * @Project NUKEVIET 4.x
 * @Author Mr.Thang (kid.apt@gmail.com)
 * @Copyright (C) 2016 VINADES.,JSC.
 * All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @Createdate 07/10/2016, 00:36
 */
 
define('NV_SYSTEM', true);
 
// Xac dinh thu muc goc cua site
define('NV_ROOTDIR', pathinfo(str_replace(DIRECTORY_SEPARATOR, '/', __file__), PATHINFO_DIRNAME));
 
require NV_ROOTDIR . '/includes/mainfile.php';
 
$module_name = 'news'; //module can chuyen du lieu
$site_mods = nv_site_mods();
if (isset($site_mods[$module_name])) {
    $nukeVietElasticSearh = new NukeViet\ElasticSearch\Functions($module_config[$module_name]['elas_host'], $module_config[$module_name]['elas_port'], $module_config[$module_name]['elas_index']);
 
    $module_data = $site_mods[$module_name]['module_data'];
    $elas_type = NV_PREFIXLANG . '_' . $module_data . '_rows'; //bảng dữ liệu sẽ lưu trữ bên máy chủ elasticsearch - lấy tương ứng với bảng bên NukeViet để dễ quản lý
    $id = $nv_Request->get_int('id', 'get', 0);
    $per_page = 500; //so ban ghi se chay moi lan request
 
 
    $htm = '<pre>';
    //Index a document:
    $db_slave->sqlreset()
        ->select('*')
        ->from($elas_type)
        ->limit($per_page)
        ->where('id > ' . $id)
        ->order('id ASC');
    $id = 0;
    $created = 0;
    $result = $db_slave->query($db_slave->sql());
    while ($row = $result->fetch()) {
        //  Lấy nội dung chi tiết
        $body_contents = $db_slave->query('SELECT * FROM ' . NV_PREFIXLANG . '_' . $module_data . '_detail where id=' . $row['id'])->fetch();
        $row = array_merge($row, $body_contents);
 
        // Lọc dấu để phục vụ chi tìm kiếm
        $row['unsigned_title'] = nv_EncString($row['title']);
        $row['unsigned_bodyhtml'] = nv_EncString($row['bodyhtml']);
        $row['unsigned_author'] = nv_EncString($row['author']);
        $row['unsigned_hometext'] = nv_EncString($row['hometext']);
 
        $response = $nukeVietElasticSearh->insert_data($elas_type, $row['id'], $row);
        if ($response['created']) {
            ++$created;
        }
        $id = $row['id'];
 
    }
    if ($id) {
        $redirect = NV_MY_DOMAIN . NV_BASE_SITEURL . 'update-elastic.php?id=' . $id;
        $htm .= '</pre>';
        $time = number_format((microtime(true) - NV_START_TIME), 3, '.', '');
        nv_info_die($global_config['site_description'], $lang_global['site_info'], "Đang thực hiện, thời gian xử lý: " . $time . "  thàng công " . number_format($created) . "/" . number_format($per_page) . " row \n <meta http-equiv=\"refresh\" content=\"2;URL=" . $redirect . "\" />" . $htm);
    }
    die('Thực hiện xong');
} else {
    die('Không có module: ' . $module_name);
}

Sau đó truy cập qua trình duyệt http://my-domain.com/file update-elastic.php

Hướng dẫn tích hợp ElasticSearch với các module khác

1. Kết nối CSDL Elasticsearch

if(isset($db_config['elas_host']))  {
    $nukeVietElasticSearh = new NukeViet\NukeVietElasticSearch\Functions( $db_config['elas_host'], $db_config['elas_port'], $db_config['elas_index'] );
}

Các tham số truyền vào đều đã được cấu hình tại file config.

2. Thêm mới 1 dữ liệu vào Elasticsearch

if(isset($db_config['elas_host']))  {
    $table = NV_PREFIXLANG . '_' . $module_data . '_rows'//bảng dữ liệu cần lưu bên elasticsearch
    $id = 1;//ID bản ghi sau khi đã lưu tại CSDL NukeViet
    $rowcontent = array();//đây là danh sách các phần tử cần lưu
    $response = $nukeVietElasticSearh->insert_data($table, $id, $rowcontent);
}

3. Cập nhật lại dữ liệu vào Elasticsearch

if(isset($db_config['elas_host']))  {
    $table = NV_PREFIXLANG . '_' . $module_data . '_rows'//bảng dữ liệu cần lưu bên elasticsearch
    $id = 1;//ID bản ghi cần cập nhật
    $rowcontent = array();//đây là danh sách các phần tử cần lưu
    $response = $nukeVietElasticSearh->update_data($table, $id, $rowcontent);
}

4. Xóa dữ liệu tại Elasticsearch

if(isset($db_config['elas_host']))  {
    $table = NV_PREFIXLANG . '_' . $module_data . '_rows'//bảng dữ liệu cần lưu bên elasticsearch
    $id = 1;//ID bản ghi cần xóa
    $response = $nukeVietElasticSearh->delete_data($table, $id);
}

5. Tìm kiếm dữ liệu trong Elasticsearch

    $search_elastic = [
        '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 các cột dữ liệu nào, cột này khởi tạo khi thêm 1 bản ghi
                'minimum_should_match' => [
                    '50%'
                ]
            ]
        ]
    ];
    
    
    $array_query_elastic = array();
    $array_query_elastic['query']['bool']=$search_elastic;
    $array_query_elastic['size']=$limit;
    $array_query_elastic['from']=($page - 1) * $limit;
    
    
    $table = NV_PREFIXLANG . '_' . $module_data . '_rows'//bảng dữ liệu cần lưu bên elasticsearch
    $response = $nukeVietElasticSearh->search_data( $table, $array_query_elastic);
}
web_server/use-elasticsearch-in-nukeviet.txt · Sửa đổi lần cuối: 2016/10/14 15:16 bởi vuthao