Sphinx使用入門

搜索三大要素:數據來源、預處理、查詢。
在Sphinx+MySQL的架構中,MySQL主要提供了數據來源和查詢接口,真正進行全文索引建立和查詢的是Sphinx。
MySQL裏面存放真正的數據;Sphinx從MySQL中獲取數據建立全文索引;應用程序使用相應的api與Sphinx交互以獲得真正的數據(此處的api包含SQL接口、php接口,以及其他一些編程語言能夠調用的接口)。

假設test庫內有表test,結構如下:
CREATE TABLE `test` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`content` varchar(20000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`v` char(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
我們需要在content字段上面做全文索引。

根據以上信息配置Sphinx是一件相對容易的事情。在只涉及到單數據來源(MySQL)的情況下,我們只需要關注下面這幾個參數:
source src1
{
# data source type. mandatory, no default value
# known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
type = mysql

#####################################################################
## SQL settings (for ‘mysql’ and ‘pgsql’ types)
#####################################################################

# some straightforward parameters for SQL source types
sql_host = 192.168.*.*
sql_user = realzyy
sql_pass = realzyy
sql_db = test
sql_port = 3306 # optional, default is 3306

# pre-query, executed before the main fetch query
# multi-value, optional, default is empty list of queries
#
sql_query_pre = SET NAMES utf8
# sql_query_pre = SET SESSION query_cache_type=OFF

# range query setup, query that must return min and max ID values
# optional, default is empty
#
# sql_query will need to reference $start and $end boundaries
# if using ranged query:
#
sql_query = \
SELECT id, gmt_create, content,v \
FROM test \
WHERE id>=$start AND id<=$end

sql_query_range = SELECT MIN(id),MAX(id) FROM test

# range query step
# optional, default is 1024
#
sql_range_step = 128

# UNIX timestamp attribute declaration
# multi-value (an arbitrary number of attributes is allowed), optional
# similar to integer, but can also be used in date functions
#
# sql_attr_timestamp = posted_ts
# sql_attr_timestamp = last_edited_ts
sql_attr_timestamp = gmt_create

# string ordinal attribute declaration
# multi-value (an arbitrary number of attributes is allowed), optional
# sorts strings (bytewise), and stores their indexes in the sorted list
# sorting by this attr is equivalent to sorting by the original strings
#
sql_attr_str2ordinal = v

# ranged query throttling, in milliseconds
# optional, default is 0 which means no delay
# enforces given delay before each query step
sql_ranged_throttle = 0

# document info query, ONLY for CLI search (ie. testing and debugging)
# optional, default is empty
# must contain $id macro and must fetch the document by that id
sql_query_info = SELECT * FROM test WHERE id=$id
}

因爲這個表的字符集採用了utf8,所以還需要修改一下:
index test1
{
source = src1
# document source(s) to index
# multi-value, mandatory
# document IDs must be globally unique across all sources
source = src1

# index files path and file name, without extension
# mandatory, path must be writable, extensions will be auto-appended
path = /u01/sphinx/var/data/index_for_test

# document attribute values (docinfo) storage mode
# optional, default is 'extern'
# known values are 'none', 'extern' and 'inline'
docinfo = extern

# memory locking for cached data (.spa and .spi), to prevent swapping
# optional, default is 0 (do not mlock)
# requires searchd to be run from root
mlock = 0

# a list of morphology preprocessors to apply
# optional, default is empty
#
# builtin preprocessors are 'none', 'stem_en', 'stem_ru', 'stem_enru',
# 'soundex', and 'metaphone'; additional preprocessors available from
# libstemmer are 'libstemmer_XXX', where XXX is algorithm code
# (see libstemmer_c/libstemmer/modules.txt)
#
# morphology = stem_en, stem_ru, soundex
# morphology = libstemmer_german
# morphology = libstemmer_sv
morphology = none
# minimum indexed word length
# default is 1 (index everything)
min_word_len = 1

# charset encoding type
# optional, default is 'sbcs'
# known types are 'sbcs' (Single Byte CharSet) and 'utf-8'
charset_type = utf-8

# charset definition and case folding rules "table"
# optional, default value depends on charset_type
#
# defaults are configured to include English and Russian characters only
# you need to change the table to include additional ones
# this behavior MAY change in future versions
#
# 'sbcs' default value is
# charset_table = 0..9, A..Z->a..z, _, a..z, U+A8->U+B8, U+B8, U+C0..U+DF->U+E0..U+FF, U+E0..U+FF
#
# ‘utf-8′ default value is
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
}
執行一下indexer創建全文索引;再執行searchd打開Sphinx的監聽端口以便接收請求;最後可以用search去檢測一下戰果~
#indexer –all
#searchd
#search *****
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章