0

我能够将元数据与文档一起存储在 Watson 的 Discovery 服务中,但现在它不再工作了!发生了什么?

CreateDocumentRequest.Builder createDocumentBuilder = new CreateDocumentRequest.Builder(environmentId, collectionId);
    JsonObject metadata = new JsonObject();
    metadata.addProperty("filename", fileName);
    createDocumentBuilder.metadata(metadata);
    createDocumentBuilder.inputStream(documentStream, applicationString);
    CreateDocumentResponse createDocumentResponse = discovery.createDocument(createDocumentBuilder.build()).execute();

https://gateway.watsonplatform.net/discovery/api/v1/environments/[environmentId]/collections/[collectionId]/query?version=2016-11-07&query=&count=&offset=&aggregation=&filter=&return=元数据

{
"matching_results": 1,
"results": [
    {
        "id": "7c705482-9902-4976-9c77-25fbe92bf7fa",
        "score": 1
    }
]

}

为什么它停止工作?我对这个突然发生的底层框架有点失望。一起来IBM!

4

1 回答 1

1

我修正了你的错误 IBM。

在 com.ibm.watson.developer_cloud.discovery.v1.Discovery.createDocument(CreateDocumentRequest createRequest) 中,您可以看到正在构建的 Multipart 请求,而从未添加所需的元数据部分 (smh)。

我扩展了 Discovery 并添加了

multipartBuilder.addFormDataPart("metadata", createRequest.getMetadata().toString());

对请求...

public ServiceCall<CreateDocumentResponse> createDocument(CreateDocumentRequest createRequest) {
    Validator.notEmpty(createRequest.getEnvironmentId(), "environment_id cannot be empty");
    Validator.notEmpty(createRequest.getCollectionId(), "collection_id cannot be empty");
    RequestBuilder builder = RequestBuilder.post(String.format("/v1/environments/%s/collections/%s/documents", new Object[] { createRequest.getEnvironmentId(), createRequest.getCollectionId() }));


    if (createRequest.getConfigurationId() != null) {
        builder.query(new Object[] { "configuration_id", createRequest.getConfigurationId() });
    }
    Validator.notNull(createRequest.getFile(), "Document file cannot be null");
    MediaType mediaType = (MediaType)supportedMediaTypes.get(createRequest.getMediaType());
    Validator.notNull(mediaType, String.format("Media Type '%s' not supported", new Object[] { createRequest.getMediaType() }));
    RequestBody file = InputStreamRequestBody.create(mediaType, createRequest.getFile());
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    multipartBuilder.addFormDataPart("file", "filename", file);
    multipartBuilder.addFormDataPart("metadata", createRequest.getMetadata().toString());
    builder.body(multipartBuilder.build());
    Request request = createVersionedRequest(builder);
    return createServiceCall(request, ResponseConverterUtils.getObject(CreateDocumentResponse.class));
}
于 2017-03-28T18:03:43.010 回答