`

rails3 和 thinking_sphinx安装

阅读更多
可以下面,下载到thinking-sphinx、sphinx(sphinx插件包)、riddle插件

1.安装thinking-sphinx,在Gemfile中添加下面代码:
gem 'thinking-sphinx'

再运行bundle,就自动安装thinking-sphinx;或者下载gem包安装

如果没有riddle,请先安装riddle插件

2.把sphinx插件放到vendor/plugins目录下,再Gemfile文件中添加
gem 'sphinx'

运行bundle,自动安装sphinx;或下载sphinx的gem插件,安装。
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   ##停服务

参考网站:
http://www.cnblogs.com/rubylouvre/archive/2009/07/23/1529029.html

问题
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'
分享到:
评论
4 楼 zyn_zyn 2013-07-19  
jiafuguang 写道
楼主,是你原创的吗

自己总结的....
3 楼 jiafuguang 2013-07-12  
楼主,是你原创的吗
2 楼 zyn_zyn 2012-08-31  
你用的是什么操作系统,windows上你要把下载一下我文章上的“sphinx.zip (5.7 MB) ”,然后把它放到“项目目录/vendor/plugins/”下,然后,再按照文章上的代码写就可以了..

linux上与windows配置不一样,我还有一篇文章有写的
1 楼 bnulee 2012-08-29  
嘿,你好。最近在用rails3+ts做全文搜索。。现在中文的还没解决。能加我qq503594364么?

相关推荐

Global site tag (gtag.js) - Google Analytics