Class: Thebes::SphinxSearch
- Inherits:
-
Object
- Object
- Thebes::SphinxSearch
- Defined in:
- lib/thebes/sphinx_search.rb
Instance Attribute Summary collapse
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#initialize(options) ⇒ SphinxSearch
constructor
A new instance of SphinxSearch.
- #search(query) ⇒ Object
- #total_pages ⇒ Object
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 = @client = Riddle::Client.new(*server) @client.limit = [:per_page] || 20 @client.offset = offset @client.filters = filters @client.field_weights = [:field_weights] if [:field_weights] @client.index_weights = [:index_weights] || {} @client.sort_by = [:sort_by] if [:sort_by] @client.match_mode = :extended end |
Instance Attribute Details
#results ⇒ Object (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_pages ⇒ Object
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 |