搜索三大要素:数据来源、预处理、查询。
在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 *****