Returns a Tire::Index instance for this model.
Example usage: `Article.index.refresh`.
# 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
# File lib/tire/model/search.rb, line 108 def multi_search(options={}, &block) default_options = {:type => document_type} options = default_options.update(options) Tire::Search::Multi::Search.new(index.name, options, &block).results end
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
# File lib/tire/model/search.rb, line 65 def search(*args, &block) default_options = {:type => document_type, :index => index.name} if block_given? options = args.shift || {} else query, options = args options ||= {} end options = default_options.update(options) sort = Array( options.delete(:order) || options.delete(:sort) ) s = Tire::Search::Search.new(options.delete(:index), options) page = options.delete(:page) per_page = options.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 = options.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(options[:fields]) if options[:fields] end s.results end