Class: CloudSearch::Searcher

Inherits:
Object
  • Object
show all
Includes:
ConfigurationChecking
Defined in:
lib/cloud_search/searcher.rb

Instance Method Summary collapse

Constructor Details

#initializeSearcher

Returns a new instance of Searcher.



7
8
9
# File 'lib/cloud_search/searcher.rb', line 7

def initialize
  @response = SearchResponse.new
end

Instance Method Details

#at_page(page) ⇒ Object



53
54
55
56
# File 'lib/cloud_search/searcher.rb', line 53

def at_page(page)
  @page_number = (page && page < 1) ? 1 : page
  self
end

#boolean_query?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cloud_search/searcher.rb', line 35

def boolean_query?
  !!@boolean
end

#items_per_pageObject



49
50
51
# File 'lib/cloud_search/searcher.rb', line 49

def items_per_page
  @response.items_per_page
end

#page_numberObject



58
59
60
# File 'lib/cloud_search/searcher.rb', line 58

def page_number
  @page_number or 1
end

#queryObject



30
31
32
33
# File 'lib/cloud_search/searcher.rb', line 30

def query
  return '' unless @query
  URI.escape(@query).gsub('&', '%26')
end

#searchObject



11
12
13
14
15
16
17
# File 'lib/cloud_search/searcher.rb', line 11

def search
  cloud_search_response = RestClient.get url
  @response.http_code    = cloud_search_response.code
  @response.body         = cloud_search_response.body

  @response
end

#startObject



62
63
64
65
# File 'lib/cloud_search/searcher.rb', line 62

def start
  return 0 if page_number <= 1
  (items_per_page * (page_number - 1)) + 1
end

#urlObject



67
68
69
70
71
72
73
74
# File 'lib/cloud_search/searcher.rb', line 67

def url
  check_configuration_parameters

  "#{CloudSearch.config.search_url}/search".tap do |u|
    u.concat("?#{query_parameter}=#{query}&size=#{items_per_page}&start=#{start}")
    u.concat("&return-fields=#{URI.escape(@fields.join(","))}") if @fields && @fields.any?
  end
end

#with_boolean_query(query) ⇒ Object



24
25
26
27
28
# File 'lib/cloud_search/searcher.rb', line 24

def with_boolean_query(query)
  @boolean = true
  with_query query
  self
end

#with_fields(*fields) ⇒ Object



39
40
41
42
# File 'lib/cloud_search/searcher.rb', line 39

def with_fields(*fields)
  @fields = fields
  self
end

#with_items_per_page(items_per_page) ⇒ Object



44
45
46
47
# File 'lib/cloud_search/searcher.rb', line 44

def with_items_per_page(items_per_page)
  @response.items_per_page = items_per_page
  self
end

#with_query(query) ⇒ Object



19
20
21
22
# File 'lib/cloud_search/searcher.rb', line 19

def with_query(query)
  @query = query
  self
end