Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

วิธีติดตั้งและตั้งค่า Sphinx Search บน Ubuntu 16.04

Sphinx Search คืออะไร

Sphinx เป็น Search Engine แบบ Open Source ที่ออกแบบมาสำหรับ Full-Text Search โดยเฉพาะ มีประสิทธิภาพสูงมากในการค้นหาข้อมูลจำนวนมาก (Big Data) จากแหล่งข้อมูลหลากหลาย เช่น SQL Databases, ไฟล์ข้อความธรรมดา, XML ฯลฯ

คุณสมบัติเด่นของ Sphinx

  • ดัชนี (Index) ขั้นสูง และเครื่องมือ Query ที่ทรงพลัง
  • ประสิทธิภาพการค้นหาและการสร้างดัชนีสูงมาก
  • รองรับ Post-processing ผลลัพธ์ขั้นสูง
  • Scale ได้ง่าย รองรับการค้นหาขั้นสูง
  • รวมกับ SQL และ XML Data Sources ได้
  • รองรับข้อมูลปริมาณมหาศาล (Billions of documents, Terabytes) และพร้อมสอบ thousands of queries/second

ข้อกำหนดก่อนติดตั้ง (Prerequisites)

  • เครื่อง Ubuntu ที่มี User สามารถใช้ sudo ได้
  • ติดตั้ง MySQL ไว้แล้ว

ติดตั้ง Sphinx บน Ubuntu

ติดตั้งผ่าน Native Package Repository ด้วย apt-get:

$ sudo apt-get install sphinxsearch

จะติดตั้งแพ็กเกจเพิ่มเติม: libmysqlclient20, libstemmer0d

สร้างฐานข้อมูลทดสอบสำหรับ Sphinx

$ mysql -u root -p
mysql> create database test;
mysql> SOURCE /etc/sphinxsearch/example.sql;
mysql> quit

คำสั่ง SOURCE จะนำเข้า Schema และข้อมูลตัวอย่างที่มาพร้อมแพ็กเกจ

กำหนดค่า Sphinx (Configuration)

คัดลอกไฟล์ตัวอย่าง แล้วแก้ไข:

$ cp /etc/sphinxsearch/sphinx.conf.sample /etc/sphinxsearch/sphinx.conf
$ sudo vi /etc/sphinxsearch/sphinx.conf

บล็อก Source (แหล่งข้อมูล)

source src1 {
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass = ubuntu
    sql_db = test
    sql_port = 3306
    sql_query = \
        SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
        FROM documents
    sql_attr_uint = group_id
    sql_attr_timestamp = date_added
}

บล็อก Index (ดัชนี)

index test {
    source = src1
    path = /var/lib/sphinxsearch/data/test
    docinfo = extern
}

บล็อก Searchd (Daemon)

searchd {
    listen = 9312:sphinx      # SphinxAPI port
    listen = 9306:mysql41     # SphinxQL port (MySQL Protocol)
    log = /var/log/sphinxsearch/searchd.log
    query_log = /var/log/sphinxsearch/testquery.log
    read_timeout = 5
    max_children = 30
    pid_file = /var/run/sphinxsearch/testsearchd.pid
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old = 1
    binlog_path = /var/lib/sphinxsearch/datatest
}

สร้างดัชนี (Indexing)

$ sudo indexer --all
Sphinx 2.2.9-id64-release ... using config file '/etc/sphinxsearch/sphinx.conf'...
indexing index 'test'... collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.007 sec, 24319 bytes/sec, 504.03 docs/sec

ตั้งค่า Cron Job อัปเดตดัชนีอัตโนมัติ

$ crontab -e
# เพิ่มบรรทัดนี้ท้ายไฟล์
@hourly /usr/bin/indexer --rotate --config /etc/sphinxsearch/sphinx.conf --all

เริ่มต้นบริการ Sphinx

$ sudo vi /etc/default/sphinxsearch
# แก้ไข: START=yes

$ sudo systemctl restart sphinxsearch.service
$ sudo systemctl status sphinxsearch.service

ทดสอบการค้นหา

เชื่อมต่อผ่าน SphinxQL (Port 9306) ด้วย MySQL Client:

$ mysql -h0 -P9306
mysql> SELECT * FROM test WHERE MATCH('test'); SHOW META;
+------+----------+------------+
| id   | group_id | date_added |
+------+----------+------------+
| 1    | 1        | 1474272578 |
| 2    | 1        | 1474272578 |
| 4    | 2        | 1474272578 |
+------+----------+------------+
3 rows in set (0.00 sec)

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 3     |
| total_found   | 3     |
| time          | 0.000 |
| keyword[0]    | test  |
| docs[0]       | 3     |
| hits[0]       | 5     |
+---------------+-------+

สรุป

เมื่อติดตั้งและตั้งค่าเสร็จแล้ว Sphinx จะทำงานเป็น Full-Text Search Engine ที่ทรงพลัง รองรับข้อมูลปริมาณมหาศาล และประมวลผลคำค้นหาหลายพันคำต่อวินาที ได้