Class: Blodsband::Riak::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/blodsband/riak/search.rb

Constant Summary collapse

BOOLS =
Set.new(["AND", "OR"])

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Search

Create a search instance.

Parameters:

  • url (URI)

    a URI to the HTTP port of a Riak node with Riak Search enabled.



15
16
17
# File 'lib/blodsband/riak/search.rb', line 15

def initialize(url)
  @url = url
end

Instance Method Details

#aindex!(document) ⇒ Blodsband::Riak::Future<Object>

Index a document in Riak Search.

Parameters:

  • document (Hash)

    the object to index. All keys and values will be used to create the indexed document.

Returns:

  • (Blodsband::Riak::Future<Object>)

    a future that returns when the indexing is finished.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blodsband/riak/search.rb', line 79

def aindex!(document)
  mult = Multi.new
  mult.add(:resp, 
           client(URI.join(@url.to_s, 
                           "/solr/update")).
           apost(:head => { 'Content-Type' => 'text/xml' },
                 :body => add_document_xml(document).to_s))
  return(Future.new do
           mult.really_perform
           mult.responses[:callback][:resp].response
         end)
end

#asearch(query, options = {}) ⇒ Blodsband::Future<Hash<Symbol, Object>>

Search for documents in Riak Search.

Parameters:

  • url (URI)

    a url pointing to the HTML port of a Riak node.

Returns:

  • (Blodsband::Future<Hash<Symbol, Object>>)

    an eventual Hash :total => number_of_results, :results => [doc1, doc2, ...]



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blodsband/riak/search.rb', line 25

def asearch(query, options = {})
  options = options.clone
  fields = options.delete(:fields)
  if fields
    asearch(multi_field_query(query, fields), options)
  else
    mult = Multi.new
    mult.add(:resp, 
             client(URI.join(@url.to_s, 
                             "/solr/select")).
             aget(:query => query_with_options(query, options)))
    return(Future.new do
             mult.really_perform
             response = mult.responses[:callback][:resp].response['response']
             rval = {
               :total => response['numFound'],
               :results => response['docs'].map do |x| 
                 x['fields'] 
               end
             }
             class << rval
               attr_reader :max_score
             end
             rval.instance_eval do
               @max_score = response['maxScore']
             end
             rval
           end)
  end
end

#index!(document) ⇒ Object

Index a document in Riak Search.

Parameters:

  • document (Hash)

    the object to index. All keys and values will be used to create the indexed document.



97
98
99
# File 'lib/blodsband/riak/search.rb', line 97

def index!(document)
  aindex!(document).get
end

#search(query, options = {}) ⇒ Hash<Symbol, Object>

Search for documents in Riak Search.

Parameters:

  • query (String)

    the query to use when searching. See http://wiki.basho.com/Riak-Search---Querying.html for more information.

  • options (Hash<Symbol, Object>) (defaults to: {})
    :default_field
    String

    the default field to apply to all terms in the query that lack their own field specifications.

    :per_page
    Integer

    the maximum number of hits to return.

    :page
    Integer

    the page number (starting at 1) that you want returned

    :fields
    Array<String>

    if you want to search for terms without field specification among multiple fields, provide :fields instead of :default_field. This will emulate (slightly) the dismax functionality of Solr by rewriting the query and adding each field you define to each unfielded term.

Returns:

  • (Hash<Symbol, Object>)

    a Hash :total => number_of_results, :results => [doc1, doc2, ...]



68
69
70
# File 'lib/blodsband/riak/search.rb', line 68

def search(query, options = {})
  asearch(query, options).get
end