use of com.ibm.watson.discovery.v2.Discovery in project java-sdk by watson-developer-cloud.
the class DiscoveryV2Example method main.
public static void main(String[] args) throws IOException {
Authenticator authenticator = new BearerTokenAuthenticator("{bearer_token}");
Discovery service = new Discovery("2019-11-22", authenticator);
service.setServiceUrl("{url}");
// This example assumes you have a project and collection set up which can accept documents.
// Paste those
// IDs below.
String projectId = "";
String collectionId = "";
// Add a new document to our collection. Fill in the file path with the file you want to send.
InputStream file = new FileInputStream("");
AddDocumentOptions addDocumentOptions = new AddDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).file(file).filename("example-file").build();
DocumentAccepted addResponse = service.addDocument(addDocumentOptions).execute().getResult();
String documentId = addResponse.getDocumentId();
// Query your collection with the new document inside.
QueryOptions queryOptions = new QueryOptions.Builder().projectId(projectId).addCollectionIds(collectionId).naturalLanguageQuery(// Feel free to replace this to query something different.
"Watson").build();
QueryResponse queryResponse = service.query(queryOptions).execute().getResult();
System.out.println(queryResponse.getMatchingResults() + " results were returned by the query!");
// See if the added document got returned by the query.
for (QueryResult result : queryResponse.getResults()) {
if (result.getDocumentId().equals(documentId)) {
System.out.println("Our new document matched the query!");
}
}
// Delete our uploaded document from the collection.
DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).documentId(documentId).build();
service.deleteDocument(deleteDocumentOptions).execute();
}
use of com.ibm.watson.discovery.v2.Discovery in project java-sdk by watson-developer-cloud.
the class Discovery method getTrainingQuery.
/**
* Get a training data query.
*
* <p>Get details for a specific training data query, including the query string and all examples.
*
* @param getTrainingQueryOptions the {@link GetTrainingQueryOptions} containing the options for
* the call
* @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
*/
public ServiceCall<TrainingQuery> getTrainingQuery(GetTrainingQueryOptions getTrainingQueryOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getTrainingQueryOptions, "getTrainingQueryOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", getTrainingQueryOptions.projectId());
pathParamsMap.put("query_id", getTrainingQueryOptions.queryId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/training_data/queries/{query_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "getTrainingQuery");
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.Discovery in project java-sdk by watson-developer-cloud.
the class Discovery method listCollections.
/**
* List collections.
*
* <p>Lists existing collections for the specified project.
*
* @param listCollectionsOptions the {@link ListCollectionsOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link ListCollectionsResponse}
*/
public ServiceCall<ListCollectionsResponse> listCollections(ListCollectionsOptions listCollectionsOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(listCollectionsOptions, "listCollectionsOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", listCollectionsOptions.projectId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "listCollections");
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<ListCollectionsResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ListCollectionsResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v2.Discovery in project java-sdk by watson-developer-cloud.
the class Discovery method getCollection.
/**
* Get collection.
*
* <p>Get details about the specified collection.
*
* @param getCollectionOptions the {@link GetCollectionOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link CollectionDetails}
*/
public ServiceCall<CollectionDetails> getCollection(GetCollectionOptions getCollectionOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getCollectionOptions, "getCollectionOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", getCollectionOptions.projectId());
pathParamsMap.put("collection_id", getCollectionOptions.collectionId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections/{collection_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "getCollection");
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<CollectionDetails> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<CollectionDetails>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v2.Discovery in project java-sdk by watson-developer-cloud.
the class Discovery method queryNotices.
/**
* Query project notices.
*
* <p>Finds project-level notices (errors and warnings). Currently, project-level notices are
* generated by relevancy training.
*
* @param queryNoticesOptions the {@link QueryNoticesOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link QueryNoticesResponse}
*/
public ServiceCall<QueryNoticesResponse> queryNotices(QueryNoticesOptions queryNoticesOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(queryNoticesOptions, "queryNoticesOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", queryNoticesOptions.projectId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/notices", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "queryNotices");
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));
if (queryNoticesOptions.filter() != null) {
builder.query("filter", String.valueOf(queryNoticesOptions.filter()));
}
if (queryNoticesOptions.query() != null) {
builder.query("query", String.valueOf(queryNoticesOptions.query()));
}
if (queryNoticesOptions.naturalLanguageQuery() != null) {
builder.query("natural_language_query", String.valueOf(queryNoticesOptions.naturalLanguageQuery()));
}
if (queryNoticesOptions.count() != null) {
builder.query("count", String.valueOf(queryNoticesOptions.count()));
}
if (queryNoticesOptions.offset() != null) {
builder.query("offset", String.valueOf(queryNoticesOptions.offset()));
}
ResponseConverter<QueryNoticesResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<QueryNoticesResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations