Sha256: 1ae12e1fbfd489aebaa40a2963b7e168b44fbea06acfb4f8b393fb2cdb75fa9a

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

class ThinkingSphinx::Middlewares::Inquirer <
  ThinkingSphinx::Middlewares::Middleware

  def call(contexts)
    @contexts = contexts
    @batch    = nil

    ThinkingSphinx::Logger.log :query, combined_queries do
      batch.results
    end

    index = 0
    contexts.each do |context|
      Inner.new(context).call batch.results[index], batch.results[index + 1]

      index += 2
    end

    app.call contexts
  end

  private

  def batch
    @batch ||= begin
      batch = ThinkingSphinx::Search::BatchInquirer.new

      @contexts.each do |context|
        batch.append_query context[:sphinxql].to_sql
        batch.append_query Riddle::Query.meta
      end

      batch
    end
  end

  def combined_queries
    @contexts.collect { |context| context[:sphinxql].to_sql }.join('; ')
  end

  class Inner
    def initialize(context)
      @context = context
    end

    def call(raw_results, meta_results)
      context[:results] = raw_results.to_a
      context[:raw]     = context[:results].dup
      context[:meta]    = meta_results.inject({}) { |hash, row|
        hash[row['Variable_name']] = row['Value']
        hash
      }

      total = context[:meta]['total_found']
      ThinkingSphinx::Logger.log :message, "Found #{total} result#{'s' unless total == 1}"
    end

    private

    attr_reader :context
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
thinking-sphinx-3.4.2 lib/thinking_sphinx/middlewares/inquirer.rb
thinking-sphinx-3.4.1 lib/thinking_sphinx/middlewares/inquirer.rb
thinking-sphinx-3.4.0 lib/thinking_sphinx/middlewares/inquirer.rb
thinking-sphinx-3.3.0 lib/thinking_sphinx/middlewares/inquirer.rb
thinking-sphinx-3.2.0 lib/thinking_sphinx/middlewares/inquirer.rb