Search in sources :

Example 1 with Discovery

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();
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QueryOptions(com.ibm.watson.discovery.v2.model.QueryOptions) FileInputStream(java.io.FileInputStream) QueryResult(com.ibm.watson.discovery.v2.model.QueryResult) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v2.model.QueryResponse) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) DeleteDocumentOptions(com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)

Example 2 with Discovery

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery)

Example 3 with Discovery

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) ListCollectionsResponse(com.ibm.watson.discovery.v2.model.ListCollectionsResponse)

Example 4 with Discovery

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) CollectionDetails(com.ibm.watson.discovery.v2.model.CollectionDetails)

Example 5 with Discovery

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) QueryNoticesResponse(com.ibm.watson.discovery.v2.model.QueryNoticesResponse)

Aggregations

RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)26 HashMap (java.util.HashMap)24 JsonObject (com.google.gson.JsonObject)9 DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)4 MultipartBody (okhttp3.MultipartBody)4 CollectionDetails (com.ibm.watson.discovery.v2.model.CollectionDetails)3 Enrichment (com.ibm.watson.discovery.v2.model.Enrichment)3 ProjectDetails (com.ibm.watson.discovery.v2.model.ProjectDetails)3 TrainingQuery (com.ibm.watson.discovery.v2.model.TrainingQuery)3 AddDocumentOptions (com.ibm.watson.discovery.v2.model.AddDocumentOptions)2 QueryNoticesResponse (com.ibm.watson.discovery.v2.model.QueryNoticesResponse)2 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)2 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1 BearerTokenAuthenticator (com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator)1 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)1 Discovery (com.ibm.watson.discovery.v2.Discovery)1 AnalyzedDocument (com.ibm.watson.discovery.v2.model.AnalyzedDocument)1 Completions (com.ibm.watson.discovery.v2.model.Completions)1 ComponentSettingsResponse (com.ibm.watson.discovery.v2.model.ComponentSettingsResponse)1 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)1