Sha256: babeabac0c9820ec1f6f60e480b526a8b8f45db769edfdf7adc076e6801b0c36

Contents?: true

Size: 1.69 KB

Versions: 15

Compression:

Stored size: 1.69 KB

Contents

require 'rsolr'

module Query

  #
  #
  class Solr < Base

    attr_reader :server, :index_types

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

    # # This runs the actual search.
    # #
    # # TODO Remove!
    # #
    # def search tokens, offset = 0
    #   results = nil
    #
    #   duration = timed do
    #     results = execute(tokens, offset) || empty_results # TODO Does not work yet
    #   end
    #   results.duration = duration
    #
    #   results
    # 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

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

      results
    end

  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
picky-0.12.3 lib/picky/query/solr.rb
picky-0.12.2 lib/picky/query/solr.rb
picky-0.12.1 lib/picky/query/solr.rb
picky-0.12.0 lib/picky/query/solr.rb
picky-0.11.2 lib/picky/query/solr.rb
picky-0.11.1 lib/picky/query/solr.rb
picky-0.11.0 lib/picky/query/solr.rb
picky-0.10.5 lib/picky/query/solr.rb
picky-0.10.4 lib/picky/query/solr.rb
picky-0.10.2 lib/picky/query/solr.rb
picky-0.10.1 lib/picky/query/solr.rb
picky-0.10.0 lib/picky/query/solr.rb
picky-0.9.4 lib/picky/query/solr.rb
picky-0.9.3 lib/picky/query/solr.rb
picky-0.9.2 lib/picky/query/solr.rb