tire的使用入门

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

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

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

如果你在进行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

结果如下:
引用
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


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

这样的话,可以看到日志
引用

# 错误的日志(出错原因:注意 -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"}'

    
  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

 

你可能感兴趣的:(elasticsearch,Rails,tire)