Sha256: e66eb0aef510cc39b2fa83e018e001336941da0302efe89bd87a88c9ca142795

Contents?: true

Size: 1.36 KB

Versions: 33

Compression:

Stored size: 1.36 KB

Contents

require 'rsolr'

module Query

  #
  #
  class Solr < Base # :nodoc:all

    attr_reader :server, :index_types

    def initialize *index_types
      @server = RSolr.connect rescue nil
      super *index_types
    end

    #
    #
    def execute tokens, offset = 0
      results = Results::Live.new

      if server
        similar = {}

        new_query = tokens.to_solr_query

        return results if new_query.empty?

        index_types.each do |index|
          begin
            response = server.select q: new_query, fq: "type:#{index.name}", hl: true, :'hl.fl' => '*', :'hl.simple.pre' => '<', :'hl.simple.post' => '>', facet: true
          rescue RSolr::RequestError => re
            return results
          end

          highlighting = response['highlighting']
          possibilities = response['response']['docs'].map do |doc|
            highlights = highlighting[doc['id'].to_s]
            next unless highlights
            selected = doc.select { |key| highlights.has_key?(key) }
            selected.values.join ' '
          end
          possibilities.collect! { |possibility| possibility.strip }.uniq!
          similar[index.name] = possibilities unless possibilities.empty?
        end

        results.add similar: similar
      end

      class << results
        def to_log query
          ?* + super
        end
      end

      results
    end

  end

end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
picky-1.5.3 lib/picky/query/solr.rb
picky-1.5.2 lib/picky/query/solr.rb
picky-1.5.1 lib/picky/query/solr.rb
picky-1.5.0 lib/picky/query/solr.rb
picky-1.4.3 lib/picky/query/solr.rb
picky-1.4.2 lib/picky/query/solr.rb
picky-1.4.1 lib/picky/query/solr.rb
picky-1.4.0 lib/picky/query/solr.rb
picky-1.3.4 lib/picky/query/solr.rb
picky-1.3.3 lib/picky/query/solr.rb
picky-1.3.2 lib/picky/query/solr.rb
picky-1.3.1 lib/picky/query/solr.rb
picky-1.3.0 lib/picky/query/solr.rb