Class: Xapit::Collection
- Inherits:
-
Object
- Object
- Xapit::Collection
- Defined in:
- lib/xapit/collection.rb
Overview
This is the object which is returned when performing a search. It behaves like an array, so you do not need to worry about fetching the results separately. Just loop through this collection.
The results are lazy loading, meaning it does not perform the query on the database until it has to. This allows you to string queries onto one another.
Article.search("kite").search("sky") # only performs one query
This class is compatible with will_paginate; you can pass it to the will_paginate helper in the view.
Constant Summary collapse
- NON_DELEGATE_METHODS =
%w(nil? send object_id class extend size paginate first last empty? respond_to?).to_set
Class Method Summary collapse
Instance Method Summary collapse
-
#all_facets ⇒ Object
All Xapit::Facet objects, even if they do not include options.
-
#applied_facet_options ⇒ Object
Xapit::FacetOption objects which are currently applied to search (through :facets option).
- #base_query ⇒ Object
- #base_query=(base_query) ⇒ Object
-
#current_page ⇒ Object
The page number we are currently on.
-
#empty? ⇒ Boolean
Returns true if no results are found.
-
#facets ⇒ Object
Xapit::Facet objects matching this search query.
-
#first ⇒ Object
The first record in the result set.
-
#initialize(*args) ⇒ Collection
constructor
A new instance of Collection.
-
#last ⇒ Object
The last record in the result set.
-
#next_page ⇒ Object
The next page number.
-
#offset ⇒ Object
The offset for the current page.
-
#per_page ⇒ Object
How many records to display on each page, defaults to 20.
-
#previous_page ⇒ Object
The previous page number.
-
#results ⇒ Object
Returns an array of results.
-
#search(keywords, options = {}) ⇒ Object
Perform another search on this one, inheriting all options already passed.
-
#size ⇒ Object
(also: #total_entries)
The number of total records found despite any pagination settings.
-
#spelling_suggestion ⇒ Object
Includes a suggested variation of a term which will get many more results.
-
#total_pages ⇒ Object
Total number of pages with found results.
Constructor Details
#initialize(*args) ⇒ Collection
Returns a new instance of Collection.
26 27 28 |
# File 'lib/xapit/collection.rb', line 26 def initialize(*args) @query_parser = Config.query_parser.new(*args) end |
Class Method Details
.search_similar(member, *args) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/xapit/collection.rb', line 17 def self.search_similar(member, *args) collection = new(member.class, *args) indexer = SimpleIndexer.new(member.class.xapit_index_blueprint) terms = indexer.text_terms(member) + indexer.field_terms(member) collection.base_query.and_query(terms, :or) collection.base_query.not_query("Q#{member.class}-#{member.id}") collection end |
Instance Method Details
#all_facets ⇒ Object
All Xapit::Facet objects, even if they do not include options. Usually you’ll want to call Collection#facets
146 147 148 149 150 |
# File 'lib/xapit/collection.rb', line 146 def all_facets @query_parser.member_class.xapit_index_blueprint.facets.map do |facet_blueprint| Facet.new(facet_blueprint, @query_parser.query, @query_parser.facet_identifiers) end end |
#applied_facet_options ⇒ Object
Xapit::FacetOption objects which are currently applied to search (through :facets option). Use this to display the facets which are currently applied.
<% for option in @articles.applied_facet_options %>
<%=h option.name %>
<%= link_to "remove", :overwrite_params => { :facets => option } %>
<% end %>
If you set :breadcrumb_facets option to true in Config#setup the link will drop leftover facets instead of removing the current one. This makes it easy to add a breadcrumb style interface.
Xapit.setup(:breadcrumb_facets => true)
<% for option in @articles.applied_facet_options %>
<%= link_to h(option.name), :overwrite_params => { :facets => option } %> >
<% end %>
126 127 128 129 130 131 132 |
# File 'lib/xapit/collection.rb', line 126 def @query_parser.facet_identifiers.map do |identifier| option = FacetOption.find(identifier) option.existing_facet_identifiers = @query_parser.facet_identifiers option end end |
#base_query ⇒ Object
69 70 71 |
# File 'lib/xapit/collection.rb', line 69 def base_query @query_parser.base_query end |
#base_query=(base_query) ⇒ Object
65 66 67 |
# File 'lib/xapit/collection.rb', line 65 def base_query=(base_query) @query_parser.base_query = base_query end |
#current_page ⇒ Object
The page number we are currently on.
74 75 76 |
# File 'lib/xapit/collection.rb', line 74 def current_page @query_parser.current_page end |
#empty? ⇒ Boolean
Returns true if no results are found.
43 44 45 |
# File 'lib/xapit/collection.rb', line 43 def empty? @results ? @results.empty? : size.zero? end |
#facets ⇒ Object
Xapit::Facet objects matching this search query. See class for details.
104 105 106 107 108 |
# File 'lib/xapit/collection.rb', line 104 def facets all_facets.select do |facet| facet..size > 0 end end |
#first ⇒ Object
The first record in the result set.
48 49 50 |
# File 'lib/xapit/collection.rb', line 48 def first fetch_results(:offset => 0, :limit => 1).first end |
#last ⇒ Object
The last record in the result set.
53 54 55 |
# File 'lib/xapit/collection.rb', line 53 def last fetch_results(:offset => size-1, :limit => 1).last end |
#next_page ⇒ Object
The next page number. Returns nil if on last page.
99 100 101 |
# File 'lib/xapit/collection.rb', line 99 def next_page current_page < total_pages ? (current_page + 1): nil end |
#offset ⇒ Object
The offset for the current page
84 85 86 |
# File 'lib/xapit/collection.rb', line 84 def offset @query_parser.offset end |
#per_page ⇒ Object
How many records to display on each page, defaults to 20. Sets with :per_page option when performing search.
79 80 81 |
# File 'lib/xapit/collection.rb', line 79 def per_page @query_parser.per_page end |
#previous_page ⇒ Object
The previous page number. Returns nil if on first page.
94 95 96 |
# File 'lib/xapit/collection.rb', line 94 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#results ⇒ Object
Returns an array of results. You should not need to call this directly because most methods are automatically delegated to this array.
32 33 34 |
# File 'lib/xapit/collection.rb', line 32 def results @results ||= fetch_results end |
#search(keywords, options = {}) ⇒ Object
Perform another search on this one, inheriting all options already passed. See Xapit::Membership for search options.
59 60 61 62 63 |
# File 'lib/xapit/collection.rb', line 59 def search(keywords, = {}) collection = Collection.new(@query_parser.member_class, keywords, ) collection.base_query = @query_parser.query collection end |
#size ⇒ Object Also known as: total_entries
The number of total records found despite any pagination settings.
37 38 39 |
# File 'lib/xapit/collection.rb', line 37 def size @query_parser.query.count end |
#spelling_suggestion ⇒ Object
Includes a suggested variation of a term which will get many more results. Returns nil if no suggestion.
<% if @articles.spelling_suggestion %>
Did you mean <%= link_to h(@articles.spelling_suggestion), :overwrite_params => { :keywords => @articles.spelling_suggestion } %>?
<% end %>
140 141 142 |
# File 'lib/xapit/collection.rb', line 140 def spelling_suggestion @query_parser.spelling_suggestion end |
#total_pages ⇒ Object
Total number of pages with found results.
89 90 91 |
# File 'lib/xapit/collection.rb', line 89 def total_pages (total_entries / per_page.to_f).ceil end |