class ElasticSearch::SearchRequest

Constants

QueryStringQueryBuilder

The 'xcontent' namespace was removed in elasticsearch 0.17.0

Public Class Methods

new(client) click to toggle source

Create a new index request.

Calls superclass method ElasticSearch::Request::new
# File lib/jruby-elasticsearch/searchrequest.rb, line 14
def initialize(client)
  @client = client
  # Try the 0.19 API
  begin
    @prep = org.elasticsearch.action.search.SearchRequestBuilder.new(@client)
  rescue NameError
    # Okay so maybe the pre-0.19 API works?
    @prep = org.elasticsearch.client.action.search.SearchRequestBuilder.new(@client)
  end
  @indeces = []
  super()
end

Public Instance Methods

count(s)
Alias for: size
execute(&block) click to toggle source

Execute this search request. This call is asynchronous.

If a block is given, register it for both failure and success.

On success, callback will receive a org.elasticsearch.action.search.SearchResponse

# File lib/jruby-elasticsearch/searchrequest.rb, line 46
def execute(&block)
  use_callback(&block) if block_given?
  @prep.setIndices(@indeces.to_java(:String))
  action = @prep.execute(@handler)
  return action
end
execute!() click to toggle source

Execute this index request synchronously Returns an org.elasticsearch.action.search.SearchResponse

# File lib/jruby-elasticsearch/searchrequest.rb, line 56
def execute!
  @prep.setIndices(@indeces.to_java(:String))
  return @prep.execute.actionGet()
end
from(from) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 131
def from(from)
  @prep.setFrom(from)
  return self
end
Also aliased as: offset, offset
histogram(field, interval, name=nil) click to toggle source

Add a histogram facet to this query. Can be invoked multiple times.

# File lib/jruby-elasticsearch/searchrequest.rb, line 96
def histogram(field, interval, name=nil)
  if name.nil?
    # TODO(sissel): How do we expose the name of the histogram?
    name = "#{field}_#{interval}"
  end
  # TODO(sissel): Support 'global' ?
  builder = org.elasticsearch.search.facet.histogram.HistogramFacetBuilder.new(name)
  builder.field(field)
  builder.interval(interval)
  @prep.addFacet(builder)
  return self
end
index(index_name) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 34
def index(index_name)
  @indeces << index_name
end
limit(s)
Alias for: size
offset(from)
Alias for: from
query(query_string, default_operator=:and) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 76
def query(query_string, default_operator=:and)
  # TODO(sissel): allow doing other queries and such.
  qbuilder = QueryStringQueryBuilder.new(query_string)

  operator = QueryStringQueryBuilder::Operator
  case default_operator
    when :and
      qbuilder.defaultOperator(operator::AND)
    when :or
      qbuilder.defaultOperator(operator::OR)
    else
      raise "Unknown default operator '#{default_operator.inspect}'"
  end

  @prep.setQuery(qbuilder)
  return self
end
size(s) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 123
def size(s)
  @prep.setSize(s)
  return self
end
Also aliased as: count, limit
sort(field, order) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 62
def sort(field, order)
  case order
    when :asc
      order_val = org.elasticsearch.search.sort.SortOrder::ASC
    when :desc
      order_val = org.elasticsearch.search.sort.SortOrder::DESC
    else
      raise "Invalid sort order '#{order.inspect}'"
  end
  @prep.addSort(field, order_val)
  return self
end
terms(field, name=nil) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 110
def terms(field, name=nil)
  if name.nil?
    # TODO(sissel): How do we expose the name of the histogram?
    name = field
  end
  # TODO(sissel): Support 'global' ?
  builder = org.elasticsearch.search.facet.terms.TermsFacetBuilder.new(name)
  builder.field(field)
  @prep.addFacet(builder)
  return self
end
with(&block) click to toggle source
# File lib/jruby-elasticsearch/searchrequest.rb, line 28
def with(&block)
  instance_eval(&block)
  return self
end