tire的使用入门

(tire 调试的关键是:
1. 会一些基本的elasticsearch 操作, curl -XDELETE...
2. 看log
3. 看 test 目录。 这个是最好的文档。
)

tire 是 elasticsearch的 RUBY工具 (大部分是RUBY, 而不是RAILS),下面是几个基本用法的笔记:

比较全面的文档 见: (example 文件夹)[url]http://karmi.github.com/tire/[/url]

如果你在进行RAILS开发,建议你从 tire自带的 RAILS EXAMPLE 看起。官方文档还是侧重于非RAILS项目的。


1. config/initializers/tire.rb
  Tire.configure do
logger 'tire.log', :level => "DEBUG"
end



1. 进行TIRE的声明:

    class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
end


2. 把所有的现有数据进行索引:

    
# import all
Article.index.import Article.all

# call the paginate method
Article.import :per_page => 100

# call the paginate method, default per_page => 1000
Article.import


导入:
$ rake environment tire:import CLASS='Article'
或者 先删除,再导入:
$ rake environment tire:import CLASS='Article' FORCE=true
production import:
$ rake environment tire:import CLASS='Article' RAILS_ENV=production

结果如下:
[quote]focusbeijing@focusbeijing-desktop:/sg552/workspace/miaomiao_cat$ bundle exec rake environment tire:import CLASS='Item' FORCE=true
[IMPORT] Deleting index 'items'
[IMPORT] Creating index 'items' with mapping:
{"item":{"properties":{}}}
[IMPORT] Starting import for the 'Item' class
--------------------------------------------------------------------------------
287/287 | 100% ###################################################
================================================================================
Import finished in 1.77737 seconds
[/quote]

3. 查询:
    Article.search do
query { string 'love' }
facet('timeline') { date :published_on, :interval => 'month' }
sort { by :published_on, 'desc' }
end


4. 配置(dynamic mapping):
    class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks

mapping do
indexes :id, :index => :not_analyzed
indexes :title, :analyzer => 'snowball', :boost => 100
indexes :content, :analyzer => 'snowball'
indexes :content_size, :as => 'content.size'
indexes :author, :analyzer => 'keyword'
indexes :published_on, :type => 'date', :include_in_all => false
end
end


5. 与mongoid 的结合使用:

5.1 . 增加配置文件: config/initializers/tire.rb
  Tire.configure do
logger "log/tire.log"
end

这样的话,可以看到日志
[quote]
# 错误的日志(出错原因:注意 -d 中不是正确格式的JSON):
# 2012-06-24 10:40:12:%L [apple/4fe67e0c91c97d1691000001] ("apples")
#
curl -X POST "http://localhost:9200/apples/apple/4fe67e0c91c97d1691000001" -d 'tasteswe
et_id4fe67e0c91c97d1691000001colorred'

# 2012-06-24 10:40:12:%L [400]

# ,正确的日志:
# 2012-06-24 10:44:30:%L [apple/4fe67f0e91c97d16c5000001] ("apples")
#
curl -X POST "http://localhost:9200/apples/apple/4fe67f0e91c97d16c5000001" -d '{"taste":"sweet","_id":"4fe67f0e91c97d16c5000001","color":"red"}'
[/quote]

5.2 某个model的声明:

class Apple
include Mongoid::Document
field :color, :type => String
field :taste, :type => String
include Tire::Model::Search
include Tire::Model::Callbacks

# 绝对不要声明这个方法。 画蛇添足。 默认的RAILS就会自动生成正确的json string。否则的话, 会出现错误的json 导致保存不成功。
#def to_indexed_json
# self.as_json
#end
end


5.3 Gemfile:
gem 'tire'
gem 'yajl-ruby', '1.0.0' # 1.1.0 mets problems
# for tire's import
gem 'will_paginate', '~> 3.0.0'


5.4 rspec:

require 'spec_helper'

describe Apple do
it "should create index" do
apple = Apple.new :color => "red", :taste => "sweet good!"
apple.save

s = Apple.tire.search 'red'
puts "s.inspect: #{s.inspect}"
s.each do |a|
puts "document.inspect: #{a.inspect}"
end
end
end


5.5 确保 mongodb 和 elasticsearch 都在运行,然后
$ bundle exec rspec spec/models/apple_spec.rb
就可以看到, mongodb 和 elasticsearch 都同时被加入了新数据。

5.6 一个 ACTION的例子:
(注意其中的分页 和 查询 )

+ page = params[:page] || 1
+ key_word = params[:key_word] || ""
+ s = Tire.search 'items' do
+ unless key_word.blank?
+ query do
+ string(key_word)
+ end
+ end
+ sort { by :created_at, 'desc' }
+ size 50
+ from (page.to_i - 1) * 50
end
+ @items = s.results
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值