Search in sources :

Example 1 with BearerTokenAuthenticator

use of com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator 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 BearerTokenAuthenticator

use of com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method testGetAutocompletion.

/**
 * This only works on a Cloud Pak for Data instance, so ignoring to just run manually.
 */
@Test
@Ignore
public void testGetAutocompletion() {
    // fill in
    Authenticator authenticator = new BearerTokenAuthenticator("");
    Discovery service = new Discovery("2019-10-03", authenticator);
    service.setServiceUrl("");
    HttpConfigOptions configOptions = new HttpConfigOptions.Builder().disableSslVerification(true).build();
    service.configureClient(configOptions);
    GetAutocompletionOptions options = new GetAutocompletionOptions.Builder().environmentId(// fill in
    "").collectionId(// fill in
    "").prefix("Ba").count(10L).build();
    Completions response = service.getAutocompletion(options).execute().getResult();
// System.out.println(response);
}
Also used : HttpConfigOptions(com.ibm.cloud.sdk.core.http.HttpConfigOptions) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) BasicAuthenticator(com.ibm.cloud.sdk.core.security.BasicAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 3 with BearerTokenAuthenticator

use of com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method testQueryWithSpellingSuggestions.

/**
 * This only works on a Cloud Pak for Data instance, so ignoring to just run manually.
 */
@Test
@Ignore
public void testQueryWithSpellingSuggestions() {
    // fill in
    Authenticator authenticator = new BearerTokenAuthenticator("");
    Discovery service = new Discovery("2019-10-03", authenticator);
    service.setServiceUrl("");
    HttpConfigOptions configOptions = new HttpConfigOptions.Builder().disableSslVerification(true).build();
    service.configureClient(configOptions);
    QueryOptions options = new QueryOptions.Builder().naturalLanguageQuery("cluod").spellingSuggestions(true).environmentId(// fill in
    "").collectionId(// fill in
    "").build();
    QueryResponse response = service.query(options).execute().getResult();
// System.out.println(response);
}
Also used : HttpConfigOptions(com.ibm.cloud.sdk.core.http.HttpConfigOptions) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) BasicAuthenticator(com.ibm.cloud.sdk.core.security.BasicAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)3 BearerTokenAuthenticator (com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator)3 HttpConfigOptions (com.ibm.cloud.sdk.core.http.HttpConfigOptions)2 BasicAuthenticator (com.ibm.cloud.sdk.core.security.BasicAuthenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 AddDocumentOptions (com.ibm.watson.discovery.v2.model.AddDocumentOptions)1 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)1 DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)1 QueryOptions (com.ibm.watson.discovery.v2.model.QueryOptions)1 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)1 QueryResult (com.ibm.watson.discovery.v2.model.QueryResult)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1