Class: Xapit::Facet
- Inherits:
-
Object
- Object
- Xapit::Facet
- Defined in:
- lib/xapit/facet.rb
Overview
Facets allow users to further filter the result set based on certain attributes. You should fetch facets by calling “facets” on a Xapit::Collection search result.
<% for facet in @articles.facets %>
<%= facet.name %>
<% for option in facet.options %>
<%= link_to option.name, :overwrite_params => { :facets => option } %>
(<%= option.count %>)
<% end %>
<% end %>
See Xapit::FacetBlueprint for details on how to index a facet.
Instance Attribute Summary collapse
-
#existing_facet_identifiers ⇒ Object
Returns the value of attribute existing_facet_identifiers.
Instance Method Summary collapse
-
#initialize(blueprint, query, existing_facet_identifiers) ⇒ Facet
constructor
A new instance of Facet.
- #matching_identifiers ⇒ Object
-
#name ⇒ Object
The name of the facet.
-
#options ⇒ Object
Xapit::FacetOption objects for this facet which match the current query.
-
#unfiltered_options ⇒ Object
Xapit::FacetOption objects for this facet which match the current query.
Constructor Details
#initialize(blueprint, query, existing_facet_identifiers) ⇒ Facet
Returns a new instance of Facet.
17 18 19 20 21 |
# File 'lib/xapit/facet.rb', line 17 def initialize(blueprint, query, existing_facet_identifiers) @blueprint = blueprint @query = query.dup @existing_facet_identifiers = existing_facet_identifiers end |
Instance Attribute Details
#existing_facet_identifiers ⇒ Object
Returns the value of attribute existing_facet_identifiers.
15 16 17 |
# File 'lib/xapit/facet.rb', line 15 def existing_facet_identifiers @existing_facet_identifiers end |
Instance Method Details
#matching_identifiers ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xapit/facet.rb', line 42 def matching_identifiers result = {} matches.each do |match| identifiers = match.document.terms.map(&:term).grep(/^F/).map { |t| t[1..-1] } identifiers.each do |identifier| unless existing_facet_identifiers.include? identifier result[identifier] ||= 0 result[identifier] += (match.collapse_count + 1) end end end result end |
#name ⇒ Object
The name of the facet. See Xapit::FacetBlueprint for details.
57 58 59 |
# File 'lib/xapit/facet.rb', line 57 def name @blueprint.name end |
#options ⇒ Object
Xapit::FacetOption objects for this facet which match the current query. Hides options which don’t narrow down results.
25 26 27 |
# File 'lib/xapit/facet.rb', line 25 def .select { |o| o.count < @query.count } end |
#unfiltered_options ⇒ Object
Xapit::FacetOption objects for this facet which match the current query. Includes options which may not narrow down result set.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/xapit/facet.rb', line 31 def matching_identifiers.map do |identifier, count| option = FacetOption.find(identifier) if option.facet.attribute == @blueprint.attribute option.count = count option.existing_facet_identifiers = @existing_facet_identifiers option end end.compact.sort_by(&:name) end |