Class: AWSCloudSearch::CloudSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cloud_search/cloud_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, region = "us-east-1") ⇒ CloudSearch


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

def initialize(domain, region="us-east-1")
  @doc_conn = AWSCloudSearch::create_connection( AWSCloudSearch::document_url(domain, region) )
  @search_conn = AWSCloudSearch::create_connection( AWSCloudSearch::search_url(domain, region) )
end

Instance Method Details

#documents_batch(doc_batch) ⇒ Object

Sends a batch of document updates and deletes by invoking the CloudSearch documents/batch API

Raises:

  • (ArgumentError)

15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aws_cloud_search/cloud_search.rb', line 15

def documents_batch(doc_batch)
  raise ArgumentError.new("Invalid argument. Expected DocumentBatch, got #{doc_batch.class}.") unless doc_batch.is_a? DocumentBatch

  resp = @doc_conn.post do |req|
    req.url "/#{AWSCloudSearch::API_VERSION}/documents/batch"
    req.headers['Content-Type'] = 'application/json'
    req.body = doc_batch.to_json
  end
  raise(Exception, "AwsCloudSearchCloud::DocumentService batch returned #{resp.body[:errors].size} errors: #{resp.body[:errors].join(';')}") if resp.body[:status] == 'error'
  resp.body
end

#new_batcherDocumentBatcher

Build a DocumentBatcher linked to this CloudSearch domain


68
69
70
# File 'lib/aws_cloud_search/cloud_search.rb', line 68

def new_batcher
  DocumentBatcher.new(self)
end

#search(search_req) ⇒ Object

Performs a search Note that for strings in the search criteria, any single quotation marks or backslashes in the string must be escaped with a backslash. The #escape method is provided as a convenience to perform the required escaping.

Raises:

  • (ArgumentError)

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aws_cloud_search/cloud_search.rb', line 32

def search(search_req)
  raise ArgumentError.new("Invalid Type: search_request must be of type SearchRequest") unless search_req.is_a? SearchRequest

  resp = @search_conn.get do |req|
    req.url "/#{AWSCloudSearch::API_VERSION}/search", search_req.to_hash
  end

  search_response = SearchResponse.new(resp.body)
  if search_response.error
    raise StandardError.new("Unknown error") if resp.messages.blank?
    code = resp.messages.first['code']
    message = resp.messages.first['message']
    msg = "#{code}: #{message}"
    case code
      when /WildcardTermLimit/
        raise WildcardTermLimit.new(msg)
      when /InvalidFieldOrRankAliasInRankParameter/
        raise InvalidFieldOrRankAliasInRankParameter, msg
      when /UnknownFieldInMatchExpression/
        raise UnknownFieldInMatchExpression, msg
      when /IncorrectFieldTypeInMatchExpression/
        raise IncorrectFieldTypeInMatchExpression, msg
      when /InvalidMatchExpression/
        raise InvalidMatchExpression, msg
      when /UndefinedField/
        raise UndefinedField, msg
      else
        raise AwsCloudSearchError, "Unknown error. #{msg}"
    end
  end

  search_response
end