Sphinx เป็นโปรแกรมที่มีหน้าที่ในการค้นหาข้อมูลจากแหล่งข้อมูลหลากหลายชนิดเช่น SQL databases, plain text files, HTML files, mailboxes เป็นต้น สามารถอ่านข้อมูลความสามารถเพิ่มเติมได้ที่ http://sphinxsearch.com/
ติดตั้ง MySQL ด้วยคำสั่ง
sudo apt-get update
sudo apt-get install mysql-server
ติดตั้ง Sphinx ด้วยคำสั่ง
sudo apt-get install sphinxsearch
สร้างฐานข้อมูลให้กับ Sphinx ด้วยคำสั่ง
mysql -u root –p
CREATE DATABASE test;
SOURCE /etc/sphinxsearch/example.sql;
quit
ตั้งค่า Sphinx โดยการสร้างไฟล์ sphinx.conf ด้วยคำสั่ง
sudo nano /etc/sphinxsearch/sphinx.conf
โดยมีเนื้อหาดังนี้
source src1 { type = mysql sql_host = localhost sql_user = root sql_pass = password sql_db = test sql_port = 3306 # optional, default is 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 sql_query_info = SELECT * FROM documents WHERE id=$id } index test1 { source = src1 path = /var/lib/sphinxsearch/data/test1 docinfo = extern charset_type = sbcs } searchd { listen = 9312 # Port to listen on log = /var/log/sphinxsearch/searchd.log query_log = /var/log/sphinxsearch/query.log read_timeout = 5 max_children = 30 pid_file = /var/run/sphinxsearch/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 binlog_path = /var/lib/sphinxsearch/data }
โดยทำการใส่ค่า Username และ Password ของฐานข้อมูล MySQL ตามที่ได้ตั้งค่าไว้ในส่วนของ sql_user และ sql_pass
คำอธิบายเพิ่มเติมของของการตั้งค่าต่างๆมีดังนี้
- sql_host : ตำแหน่งที่อยู่ของฐานข้อมูล
- sql_db : ชื่อฐานข้อมูลที่จะทำการค้นหา
- sql_query : คำสั่งในการดึงข้อมูลจากฐานข้อมูล
- path : ตำแหน่งที่ใช้ในการเก็บผลการค้นหา
- charset_type : ประเภทของรูปแบบตัวอักษรที่ใช้ในการเก็บผลการค้นหา
- listen : หมายเลข Port ที่ sphinx daemon ใช้ทำงาน
- query_log : ตำแหน่งในการเก็บ Log ของการค้นหา
- pid_file : ตำแหน่งในการเก็บหมายเลข PID ของ sphinx daemon
ทำการเพิ่มข้อมูลตัวอย่างเพื่อทดสอบการค้นหาด้วยคำสั่ง
sudo indexer –all
จะได้ข้อมูลประมาณนี้
Sphinx 2.0.4-id64-release (r3135) Copyright (c) 2001-2012, Andrew Aksyonoff Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinxsearch/sphinx.conf'... indexing index 'test1'... WARNING: collect_hits: mem_limit=0 kb too low, increasing to 25600 kb collected 4 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 4 docs, 193 bytes total 0.005 sec, 33788 bytes/sec, 700.28 docs/sec total 3 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
เปิดการใช้งาน Sphinx ด้วยการเข้าไปแก้ไขไฟล์ sphinxsearch ด้วยคำสั่ง
sudo nano /etc/default/sphinxsearch
โดยทำการแก้ไขเนื้อหาดังนี้
START=yes
บันทึกไฟล์แล้วสั่งคำสั่ง
sudo service sphinxsearch start
ทดสอบการค้นหาด้วยคำสั่ง
search this is my test document number
จะได้ผลลัพธ์ดังนี้
Sphinx 2.0.4-id64-release (r3135) Copyright (c) 2001-2012, Andrew Aksyonoff Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com) using config file '/etc/sphinxsearch/sphinx.conf'... index 'test1': query 'this is my test document number ': returned 2 matches of 2 total in 0.002 sec displaying matches: 1. document=1, weight=7431, group_id=1, date_added=Tue Dec 16 09:49:04 2014 id=1 group_id=1 group_id2=5 date_added=2014-12-16 09:49:04 title=test one content=this is my test document number one. also checking search within phrases. 2. document=2, weight=7431, group_id=1, date_added=Tue Dec 16 09:49:04 2014 id=2 group_id=1 group_id2=6 date_added=2014-12-16 09:49:04 title=test two content=this is my test document number two words: 1. 'this': 4 documents, 4 hits 2. 'is': 4 documents, 4 hits 3. 'my': 2 documents, 2 hits 4. 'test': 3 documents, 5 hits 5. 'document': 2 documents, 2 hits 6. 'number': 3 documents, 3 hits