Class: ThinkingSphinx::FacetSearch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/thinking_sphinx/facet_search.rb

Defined Under Namespace

Classes: Filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query = nil, options = {}) ⇒ FacetSearch

Returns a new instance of FacetSearch



7
8
9
10
11
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



4
5
6
# File 'lib/thinking_sphinx/facet_search.rb', line 4

def options
  @options
end

#queryObject

Returns the value of attribute query



5
6
7
# File 'lib/thinking_sphinx/facet_search.rb', line 5

def query
  @query
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
16
17
# File 'lib/thinking_sphinx/facet_search.rb', line 13

def [](key)
  populate

  @hash[key]
end

#each(&block) ⇒ Object



19
20
21
22
23
# File 'lib/thinking_sphinx/facet_search.rb', line 19

def each(&block)
  populate

  @hash.each(&block)
end

#for(facet_values) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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

#populateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/thinking_sphinx/facet_search.rb', line 54

def populated?
  @populated
end

#to_hashObject



58
59
60
61
62
# File 'lib/thinking_sphinx/facet_search.rb', line 58

def to_hash
  populate

  @hash
end