Công cụ thành viên

Công cụ trang web


web_server:use-elasticsearch-in-nukeviet

Đây là một phiên bản cũ của tài liệu!


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

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

Mở file config.php của NukeViet thêm vào đoạn sau đây

$db_config['elas_host'] = '192.168.0.30';//Đây là địa chỉ IP máy chủ đã cài elasticsearch
$db_config['elas_port'] = '9200';//Cổng kết nối đến máy chủ
$db_config['elas_index'] = 'nukeviet4_elastic';//Cơ sở dữ liệu lưu trữ tại máy chủ.

Phần cơ sở dữ liệu không cần phải khởi tạo trước. Có thể đặt bất cứ tên nào tùy ý.

2. 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.

3. 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);
}

4. 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);
}

5. 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);
}

6. 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);
}

7. Hướng dẫn nâng cấp dữ liệu với những module đã có dữ liệu trước khi tích hợp elasticSearch

<?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_data = 'news';//module can chuyen du lieu

$elas_host = '192.168.0.30';//dia chi server elasticsearch
$elas_port = '9200';//cong ket noi elasticsearch
$elas_index = 'nukeviet4_thang';//CSDL elasticsearch
$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ý

$nukeVietElasticSearh = new NukeViet\NukeVietElasticSearch\Functions( $elas_host, $elas_port, $elas_index );

$htm = '<pre>';

$id = $nv_Request->get_int('id', 'get', 0);
$per_page = 500;//so ban ghi se chay moi lan request
//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 = 'http://elasticsearch.xyz/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');
web_server/use-elasticsearch-in-nukeviet.1475829095.txt.gz · Sửa đổi lần cuối: 2016/10/07 15:31 bởi kid.apt