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

- (FacetSearch) initialize(query = nil, options = {})

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

- (Object) options (readonly)

Returns the value of attribute options



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

def options
  @options
end

- (Object) query

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

- (Object) [](key)



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

def [](key)
  populate

  @hash[key]
end

- (Object) each(&block)



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

def each(&block)
  populate

  @hash.each(&block)
end

- (Object) for(facet_values)



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

- (Object) populate



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

- (Object) to_hash



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

def to_hash
  populate

  @hash
end