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.



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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#queryObject

Returns the value of attribute query.



7
8
9
# File 'lib/thinking_sphinx/facet_search.rb', line 7

def query
  @query
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
18
19
# File 'lib/thinking_sphinx/facet_search.rb', line 15

def [](key)
  populate

  @hash[key]
end

#each(&block) ⇒ Object



21
22
23
24
25
# File 'lib/thinking_sphinx/facet_search.rb', line 21

def each(&block)
  populate

  @hash.each(&block)
end

#for(facet_values) ⇒ Object



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

#populateObject



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

Returns:

  • (Boolean)


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

def populated?
  @populated
end

#to_hashObject



60
61
62
63
64
# File 'lib/thinking_sphinx/facet_search.rb', line 60

def to_hash
  populate

  @hash
end