Search in sources :

Example 26 with Collection

use of com.ibm.watson.discovery.v1.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method federatedQuery.

/**
 * Query multiple collections.
 *
 * <p>By using this method, you can construct long queries that search multiple collection. For
 * details, see the [Discovery
 * documentation](https://cloud.ibm.com/docs/discovery?topic=discovery-query-concepts#query-concepts).
 *
 * @param federatedQueryOptions the {@link FederatedQueryOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link QueryResponse}
 */
public ServiceCall<QueryResponse> federatedQuery(FederatedQueryOptions federatedQueryOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(federatedQueryOptions, "federatedQueryOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", federatedQueryOptions.environmentId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/query", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "federatedQuery");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (federatedQueryOptions.xWatsonLoggingOptOut() != null) {
        builder.header("X-Watson-Logging-Opt-Out", federatedQueryOptions.xWatsonLoggingOptOut());
    }
    builder.query("version", String.valueOf(this.version));
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("collection_ids", federatedQueryOptions.collectionIds());
    if (federatedQueryOptions.filter() != null) {
        contentJson.addProperty("filter", federatedQueryOptions.filter());
    }
    if (federatedQueryOptions.query() != null) {
        contentJson.addProperty("query", federatedQueryOptions.query());
    }
    if (federatedQueryOptions.naturalLanguageQuery() != null) {
        contentJson.addProperty("natural_language_query", federatedQueryOptions.naturalLanguageQuery());
    }
    if (federatedQueryOptions.passages() != null) {
        contentJson.addProperty("passages", federatedQueryOptions.passages());
    }
    if (federatedQueryOptions.aggregation() != null) {
        contentJson.addProperty("aggregation", federatedQueryOptions.aggregation());
    }
    if (federatedQueryOptions.count() != null) {
        contentJson.addProperty("count", federatedQueryOptions.count());
    }
    if (federatedQueryOptions.xReturn() != null) {
        contentJson.addProperty("return", federatedQueryOptions.xReturn());
    }
    if (federatedQueryOptions.offset() != null) {
        contentJson.addProperty("offset", federatedQueryOptions.offset());
    }
    if (federatedQueryOptions.sort() != null) {
        contentJson.addProperty("sort", federatedQueryOptions.sort());
    }
    if (federatedQueryOptions.highlight() != null) {
        contentJson.addProperty("highlight", federatedQueryOptions.highlight());
    }
    if (federatedQueryOptions.passagesFields() != null) {
        contentJson.addProperty("passages.fields", federatedQueryOptions.passagesFields());
    }
    if (federatedQueryOptions.passagesCount() != null) {
        contentJson.addProperty("passages.count", federatedQueryOptions.passagesCount());
    }
    if (federatedQueryOptions.passagesCharacters() != null) {
        contentJson.addProperty("passages.characters", federatedQueryOptions.passagesCharacters());
    }
    if (federatedQueryOptions.deduplicate() != null) {
        contentJson.addProperty("deduplicate", federatedQueryOptions.deduplicate());
    }
    if (federatedQueryOptions.deduplicateField() != null) {
        contentJson.addProperty("deduplicate.field", federatedQueryOptions.deduplicateField());
    }
    if (federatedQueryOptions.similar() != null) {
        contentJson.addProperty("similar", federatedQueryOptions.similar());
    }
    if (federatedQueryOptions.similarDocumentIds() != null) {
        contentJson.addProperty("similar.document_ids", federatedQueryOptions.similarDocumentIds());
    }
    if (federatedQueryOptions.similarFields() != null) {
        contentJson.addProperty("similar.fields", federatedQueryOptions.similarFields());
    }
    if (federatedQueryOptions.bias() != null) {
        contentJson.addProperty("bias", federatedQueryOptions.bias());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<QueryResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<QueryResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) LogQueryResponse(com.ibm.watson.discovery.v1.model.LogQueryResponse) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) JsonObject(com.google.gson.JsonObject)

Example 27 with Collection

use of com.ibm.watson.discovery.v1.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method createCollection.

/**
 * Create a collection.
 *
 * @param createCollectionOptions the {@link CreateCollectionOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Collection}
 */
public ServiceCall<Collection> createCollection(CreateCollectionOptions createCollectionOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createCollectionOptions, "createCollectionOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", createCollectionOptions.environmentId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "createCollection");
    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("name", createCollectionOptions.name());
    if (createCollectionOptions.description() != null) {
        contentJson.addProperty("description", createCollectionOptions.description());
    }
    if (createCollectionOptions.configurationId() != null) {
        contentJson.addProperty("configuration_id", createCollectionOptions.configurationId());
    }
    if (createCollectionOptions.language() != null) {
        contentJson.addProperty("language", createCollectionOptions.language());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Collection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Collection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Collection(com.ibm.watson.discovery.v1.model.Collection)

Example 28 with Collection

use of com.ibm.watson.discovery.v1.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method federatedQueryNotices.

/**
 * Query multiple collection system notices.
 *
 * <p>Queries for notices (errors or warnings) that might have been generated by the system.
 * Notices are generated when ingesting documents and performing relevance training. See the
 * [Discovery
 * documentation](https://cloud.ibm.com/docs/discovery?topic=discovery-query-concepts#query-concepts)
 * for more details on the query language.
 *
 * @param federatedQueryNoticesOptions the {@link FederatedQueryNoticesOptions} containing the
 *     options for the call
 * @return a {@link ServiceCall} with a result of type {@link QueryNoticesResponse}
 */
public ServiceCall<QueryNoticesResponse> federatedQueryNotices(FederatedQueryNoticesOptions federatedQueryNoticesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(federatedQueryNoticesOptions, "federatedQueryNoticesOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", federatedQueryNoticesOptions.environmentId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/notices", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "federatedQueryNotices");
    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));
    builder.query("collection_ids", RequestUtils.join(federatedQueryNoticesOptions.collectionIds(), ","));
    if (federatedQueryNoticesOptions.filter() != null) {
        builder.query("filter", String.valueOf(federatedQueryNoticesOptions.filter()));
    }
    if (federatedQueryNoticesOptions.query() != null) {
        builder.query("query", String.valueOf(federatedQueryNoticesOptions.query()));
    }
    if (federatedQueryNoticesOptions.naturalLanguageQuery() != null) {
        builder.query("natural_language_query", String.valueOf(federatedQueryNoticesOptions.naturalLanguageQuery()));
    }
    if (federatedQueryNoticesOptions.aggregation() != null) {
        builder.query("aggregation", String.valueOf(federatedQueryNoticesOptions.aggregation()));
    }
    if (federatedQueryNoticesOptions.count() != null) {
        builder.query("count", String.valueOf(federatedQueryNoticesOptions.count()));
    }
    if (federatedQueryNoticesOptions.xReturn() != null) {
        builder.query("return", RequestUtils.join(federatedQueryNoticesOptions.xReturn(), ","));
    }
    if (federatedQueryNoticesOptions.offset() != null) {
        builder.query("offset", String.valueOf(federatedQueryNoticesOptions.offset()));
    }
    if (federatedQueryNoticesOptions.sort() != null) {
        builder.query("sort", RequestUtils.join(federatedQueryNoticesOptions.sort(), ","));
    }
    if (federatedQueryNoticesOptions.highlight() != null) {
        builder.query("highlight", String.valueOf(federatedQueryNoticesOptions.highlight()));
    }
    if (federatedQueryNoticesOptions.deduplicateField() != null) {
        builder.query("deduplicate.field", String.valueOf(federatedQueryNoticesOptions.deduplicateField()));
    }
    if (federatedQueryNoticesOptions.similar() != null) {
        builder.query("similar", String.valueOf(federatedQueryNoticesOptions.similar()));
    }
    if (federatedQueryNoticesOptions.similarDocumentIds() != null) {
        builder.query("similar.document_ids", RequestUtils.join(federatedQueryNoticesOptions.similarDocumentIds(), ","));
    }
    if (federatedQueryNoticesOptions.similarFields() != null) {
        builder.query("similar.fields", RequestUtils.join(federatedQueryNoticesOptions.similarFields(), ","));
    }
    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.v1.model.QueryNoticesResponse)

Example 29 with Collection

use of com.ibm.watson.discovery.v1.model.Collection in project java-sdk by watson-developer-cloud.

the class Discovery method addDocument.

/**
 * Add a document.
 *
 * <p>Add a document to a collection with optional metadata.
 *
 * <p>* The **version** query parameter is still required.
 *
 * <p>* Returns immediately after the system has accepted the document for processing.
 *
 * <p>* The user must provide document content, metadata, or both. If the request is missing both
 * document content and metadata, it is rejected.
 *
 * <p>* The user can set the **Content-Type** parameter on the **file** part to indicate the media
 * type of the document. If the **Content-Type** parameter is missing or is one of the generic
 * media types (for example, `application/octet-stream`), then the service attempts to
 * automatically detect the document's media type.
 *
 * <p>* The following field names are reserved and will be filtered out if present after
 * normalization: `id`, `score`, `highlight`, and any field with the prefix of: `_`, `+`, or `-`
 *
 * <p>* Fields with empty name values after normalization are filtered out before indexing.
 *
 * <p>* Fields containing the following characters after normalization are filtered out before
 * indexing: `#` and `,`
 *
 * <p>**Note:** Documents can be added with a specific **document_id** by using the
 * **_/v1/environments/{environment_id}/collections/{collection_id}/documents** method.
 *
 * @param addDocumentOptions the {@link AddDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentAccepted}
 */
public ServiceCall<DocumentAccepted> addDocument(AddDocumentOptions addDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(addDocumentOptions, "addDocumentOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((addDocumentOptions.file() != null) || (addDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", addDocumentOptions.environmentId());
    pathParamsMap.put("collection_id", addDocumentOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/documents", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "addDocument");
    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));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (addDocumentOptions.file() != null) {
        okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(addDocumentOptions.file(), addDocumentOptions.fileContentType());
        multipartBuilder.addFormDataPart("file", addDocumentOptions.filename(), fileBody);
    }
    if (addDocumentOptions.metadata() != null) {
        multipartBuilder.addFormDataPart("metadata", addDocumentOptions.metadata());
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<DocumentAccepted> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentAccepted>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody)

Example 30 with Collection

use of com.ibm.watson.discovery.v1.model.Collection in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method createCollectionIsSuccessful.

@Test
public void createCollectionIsSuccessful() {
    Configuration createConfigResponse = createTestConfig();
    String uniqueCollectionName = uniqueName + "-collection";
    String uniqueCollectionDescription = "Description of " + uniqueCollectionName;
    CreateCollectionOptions.Builder createCollectionBuilder = new CreateCollectionOptions.Builder(environmentId, uniqueCollectionName).configurationId(createConfigResponse.getConfigurationId()).description(uniqueCollectionDescription);
    Collection createResponse = createCollection(createCollectionBuilder.build());
    assertEquals(createConfigResponse.getConfigurationId(), createResponse.getConfigurationId());
    assertEquals(uniqueCollectionName, createResponse.getName());
    assertEquals(uniqueCollectionDescription, createResponse.getDescription());
}
Also used : Configuration(com.ibm.watson.developer_cloud.discovery.v1.model.Configuration) CreateCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)26 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)23 HashMap (java.util.HashMap)22 Test (org.junit.Test)22 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)18 JsonObject (com.google.gson.JsonObject)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)14 Collection (com.ibm.watson.visual_recognition.v4.model.Collection)11 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)10 CreateCollectionOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions)9 Collection (com.ibm.watson.discovery.v1.model.Collection)9 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 Test (org.testng.annotations.Test)7 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)5 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)5 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)5