Class: Thebes::SphinxSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/thebes/sphinx_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SphinxSearch

Returns a new instance of SphinxSearch.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/thebes/sphinx_search.rb', line 9

def initialize options
  @options = options
  @client = Riddle::Client.new(*server)
  @client.limit = options[:per_page] || 20
  @client.offset = offset
  @client.filters = filters
  @client.field_weights = options[:field_weights] if options[:field_weights]
  @client.index_weights = options[:index_weights] || {}
  @client.sort_by = options[:sort_by] if options[:sort_by]
  @client.match_mode = :extended
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



7
8
9
# File 'lib/thebes/sphinx_search.rb', line 7

def results
  @results
end

Instance Method Details

#search(query) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thebes/sphinx_search.rb', line 21

def search query
  logger.info "Querying: '#{query}'"
  @total_pages = nil
  @results = { }
  runtime = Benchmark.realtime {
    @results = @client.query(format_query(query), indexes, '')
  }
  logger.error("Sphinx returned an error: #{@results[:error]}") if !@results[:error].blank?
  logger.warn("Sphinx returned a warning: #{@results[:error]}") if !@results[:warning].blank?
  logger.info "Found #{@results[:total_found]} results out of #{total_pages}"
  logger.debug "Sphinx (#{sprintf("%f", runtime)}s)"
  if @options[:ids_only]
    @results[:matches].map{ |match|
      match[:attributes]["sphinx_internal_id"]
    }
  else
    instances_from_matches
  end
end

#total_pagesObject



41
42
43
44
# File 'lib/thebes/sphinx_search.rb', line 41

def total_pages
  return 0 if @results[:total].nil?
  @total_pages ||= (@results[:total] / per_page.to_f).ceil
end