Created
May 15, 2012 10:53
-
-
Save timothyklim/2700807 to your computer and use it in GitHub Desktop.
elastic sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateCategories < ActiveRecord::Migration | |
def change | |
create_table :categories do |t| | |
t.string :name, null: false | |
t.integer :parent_id | |
t.timestamps | |
end | |
add_index :categories, :title | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Category < ActiveRecord::Base | |
include Tire::Model::Search | |
include Tire::Model::Callbacks | |
after_save do | |
update_index # Обновляем запись в elasticsearch при обновлении категории | |
end | |
mapping do | |
indexes :id, index: :not_analyzed, include_in_all: false # Включаем в выдачу но не учитываем при поиске | |
indexes :name, boost: 100, analyzer: 'snowball' # Тюнинг коэффициента boost и выбор движка для индексирования | |
end | |
def to_indexed_json | |
to_json only: [:id, :name] # Tire.rb экспортирует модель в JSON и отдает это elasticsearch. Указываем колонки модели(можно еще методы через methods: [...]) | |
end | |
def self.search query # Метод который вызываем в контроллере и получаем результат. | |
self.tire.search load: true do | |
size 999999 # При использовании kaminari, tire.rb по-умолчанию делает пагинацию для метода paginate | |
query { string "*#{query}*", default_operator: 'AND' } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
git 'git://github.com/karmi/tire.git' do | |
gem 'tire' | |
end | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
чтобы проиндексировать(или RE) существующие записи, выполняем:
bundle exec rake environment tire:import CLASS=Category FORCE=YES