class Riak::Search::Query

A {Riak::Search::Query} wraps a Solr query for Riak Search 2.

Attributes

client[R]

@!attribute [r] client @return [Riak::Client] the client to query against

df[RW]

@!attribute df @return [Array<String>] default fields for Solr to search

filter[RW]

@!attribute filter @return [String] have Solr filter the results prior to returning them

fl[RW]

@!attribute fl @return [Array<String>] fields for Solr to return

index[R]

@!attribute [r] index @return [Riak::Search::Index] the index to query against

op[RW]

@!attribute op @return [String] Solr search operator

rows[RW]

@!attribute rows @return [Numeric] the number of rows to return from the query

sort[RW]

@!attribute sort @return [String] how Solr should sort the result set

start[RW]

@!attribute start @return [Numeric] the offset into the total result set to get results for

term[R]

@!attribute [r] term @return [String] the term to query

Public Class Methods

new(client, index, term, options = { }) click to toggle source

Initializes a query object.

@param [Riak::Client] client the client connected to the Riak cluster @param [String,Riak::Search::Index] index the index to query, either a

{Riak::Search::Index} instance or a {String}

@param [String] term the query term @param [Hash] options a hash of options to quickly set attributes

# File lib/riak/search/query.rb, line 54
def initialize(client, index, term, options = {  })
  @client = client
  validate_index index
  @term = term
  @options = options.symbolize_keys

  set_defaults
  consume_options
end

Public Instance Methods

results() click to toggle source

Get results from the query. Performs the query when called the first time.

@return [Riak::Search::ResultCollection] collection of results

# File lib/riak/search/query.rb, line 67
def results
  return @results if defined? @results

  @results = ResultCollection.new @client, raw_results
end

Private Instance Methods

consume_options() click to toggle source
# File lib/riak/search/query.rb, line 110
def consume_options
  @rows = @options[:rows] if @options[:rows]
  @start = @options[:start] if @options[:start]

  @sort = @options[:sort] if @options[:sort]
  @filter = @options[:filter] if @options[:filter]

  @df = @options[:df] if @options[:df]
  @op = @options[:op] if @options[:op]
  @fl = @options[:fl] if @options[:fl]
end
index_name() click to toggle source
# File lib/riak/search/query.rb, line 75
def index_name
  return @index if @index.is_a? String
  return @index.name
end
prepare_options() click to toggle source
# File lib/riak/search/query.rb, line 122
def prepare_options
  configured_options = {
    rows: @rows,
    start: @start,
    sort: @sort,
    filter: @filter,
    df: @df.join(' '),
    op: @op,
    fl: @fl
  }
  @options.merge configured_options
end
raw_results() click to toggle source
# File lib/riak/search/query.rb, line 135
def raw_results
  @client.backend do |be|
    be.search index_name, @term, prepare_options
  end
end
set_defaults() click to toggle source
# File lib/riak/search/query.rb, line 96
def set_defaults
  @rows = nil
  @start = nil

  @sort = nil
  @filter = nil

  @df = %w{text}
  @op = nil
  @fl = %w{_yz_rb _yz_rk _yz_rt score}

  @presort = nil
end
validate_index(index) click to toggle source
# File lib/riak/search/query.rb, line 80
def validate_index(index)
  if index.is_a? String
    index = Riak::Search::Index.new @client, index
  end

  unless index.is_a? Riak::Search::Index
    raise Riak::SearchError::IndexArgumentError.new index
  end

  unless index.exists?
    raise Riak::SearchError::IndexNonExistError.new index.name
  end

  @index = index
end