Xây dựng Full-Text Search với Coreseek và Sphinx

Khi xây dựng ứng dụng web, việc tìm kiếm văn bản hiệu quả trên nhiều bảng dữ liệu là một yêu cầu phổ biến. Giả sử bạn có hai bảng là articlesarticle_photos, và bạn muốn tìm kiếm từ khóa trên các trường articles.title, articles.intro, và article_photos.caption. Coreseek (một bản phân phối của Sphinx) sẽ giúp bạn giải quyết bài toán này một cách mượt mà.

Triển khai PHP

header("content-type:text/html;charset=utf8");
include './sphinxapi.php'; // Gọi lớp Sphinx API
$searchClient = new SphinxClient();
$searchClient->SetServer('localhost', 9312);
$searchClient->SetLimits(0, 1000, 1000); // Mặc định chỉ hiển thị 20 kết quả, thiết lập này lấy tối đa 1000 bản ghi
$result = $searchClient->Query("từ_khóa", "*"); // Tham số thứ hai là tên index; '*' sẽ tìm kiếm trên tất cả index đã cấu hình

echo "<pre>";
print_r($result['matches']);
echo "</pre>";

Cấu hình Coreseek (coreseek.conf)

source mysql {
    type                    = mysql
    sql_host                = localhost
    sql_user                = root
    sql_pass                =
    sql_db                  = nextmgz_archive
    sql_port                = 3306
    sql_query_pre           = SET NAMES utf8
    sql_query_info_pre      = SET NAMES utf8
}

# Index cho bảng articles
source articles_source : mysql
{
    sql_query                = SELECT art_id, title, intro FROM articles
    sql_attr_uint            = art_id
    sql_query_info          = SELECT * FROM articles WHERE art_id=$id
}

index articles_index
{
    source           = articles_source
    path             = E:/__Work/coreseek-4.1-win32/var/data/articles
    docinfo          = extern
    mlock            = 0
    morphology       = none
    min_word_len     = 1
    html_strip       = 0
    charset_dictpath = E:/__Work/coreseek-4.1-win32/etc/
    charset_type     = zh_cn.utf-8
}

# Index cho bảng article_photos
source article_photos_source : mysql
{
    sql_query               = SELECT art_id, caption FROM article_photos
    sql_attr_uint           = art_id
    sql_query_info          = SELECT * FROM article_photos WHERE art_id=$id
}

index article_photos_index
{
    source           = article_photos_source
    path             = E:/__Work/coreseek-4.1-win32/var/data/article_photos
    docinfo          = extern
    mlock            = 0
    morphology       = none
    min_word_len     = 1
    html_strip       = 0
    charset_dictpath = E:/__Work/coreseek-4.1-win32/etc/
    charset_type     = zh_cn.utf-8
}

# Phần cấu hình chung cho indexer
indexer
{
    mem_limit            = 128M
}

# Cấu hình cho searchd (daemon tìm kiếm)
searchd
{
    listen              = 9312
    read_timeout        = 5
    max_children        = 30
    max_matches         = 1000
    seamless_rotate     = 0
    preopen_indexes     = 0
    unlink_old          = 1
    pid_file            = E:/__Work/coreseek-4.1-win32/var/log/searchd_mysql.pid
    log                 = E:/__Work/coreseek-4.1-win32/var/log/searchd_mysql.log
    query_log           = E:/__Work/coreseek-4.1-win32/var/log/query_mysql.log
    binlog_path         = 
}

Tạo Index và Khởi động Dịch vụ

# Tạo toàn bộ index (tương đương với lệnh bên dưới)
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf --all --rotate

# Tạo từng index riêng lẻ
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf articles_index
E:/__Work/coreseek-4.1-win32/bin/indexer -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf article_photos_index

# Khởi động searchd
E:/__Work/coreseek-4.1-win32/bin/searchd -c E:/__Work/coreseek-4.1-win32/bin/coreseek.conf

Thẻ: Coreseek Sphinx Full-Text Search php mysql

Đăng vào ngày 3 tháng 6 lúc 20:50