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 7
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 13
def [](key)
  populate

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

  @hash.each(&block)
end
for(facet_values) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 25
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 35
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 54
def populated?
  @populated
end
to_hash() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 58
def to_hash
  populate

  @hash
end

Private Instance Methods

configuration() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 66
def configuration
  ThinkingSphinx::Configuration.instance
end
facet_names(facets) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 94
def facet_names(facets)
  facets.collect(&:name)
end
facets() click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 70
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 86
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 98
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 108
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 104
def max_matches
  configuration.settings['max_matches'] || 1000
end
options_for(facet) click to toggle source
# File lib/thinking_sphinx/facet_search.rb, line 112
def options_for(facet)
  options.merge(
    :select      => [(options[:select] || '*'),
      "#{ThinkingSphinx::SphinxQL.group_by[:select]}",
      "#{ThinkingSphinx::SphinxQL.count[:select]}"
    ].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 76
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