linux rails3安装 thinking sphinx插件

7 篇文章 0 订阅
6 篇文章 0 订阅

rails3

 

1.下载coreseek-4.1-beta.tar.gz,可以在linux上运行下面命令下下载:

wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-4.1-beta.tar.gz

 

2. 进行解压

tar xzvf coreseek-4.1-beta.tar.gz

 

安装时要注意:是否为root权限,有些东西是要root权限才能安装上

3.进入coreseek-4.1-beta目录下

cd mmseg-3.2.14
./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决
./configure --prefix=/usr/local/mmseg
make && make install
#可以去/usr/local 下查看一下是否存在mmseg文件夹,如果没有文件夹,请重新运行一遍上面代码。

 

 

4.注意mmseg文件夹所在的目录与下面代码对应

 cd csft-3.2.14 或者 cd csft-4.0.1 或者 cd csft-4.1
 sh buildconf.sh    #输出的warning信息可以忽略,如果出现error则需要解决
 ./configure --prefix=/usr/local/coreseek  --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/ --with-mysql 
 make && make install

 

 

5.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 
  exceptions: RAILS_ROOT/log/sphinx_exception.log 
  #项目目录下log文件夹中新建一个sphinx_exception.log文件,这个路径也修改为对应路径 
  chinese_dictionary: /usr/local/mmseg/etc/ 
  #中文字典路径 
  bin_path: /usr/local/coreseek/bin 
test: 
  <<: *my_settings 
production: 
  <<: *my_settings 

 

6.如果是运行开发 

rake ts:config  
生成一个development.sphinx.conf
 
生产:RAILS_ENV=production rake ts:config
生成一个production.sphinx.conf 

 

注意:
把 listen = localhost:3312  修改为 listen = 127.0.0.1:9312
 
并且所有 charset_type = utf-8 修改为 charset_type = zh_cn.utf-8
并且所有 charset_type = zh_cn.utf-8 下添加 charset_dictpath = /usr/local/mmseg/etc/

 

7.再做索引

rake ts:index INDEX_ONLY=true
生产:RAILS_ENV=production ts:index INDEX_ONLY=true

 

8.thinkingsphinx用法:

实时索引

最常用的就是实时索引,它要求我们在被索引的模型中增加一个属性,名为delta。
ruby script/generate migration AddDeltaToTopic delta:boolean
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
  define_index do
    indexes :title,:sortable => true
    indexes first_post.body,:as => :body,:sortable => true
    indexes author
    has :created_at,:updated_at,:forum_id,:digest
    #声明使用实时索引
    set_property :delta => true
  end
  define_index do
    indexes topic(:title),:as => :title ,:sortable => true
    indexes body,:sortable => true
    indexes author
    has :created_at,:updated_at,:forum_id
    #声明使用实时索引
    set_property :delta => true
  end

定时索引
set_property :delta => :datetime, :threshold => 1.hour
一小时索引一次,实际上是重建所有索引,所以注意间隔时间不要短于建立索引的时间。

默认使用updated_at,这样就不用给被索引模型添加delta属性了。

需要结合的重建索引的命令为rake thinking_sphinx:index:delta或rake ts:in:delta

延时索引
set_property :delta => :delayed

 

9.搜索参数与配置方式

多模型查询
ThinkingSphinx::Search.search "term", :classes => [Post, User, Photo, Event]

指定匹配模式
SPH_MATCH_ALL模式,匹配所有查询词(默认模式)。
Topic.search "Ruby Louvre",:match_mode => :all
Topic.search "Louvre" 
PH_MATCH_ANY模式,匹配查询词中的任意一个。
Topic.search "Ruby Louvre", :match_mode => :any
SPH_MATCH_PHRASE模式,将整个查询看作一个词组,要求按顺序完整匹配。
Topic.search "Ruby Louvre", :match_mode => :phrase 

SPH_MATCH_BOOLEAN模式,将查询看作一个布尔表达式。
Topic.search "Ruby | Louvre, :match_mode => :boolean 
SPH_MATCH_EXTENDED模式,将查询看作一个Sphinx内部查询语言的表达式。
User.search "@name pat | @username pat", :match_mode => :extended 
加权
为了提高搜索质量,我们可以对某些模型或字段进行加权,提高它们的优先度。默认都是1
User.search "pat allan", :field_weights => { :alias => 4, :aka => 2 }
ThinkingSphinx::Search.search "pat", :index_weights => { User => 10 }

 

 

10.怎么指定model所对应的表,以及model索引写法

#encoding: utf-8
class Product < ActiveRecord::Base
  set_table_name "product"  #设定model所对应的表,可以是复数或单数

  define_index do 
    indexes :name, :sortable=>true   ##索引的字段,如果其他字段也要索引,就重新写一句 
    indexes product_type.Title, :as => :product_type_title  #表关联索引
    indexes :StartCity, :sortable=>true
    has :Sort, :ShowStatus   #搜索时有条件,必须要添加到has里
    has pinstances.comment_num, :as => :pinstance_comment_nums,:type=>:integer
    has pinstances.mall.grade,:as => :mall_grade,:type=>:integer
    #has :updated_at 
    set_property :delta => true    ##代码实时索引,并且表中要有delta字段 
    #set_property :delta => :datetime, :threshold => 1.hour
  end
  
 
end

 

11.搜索里的参数

Product.search key,    :page => page,
                       :per_page => per_page,
                       :order => order,
                       :with => search_params,
                       :star => true, 
                       :match_mode => :extended2, 
                       :field_weights => { :brand_name => 4,:brand_synonym => 3, :code => 4, :full_names =>2},
                       :sort_mode => :extended

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值