Class: Xapit::Facet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_identifiersObject

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_identifiersObject



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

#nameObject

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

#optionsObject

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 options
  unfiltered_options.select { |o| o.count < @query.count }
end

#unfiltered_optionsObject

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 unfiltered_options
  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