Class: AzureSearch::SearchIndexClient

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/azure_search/search_index_client.rb

Overview

Client to perform requests to an Azure Search service.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_name, index_name, api_key) ⇒ SearchIndexClient

Returns a new instance of SearchIndexClient.

Since:

  • 0.1.0



24
25
26
27
28
29
30
31
32
# File 'lib/azure_search/search_index_client.rb', line 24

def initialize(service_name, index_name, api_key)
  if service_name.empty? || index_name.empty? || api_key.empty?
    raise StandardError, "Must provide service_name, index_name and api_key when creating client."
  end

  self.service_name = service_name
  self.index_name = index_name
  self.api_key = api_key
end

Instance Attribute Details

#api_keySymbol

Returns The API key generated for the provisioned Search service.

Returns:

  • (Symbol)

    The API key generated for the provisioned Search service.

Since:

  • 0.1.0



22
23
24
# File 'lib/azure_search/search_index_client.rb', line 22

def api_key
  @api_key
end

#index_nameSymbol

Returns The index name.

Returns:

  • (Symbol)

    The index name.

Since:

  • 0.1.0



20
21
22
# File 'lib/azure_search/search_index_client.rb', line 20

def index_name
  @index_name
end

#service_nameSymbol

Returns The search service name.

Returns:

  • (Symbol)

    The search service name.

Since:

  • 0.1.0



18
19
20
# File 'lib/azure_search/search_index_client.rb', line 18

def service_name
  @service_name
end

Instance Method Details

#batch_insert(operations, chunk_size = 1000) ⇒ Object

Inserts documents in batch.

Parameters:

  • operations (Array<IndexBatchOperation>)

    Array of upload operations.

  • chunk_size (Integer) (defaults to: 1000)

    The batch size. Must not exceed 1000 documents.

Since:

  • 0.1.0



90
91
92
93
94
95
96
97
98
99
# File 'lib/azure_search/search_index_client.rb', line 90

def batch_insert(operations, chunk_size=1000)
  raise "Batch request must not exceed 1000 documents." unless chunk_size <= 1000
  operations.each_slice(chunk_size)
           .to_a
           .each{|op|
             resp = create_request().post(build_indexing_url(), :json => {:value => op})
             raise_on_http_error(resp)
             resp.to_s
           }
end

#create(definition) ⇒ Object

Creates the index based on the provided index definition.

Parameters:

  • definition (Hash)

    The index definition.

Options Hash (definition):

  • :name (String)

    The index name.

  • :fields (Array<IndexField>)

    Array of fields.

  • :suggesters (Array)

    Array of suggesters.

Since:

  • 0.1.0



52
53
54
55
56
57
58
# File 'lib/azure_search/search_index_client.rb', line 52

def create(definition)
  raise "Index definition must be a Hash." unless definition.is_a? Hash
  definition[:name] = self.index_name unless !definition[:name]
  resp = create_request().post(build_index_list_url(), :json => definition)
  raise_on_http_error(resp)
  return JSON.parse(resp.to_s)
end

#create_or_update(definition) ⇒ Object

Creates or updates the index based on the provided index definition.

Parameters:

  • definition (Hash)

    The index definition.

Options Hash (definition):

  • :name (String)

    The index name.

  • :fields (Array<IndexField>)

    Array of fields.

  • :suggesters (Array)

    Array of suggesters.

Since:

  • 0.1.0



66
67
68
69
70
71
72
# File 'lib/azure_search/search_index_client.rb', line 66

def create_or_update(definition)
  raise "Index definition must be a Hash." unless definition.is_a? Hash
  definition[:name] = self.index_name unless definition[:name]
  resp = create_request().put(build_index_definition_url(), :json => definition)
  raise_on_http_error(resp)
  return resp.to_s.empty? ? nil : JSON.parse(resp.to_s)
end

#deleteTrueClass, FalseClass

Deletes the specified index.

Returns:

  • (TrueClass, FalseClass)

    true if the index is deleted, false otherwise.

Since:

  • 0.1.0



77
78
79
80
81
82
83
84
# File 'lib/azure_search/search_index_client.rb', line 77

def delete
  resp = create_request().delete(build_index_definition_url())
  if resp.code == 404
    return false
  end
  raise_on_http_error(resp)
  return true
end

#existsTrueClass, FalseClass

Checks if the specified index exists.

Returns:

  • (TrueClass, FalseClass)

    true if the index exists, false otherwise.

Since:

  • 0.1.0



37
38
39
40
41
42
43
44
# File 'lib/azure_search/search_index_client.rb', line 37

def exists
  resp = create_request().get(build_index_definition_url())
  if resp.code == 404
    return false
  end
  raise_on_http_error(resp)
  return true
end

#lookup(key) ⇒ Hash

Lookup an indexed document by key.

Parameters:

  • key (String)

    The key.

Returns:

  • (Hash)

    The document identified by the supplied key.

Since:

  • 0.1.0



117
118
119
120
121
122
123
# File 'lib/azure_search/search_index_client.rb', line 117

def lookup(key)
  resp = create_request().get(build_index_lookup_url(key))
  raise_on_http_error(resp)
  doc = JSON.parse(resp.to_s)
  doc.delete("@odata.context")
  return doc
end

#search(text, options) ⇒ Hash

Search the index for the supplied text.

Parameters:

  • text (String)

    The text to search for.

  • options (IndexSearchOptions)

    Options to configure the search request.

Returns:

  • (Hash)

    Parsed JSON response.

Since:

  • 0.1.0



107
108
109
110
111
# File 'lib/azure_search/search_index_client.rb', line 107

def search(text, options)
  resp = create_request().get(build_index_search_url(text, options))
  raise_on_http_error(resp)
  return JSON.parse(resp.to_s)
end