use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testAddTrainingDataWOptions.
// Test the addTrainingData operation with a valid options model parameter
@Test
public void testAddTrainingDataWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"examples\": [{\"document_id\": \"documentId\", \"cross_reference\": \"crossReference\", \"relevance\": 9}]}";
String addTrainingDataPath = "/v1/environments/testString/collections/testString/training_data";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the TrainingExample model
TrainingExample trainingExampleModel = new TrainingExample.Builder().documentId("testString").crossReference("testString").relevance(Long.valueOf("26")).build();
// Construct an instance of the AddTrainingDataOptions model
AddTrainingDataOptions addTrainingDataOptionsModel = new AddTrainingDataOptions.Builder().environmentId("testString").collectionId("testString").naturalLanguageQuery("testString").filter("testString").examples(new java.util.ArrayList<TrainingExample>(java.util.Arrays.asList(trainingExampleModel))).build();
// Invoke addTrainingData() with a valid options model and verify the result
Response<TrainingQuery> response = discoveryService.addTrainingData(addTrainingDataOptionsModel).execute();
assertNotNull(response);
TrainingQuery responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, addTrainingDataPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.
the class Discovery method addTrainingData.
/**
* Add query to training data.
*
* <p>Adds a query to the training data for this collection. The query can contain a filter and
* natural language query.
*
* @param addTrainingDataOptions the {@link AddTrainingDataOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
*/
public ServiceCall<TrainingQuery> addTrainingData(AddTrainingDataOptions addTrainingDataOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(addTrainingDataOptions, "addTrainingDataOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", addTrainingDataOptions.environmentId());
pathParamsMap.put("collection_id", addTrainingDataOptions.collectionId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/training_data", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "addTrainingData");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
final JsonObject contentJson = new JsonObject();
if (addTrainingDataOptions.naturalLanguageQuery() != null) {
contentJson.addProperty("natural_language_query", addTrainingDataOptions.naturalLanguageQuery());
}
if (addTrainingDataOptions.filter() != null) {
contentJson.addProperty("filter", addTrainingDataOptions.filter());
}
if (addTrainingDataOptions.examples() != null) {
contentJson.add("examples", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(addTrainingDataOptions.examples()));
}
builder.bodyJson(contentJson);
ResponseConverter<TrainingQuery> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingQuery>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.
the class Discovery method getTrainingData.
/**
* Get details about a query.
*
* <p>Gets details for a specific training data query, including the query string and all
* examples.
*
* @param getTrainingDataOptions the {@link GetTrainingDataOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
*/
public ServiceCall<TrainingQuery> getTrainingData(GetTrainingDataOptions getTrainingDataOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getTrainingDataOptions, "getTrainingDataOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", getTrainingDataOptions.environmentId());
pathParamsMap.put("collection_id", getTrainingDataOptions.collectionId());
pathParamsMap.put("query_id", getTrainingDataOptions.queryId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/training_data/{query_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "getTrainingData");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
ResponseConverter<TrainingQuery> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingQuery>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.
the class Discovery method updateTrainingQuery.
/**
* Update a training query.
*
* <p>Updates an existing training query and it's examples.
*
* @param updateTrainingQueryOptions the {@link UpdateTrainingQueryOptions} containing the options
* for the call
* @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
*/
public ServiceCall<TrainingQuery> updateTrainingQuery(UpdateTrainingQueryOptions updateTrainingQueryOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(updateTrainingQueryOptions, "updateTrainingQueryOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", updateTrainingQueryOptions.projectId());
pathParamsMap.put("query_id", updateTrainingQueryOptions.queryId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/training_data/queries/{query_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "updateTrainingQuery");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
final JsonObject contentJson = new JsonObject();
contentJson.addProperty("natural_language_query", updateTrainingQueryOptions.naturalLanguageQuery());
contentJson.add("examples", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateTrainingQueryOptions.examples()));
if (updateTrainingQueryOptions.filter() != null) {
contentJson.addProperty("filter", updateTrainingQueryOptions.filter());
}
builder.bodyJson(contentJson);
ResponseConverter<TrainingQuery> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingQuery>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations