class ThinkingSphinx::FacetSearch

Attributes

options[R]
query[RW]

Public Class Methods

new(query = nil, options = {}) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 9
def initialize(query = nil, options = {})
  query, options   = nil, query if query.is_a?(Hash)
  @query, @options = query, options
  @hash             = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 15
def [](key)
  populate

  @hash[key]
end
each(&block) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 21
def each(&block)
  populate

  @hash.each(&block)
end
for(facet_values) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 27
def for(facet_values)
  filter_facets = facet_values.keys.collect { |key|
    facets.detect { |facet| facet.name == key.to_s }
  }

  ThinkingSphinx::Search.new query, options.merge(
    :indices => index_names_for(*filter_facets)
  ).merge(Filter.new(facets, facet_values).to_hash)
end
populate() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 37
def populate
  return if @populated

  batch = ThinkingSphinx::BatchedSearch.new
  facets.each do |facet|
    batch.searches << ThinkingSphinx::Search.new(query, options_for(facet))
  end

  batch.populate ThinkingSphinx::Middlewares::RAW_ONLY

  facets.each_with_index do |facet, index|
    @hash[facet.name.to_sym] = facet.results_from batch.searches[index].raw
  end

  @hash[:class] = @hash[:sphinx_internal_class]

  @populated = true
end
populated?() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 56
def populated?
  @populated
end
to_hash() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 60
def to_hash
  populate

  @hash
end

Private Instance Methods

configuration() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 68
def configuration
  ThinkingSphinx::Configuration.instance
end
facet_names(facets) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 96
def facet_names(facets)
  facets.collect(&:name)
end
facets() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 72
def facets
  @facets ||= properties.group_by(&:name).collect { |name, matches|
    ThinkingSphinx::Facet.new name, matches
  }
end
index_names_for(*facets) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 88
def index_names_for(*facets)
  facet_names(
    indices.select do |index|
      facets.all? { |facet| facet_names(index.facets).include?(facet.name) }
    end
  )
end
indices() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 100
def indices
  @indices ||= configuration.index_set_class.new(
    options.slice(:classes, :indices)
  )
end
limit() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 110
def limit
  limit = options[:limit] || options[:per_page] || max_matches
end
max_matches() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 106
def max_matches
  configuration.settings['max_matches'] || 1000
end
options_for(facet) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 114
def options_for(facet)
  options.merge(
    :select      => [(options[:select] || '*'),
      "groupby() AS sphinx_internal_group",
      "id AS sphinx_document_id, count(DISTINCT sphinx_document_id) AS sphinx_internal_count"
    ].join(', '),
    :group_by    => facet.name,
    :indices     => index_names_for(facet),
    :max_matches => max_matches,
    :limit       => limit
  )
end
properties() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 78
def properties
  properties = indices.collect(&:facets).flatten
  if options[:facets].present?
    properties = properties.select { |property|
      options[:facets].include? property.name.to_sym
    }
  end
  properties
end