Hướng dẫn xây dựng hệ thống tìm kiếm fulltext với Coreseek và MySQL

Chuẩn bị dữ liệu và tài nguyên

Để thực hiện thử nghiệm, cần có các thành phần sau: tệp dữ liệu SQL (article.sql), tệp cấu hình (csft_mysql.conf), và mã PHP kiểm tra (test_coreseek.php).

1. Dữ liệu mẫu (MySQL)

CREATE TABLE `article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `content` text COLLATE utf8mb4_general_ci,
  `pub_date` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `article` VALUES (1, 'Dịch bệnh Vũ Hán', 'Đầu năm 2020, dịch bệnh bùng phát tại Vũ Hán, trở thành vùng tâm dịch', NULL);
INSERT INTO `article` VALUES (2, 'Tuyến đầu chống dịch', 'Tình hình dịch bệnh dần cải thiện', NULL);
INSERT INTO `article` VALUES (3, 'Chỉ đạo cấp trên', 'Lãnh đạo công ty sắp xếp thống nhất', NULL);

2. Cấu hình Coreseek (csft_mysql.conf)

# Định nghĩa nguồn dữ liệu MySQL
source mysql_source
{
    type                    = mysql
    sql_host                = localhost
    sql_user                = root
    sql_pass                = your_password
    sql_db                  = coreseek
    sql_port                = 3306
    sql_query_pre           = SET NAMES utf8mb4

    # Cột id phải là kiểu số nguyên
    sql_query               = SELECT id, UNIX_TIMESTAMP(pub_date) AS pub_date, title, content FROM article

    # Thuộc tính dạng timestamp
    sql_attr_timestamp      = pub_date

    sql_query_info_pre      = SET NAMES utf8mb4
    sql_query_info          = SELECT * FROM article WHERE id=$id
}

# Định nghĩa chỉ mục
index mysql_index
{
    source          = mysql_source
    path            = /var/data/coreseek/mysql
    docinfo         = extern
    mlock           = 0
    morphology      = none
    min_word_len    = 1
    html_strip      = 0

    # Cấu hình từ điển tiếng Trung (mmseg)
    charset_dictpath = /usr/local/coreseek/etc/
    charset_type    = zh_cn.utf-8
}

# Cấu hình indexer
indexer
{
    mem_limit       = 256M
}

# Cấu hình searchd
searchd
{
    listen          = 9312
    read_timeout    = 5
    max_children    = 30
    max_matches     = 1000
    seamless_rotate = 0
    preopen_indexes = 0
    unlink_old      = 1
    pid_file        = /var/log/coreseek/searchd_mysql.pid
    log             = /var/log/coreseek/searchd_mysql.log
    query_log       = /var/log/coreseek/query_mysql.log
}

3. Xây dựng chỉ mục

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf --all

4. Khởi động dịch vụ searchd

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf

5. Mã PHP kiểm tra (test_coreseek.php)

<?php
$keyword = $_GET['q'] ?? "dịch bệnh";

require_once("sphinxapi.php");
$client = new SphinxClient();
$client->SetServer('127.0.0.1', 9312);
$client->SetConnectTimeout(3);
$client->SetArrayResult(true);
$client->SetMatchMode(SPH_MATCH_ANY);

$result = $client->Query($keyword, "*");
print_r($result);

6. Chạy thử nghiệm

php /var/www/coreseek/test_coreseek.php

Thẻ: Coreseek Sphinx mysql fulltext search php

Đăng vào ngày 10 tháng 6 lúc 00:32