Skip to content

Commit 9f01578

Browse files
committed
feat(DiscoveryV1): add support for autocompletion
1 parent 598faef commit 9f01578

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

lib/ibm_watson/discovery_v1.rb

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ def delete_document(environment_id:, collection_id:, document_id:)
13051305
#########################
13061306

13071307
##
1308-
# @!method query(environment_id:, collection_id:, filter: nil, query: nil, natural_language_query: nil, passages: nil, aggregation: nil, count: nil, _return: nil, offset: nil, sort: nil, highlight: nil, passages_fields: nil, passages_count: nil, passages_characters: nil, deduplicate: nil, deduplicate_field: nil, similar: nil, similar_document_ids: nil, similar_fields: nil, bias: nil, x_watson_logging_opt_out: nil)
1308+
# @!method query(environment_id:, collection_id:, filter: nil, query: nil, natural_language_query: nil, passages: nil, aggregation: nil, count: nil, _return: nil, offset: nil, sort: nil, highlight: nil, passages_fields: nil, passages_count: nil, passages_characters: nil, deduplicate: nil, deduplicate_field: nil, similar: nil, similar_document_ids: nil, similar_fields: nil, bias: nil, spelling_suggestions: nil, x_watson_logging_opt_out: nil)
13091309
# Query a collection.
13101310
# By using this method, you can construct long queries. For details, see the
13111311
# [Discovery
@@ -1364,9 +1364,15 @@ def delete_document(environment_id:, collection_id:, document_id:)
13641364
# a **number** type field is specified, returned results are biased towards higher
13651365
# field values. This parameter cannot be used in the same query as the **sort**
13661366
# parameter.
1367+
# @param spelling_suggestions [Boolean] When `true` and the **natural_language_query** parameter is used, the
1368+
# **natural_languge_query** parameter is spell checked. The most likely correction
1369+
# is retunred in the **suggested_query** field of the response (if one exists).
1370+
#
1371+
# **Important:** this parameter is only valid when using the Cloud Pak version of
1372+
# Discovery.
13671373
# @param x_watson_logging_opt_out [Boolean] If `true`, queries are not stored in the Discovery **Logs** endpoint.
13681374
# @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
1369-
def query(environment_id:, collection_id:, filter: nil, query: nil, natural_language_query: nil, passages: nil, aggregation: nil, count: nil, _return: nil, offset: nil, sort: nil, highlight: nil, passages_fields: nil, passages_count: nil, passages_characters: nil, deduplicate: nil, deduplicate_field: nil, similar: nil, similar_document_ids: nil, similar_fields: nil, bias: nil, x_watson_logging_opt_out: nil)
1375+
def query(environment_id:, collection_id:, filter: nil, query: nil, natural_language_query: nil, passages: nil, aggregation: nil, count: nil, _return: nil, offset: nil, sort: nil, highlight: nil, passages_fields: nil, passages_count: nil, passages_characters: nil, deduplicate: nil, deduplicate_field: nil, similar: nil, similar_document_ids: nil, similar_fields: nil, bias: nil, spelling_suggestions: nil, x_watson_logging_opt_out: nil)
13701376
raise ArgumentError.new("environment_id must be provided") if environment_id.nil?
13711377

13721378
raise ArgumentError.new("collection_id must be provided") if collection_id.nil?
@@ -1400,7 +1406,8 @@ def query(environment_id:, collection_id:, filter: nil, query: nil, natural_lang
14001406
"similar" => similar,
14011407
"similar.document_ids" => similar_document_ids,
14021408
"similar.fields" => similar_fields,
1403-
"bias" => bias
1409+
"bias" => bias,
1410+
"spelling_suggestions" => spelling_suggestions
14041411
}
14051412

14061413
method_url = "/v1/environments/%s/collections/%s/query" % [ERB::Util.url_encode(environment_id), ERB::Util.url_encode(collection_id)]
@@ -1709,6 +1716,49 @@ def federated_query_notices(environment_id:, collection_ids:, filter: nil, query
17091716
)
17101717
response
17111718
end
1719+
1720+
##
1721+
# @!method get_autocompletion(environment_id:, collection_id:, field: nil, prefix: nil, count: nil)
1722+
# Get Autocomplete Suggestions.
1723+
# Returns completion query suggestions for the specified prefix. /n/n
1724+
# **Important:** this method is only valid when using the Cloud Pak version of
1725+
# Discovery.
1726+
# @param environment_id [String] The ID of the environment.
1727+
# @param collection_id [String] The ID of the collection.
1728+
# @param field [String] The field in the result documents that autocompletion suggestions are identified
1729+
# from.
1730+
# @param prefix [String] The prefix to use for autocompletion. For example, the prefix `Ho` could
1731+
# autocomplete to `Hot`, `Housing`, or `How do I upgrade`. Possible completions are.
1732+
# @param count [Fixnum] The number of autocompletion suggestions to return.
1733+
# @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
1734+
def get_autocompletion(environment_id:, collection_id:, field: nil, prefix: nil, count: nil)
1735+
raise ArgumentError.new("environment_id must be provided") if environment_id.nil?
1736+
1737+
raise ArgumentError.new("collection_id must be provided") if collection_id.nil?
1738+
1739+
headers = {
1740+
}
1741+
sdk_headers = Common.new.get_sdk_headers("discovery", "V1", "get_autocompletion")
1742+
headers.merge!(sdk_headers)
1743+
1744+
params = {
1745+
"version" => @version,
1746+
"field" => field,
1747+
"prefix" => prefix,
1748+
"count" => count
1749+
}
1750+
1751+
method_url = "/v1/environments/%s/collections/%s/autocompletion" % [ERB::Util.url_encode(environment_id), ERB::Util.url_encode(collection_id)]
1752+
1753+
response = request(
1754+
method: "GET",
1755+
url: method_url,
1756+
headers: headers,
1757+
params: params,
1758+
accept_json: true
1759+
)
1760+
response
1761+
end
17121762
#########################
17131763
# Training data
17141764
#########################

test/unit/test_discovery_v1.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,30 @@ def test_federated_query_notices
10291029
assert_equal({ "received" => "true" }, service_response.result)
10301030
end
10311031

1032+
def test_get_autocompletion
1033+
authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
1034+
username: "username",
1035+
password: "password"
1036+
)
1037+
service = IBMWatson::DiscoveryV1.new(
1038+
version: "2018-03-05",
1039+
authenticator: authenticator
1040+
)
1041+
stub_request(:get, "https://gateway.watsonplatform.net/discovery/api/v1/environments/envid/collections/collid/autocompletion?version=2018-03-05")
1042+
.with(
1043+
headers: {
1044+
"Accept" => "application/json",
1045+
"Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
1046+
"Host" => "gateway.watsonplatform.net"
1047+
}
1048+
).to_return(status: 200, body: { "received" => "true" }.to_json, headers: { "Content-Type" => "application/json" })
1049+
service_response = service.get_autocompletion(
1050+
environment_id: "envid",
1051+
collection_id: "collid"
1052+
)
1053+
assert_equal({ "received" => "true" }, service_response.result)
1054+
end
1055+
10321056
def test_list_training_examples
10331057
authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
10341058
username: "username",

0 commit comments

Comments
 (0)