Class: AzureSearch::SearchIndexClient
- Inherits:
-
Object
- Object
- AzureSearch::SearchIndexClient
- Includes:
- Errors
- Defined in:
- lib/azure_search/search_index_client.rb
Overview
Client to perform requests to an Azure Search service.
Instance Attribute Summary collapse
-
#api_key ⇒ Symbol
The API key generated for the provisioned Search service.
-
#index_name ⇒ Symbol
The index name.
-
#service_name ⇒ Symbol
The search service name.
Instance Method Summary collapse
-
#batch_insert(operations, chunk_size = 1000) ⇒ Object
Inserts documents in batch.
-
#create(definition) ⇒ Object
Creates the index based on the provided index definition.
-
#create_or_update(definition) ⇒ Object
Creates or updates the index based on the provided index definition.
-
#delete ⇒ TrueClass, FalseClass
Deletes the specified index.
-
#exists ⇒ TrueClass, FalseClass
Checks if the specified index exists.
-
#initialize(service_name, index_name, api_key) ⇒ SearchIndexClient
constructor
A new instance of SearchIndexClient.
-
#lookup(key) ⇒ Hash
Lookup an indexed document by key.
-
#search(text, options) ⇒ Hash
Search the index for the supplied text.
Constructor Details
#initialize(service_name, index_name, api_key) ⇒ SearchIndexClient
Returns a new instance of SearchIndexClient.
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_key ⇒ Symbol
Returns The API key generated for the provisioned Search service.
22 23 24 |
# File 'lib/azure_search/search_index_client.rb', line 22 def api_key @api_key end |
#index_name ⇒ Symbol
Returns The index name.
20 21 22 |
# File 'lib/azure_search/search_index_client.rb', line 20 def index_name @index_name end |
#service_name ⇒ Symbol
Returns The search service name.
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.
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.
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.
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 |
#delete ⇒ TrueClass, FalseClass
Deletes the specified index.
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 |
#exists ⇒ TrueClass, FalseClass
Checks if the specified index exists.
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.
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.
107 108 109 110 111 |
# File 'lib/azure_search/search_index_client.rb', line 107 def search(text, ) resp = create_request().get(build_index_search_url(text, )) raise_on_http_error(resp) return JSON.parse(resp.to_s) end |