2014-04-21 3 views
0

보석이 있다면 Searchkick이나 Chewy와 같은 elasticsearch와 함께 Rails 응용 프로그램을 연결하는 것이 좋습니다. 그것을 사용하는 방법에 대한 간단한 자습서가 있다면 아주 좋을 것입니다. 감사.Elasticsearch rails gem

elasticsearch 버전> = 1

elasticsearch-rails

elasticsearch 버전을 < = 1

Tire

타이어는 있습니다

+1

https://github.com/elasticsearch/elasticsearch-rails – VDN

답변

5

이있는 레일/elasticsearch 조합에 사용중인 따라 달라집니다 많은 예들, elasticsearch 레일이 그렇게 많지 않기 때문에 aq가 여기 있습니다. 시작 요 약 :

bundle exec rake environment elasticsearch:import:model CLASS='YourModel'

# app/models/your_model.rb 
class YourModel < ActiveRecord::Base 
    include Elasticsearch::Model 
    include Elasticsearch::Model::Callbacks 
end 
코드에서 다른

어딘가에 (컨트롤러 또는 범위를 말한다 :

# Gemfile 
gem 'elasticsearch-model' 
gem 'elasticsearch-rails' 

bundle install

# lib/tasks/elasticsearch.rake 
require 'elasticsearch/rails/tasks/import' 

이 elasticsearch로 DB에서 기존 모델 데이터를로드 방법 또는 어디서나 선호)

# search with field filter 
@your_models = YourModel.search(query: { match: { your_field: "whatever" } }).records 

# faceted search 
@your_models = YourModel.search(query: { match: { your_field: "whatever" } }, facets: { your_facet_name: { terms: { field: "your_facet_field", all_terms: true, order: "term" } } }).records 
# to access to the faceted part of the elasticsearch response: 
facet_results = @your_models..response.response['facets']['your_facet_name']['terms']