Module: Tire::Model::Search::ClassMethods
- Included in:
- ClassMethodsProxy
- Defined in:
- lib/tire/model/search.rb
Instance Method Summary collapse
-
#index ⇒ Object
Returns a Tire::Index instance for this model.
- #multi_search(options = {}, &block) ⇒ Object
-
#search(*args, &block) ⇒ Object
Returns search results for a given query.
Instance Method Details
#index ⇒ Object
Returns a Tire::Index instance for this model.
Example usage: Article.index.refresh
.
118 119 120 121 |
# File 'lib/tire/model/search.rb', line 118 def index name = index_name.respond_to?(:to_proc) ? klass.instance_eval(&index_name) : index_name @index = Index.new(name) end |
#multi_search(options = {}, &block) ⇒ Object
108 109 110 111 112 |
# File 'lib/tire/model/search.rb', line 108 def multi_search(={}, &block) = {:type => document_type} = .update() Tire::Search::Multi::Search.new(index.name, , &block).results end |
#search(*args, &block) ⇒ Object
Returns search results for a given query.
Query can be passed simply as a String:
Article.search 'love'
Any options, such as pagination or sorting, can be passed as a second argument:
Article.search 'love', :per_page => 25, :page => 2
Article.search 'love', :sort => 'title'
For more powerful query definition, use the query DSL passed as a block:
Article.search do
query { terms :tags, ['ruby', 'python'] }
facet 'tags' { terms :tags }
end
You can pass options as the first argument, in this case:
Article.search :per_page => 25, :page => 2 do
query { string 'love' }
end
This methods returns a Tire::Results::Collection instance, containing instances of Tire::Results::Item, populated by the data available in _Elasticsearch, by default.
If you'd like to load the "real" models from the database, you may use the :load
option:
Article.search 'love', :load => true
You can pass options as a Hash to the model's find
method:
Article.search :load => { :include => 'comments' } do ... end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tire/model/search.rb', line 65 def search(*args, &block) = {:type => document_type, :index => index.name} if block_given? = args.shift || {} else query, = args ||= {} end = .update() sort = Array( .delete(:order) || .delete(:sort) ) s = Tire::Search::Search.new(.delete(:index), ) page = .delete(:page) per_page = .delete(:per_page) || Tire::Results::Pagination::default_per_page s.size( per_page.to_i ) if per_page s.from( page.to_i <= 1 ? 0 : (per_page.to_i * (page.to_i-1)) ) if page && per_page s.sort do sort.each do |t| field_name, direction = t.split(':') by field_name, direction end end unless sort.empty? version = .delete(:version) s.version(version) if version if block_given? block.arity < 1 ? s.instance_eval(&block) : block.call(s) else s.query { string query } # TODO: Actualy, allow passing all the valid options from # <http://www.elasticsearch.org/guide/reference/api/search/uri-request.html> s.fields Array([:fields]) if [:fields] end s.results end |