可以到http://zyn-zyn.iteye.com/blog/1218791网站下载所需插件
1.安装thinking-sphinx,在Gemfile中添加下面代码:
gem 'thinking-sphinx'
再运行bundle,就自动安装thinking-sphinx;或者下载gem包安装
2.把sphinx插件放到vendor/plugins目录下,再Gemfile文件中添加
require 'sphinx'
thinking-sphinx和sphinx安装完成,全文搜索的插件就安装好了。
3.配置在config下新建一个文件sphinx.yml,里面添加如下代码:
development: &my_settings
enable_star: 1
min_prefix_len: 0
min_infix_len: 2
min_word_len: 1
max_results: 70000
morphology: none
listen: localhost:3312
port: 9312
charset_type: utf-8
exceptions: f:/wangchuang/wangchuang/log/sphinx_exception.log
##这个路径要根据实际写
chinese_dictionary: f:/wangchuang/wangchuang/vendor/plugins/sphinx/bin/xdict
##这个路径要根据实际写
bin_path: f:/wangchuang/wangchuang/vendor/plugins/sphinx/bin
##这个路径要根据实际写
test:
<<: *my_settings
production:
<<: *my_settings
4.添加delta字段,如下写法:
class AddDeltaToTopic < ActiveRecord::Migration
def self.up
add_column :topics, :delta,:boolean, :default => true, :null => false
add_column :posts, :delta,:boolean, :default => true, :null => false
end
def self.down
remove_column :topics, :delta
remove_column :posts, :delta
end
end
5.model中添加如下代码:
define_index do
indexes :title,:sortable=>true ##索引的字段,如果其他字段也要索引,就重新写一句
has :created_at,:updated_at
set_property :delta => true ##代码实时索引,并且表中要有delta字段
end
6.配置好后,随便运行下面代码一句代码:
rake thinking_sphinx:configure
rake ts:conf
rake ts:config
7.运行好后,会在config目录下生成一个development.sphinx.conf文件,打开文件,
在charset_type = utf-8这句代码下面,添加 chinese_dictionary=f:/wangchuang/wangchuang/vendor/plugins/sphinx/bin/xdict,有几个charset_type = utf-8,就要添加几个chinese_dictionary=f:/wangchuang/wangchuang/vendor/plugins/sphinx/bin/xdict
8.上面修改好后,建立索引,运行下面代码:
rake ts:index INDEX_ONLY=true
如果有建过索引,可运行rake ts:rebuild INDEX_ONLY=true
INDEX_ONLY=true主要是起不生成(development.sphinx.conf)索引文件;如果已经存在development.sphinx.conf文件,在建立索引时就要添加代码INDEX_ONLY=true
9.运行下面代码,开/关服务
rake ts:start ##起服务
rake ts:stop ##停服务
问题
1.在搜索中,如果出现下面的错误可以忽视
Sphinx cannot be found on your system. You may need to configure the following
settings in your config/sphinx.yml file:
* bin_path
* searchd_binary_name
* indexer_binary_name
For more information, read the documentation:
http://freelancing-god.github.com/ts/en/advanced_config.html
2.在搜索中,如果出现下面的错误可以忽视
Riddle cannot detect Sphinx on your machine, and so can't determine which
version of Sphinx you are planning on using. Please use one of the following
lines after "require 'riddle'" to avoid this warning.
require 'riddle/0.9.8'
# or
require 'riddle/0.9.9'
# or
require 'riddle/1.10'