use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method addDocumentWithMetadataIsSuccessful.
@Ignore
@SuppressWarnings("deprecation")
@Test
public void addDocumentWithMetadataIsSuccessful() {
Collection collection = createTestCollection();
String collectionId = collection.getCollectionId();
String myDocumentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
JsonObject myMetadata = new JsonObject();
myMetadata.add("foo", new JsonPrimitive("bar"));
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
builder.filename("test_file");
builder.metadata(myMetadata.toString());
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute();
WaitFor.Condition documentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, createResponse.getDocumentId());
WaitFor.waitFor(documentAccepted, 5, TimeUnit.SECONDS, 500);
QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId).build();
QueryResponse queryResponse = discovery.query(queryOptions).execute();
assertTrue(queryResponse.getResults().get(0).getMetadata() != null);
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions in project java-sdk by watson-developer-cloud.
the class Discovery method query.
/**
* Query documents.
*
* See the [Discovery service documentation](https://console.bluemix.net/docs/services/discovery/using.html) for more
* details.
*
* @param queryOptions the {@link QueryOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link QueryResponse}
*/
public ServiceCall<QueryResponse> query(QueryOptions queryOptions) {
Validator.notNull(queryOptions, "queryOptions cannot be null");
String[] pathSegments = { "v1/environments", "collections", "query" };
String[] pathParameters = { queryOptions.environmentId(), queryOptions.collectionId() };
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
if (queryOptions.filter() != null) {
builder.query("filter", queryOptions.filter());
}
if (queryOptions.query() != null) {
builder.query("query", queryOptions.query());
}
if (queryOptions.naturalLanguageQuery() != null) {
builder.query("natural_language_query", queryOptions.naturalLanguageQuery());
}
if (queryOptions.passages() != null) {
builder.query("passages", String.valueOf(queryOptions.passages()));
}
if (queryOptions.aggregation() != null) {
builder.query("aggregation", queryOptions.aggregation());
}
if (queryOptions.count() != null) {
builder.query("count", String.valueOf(queryOptions.count()));
}
if (queryOptions.returnFields() != null) {
builder.query("return", RequestUtils.join(queryOptions.returnFields(), ","));
}
if (queryOptions.offset() != null) {
builder.query("offset", String.valueOf(queryOptions.offset()));
}
if (queryOptions.sort() != null) {
builder.query("sort", RequestUtils.join(queryOptions.sort(), ","));
}
if (queryOptions.highlight() != null) {
builder.query("highlight", String.valueOf(queryOptions.highlight()));
}
if (queryOptions.passagesFields() != null) {
builder.query("passages.fields", RequestUtils.join(queryOptions.passagesFields(), ","));
}
if (queryOptions.passagesCount() != null) {
builder.query("passages.count", String.valueOf(queryOptions.passagesCount()));
}
if (queryOptions.passagesCharacters() != null) {
builder.query("passages.characters", String.valueOf(queryOptions.passagesCharacters()));
}
if (queryOptions.deduplicate() != null) {
builder.query("deduplicate", String.valueOf(queryOptions.deduplicate()));
}
if (queryOptions.deduplicateField() != null) {
builder.query("deduplicate.field", queryOptions.deduplicateField());
}
if (queryOptions.similar() != null) {
builder.query("similar", String.valueOf(queryOptions.similar()));
}
if (queryOptions.similarDocumentIds() != null) {
builder.query("similar.document_ids", RequestUtils.join(queryOptions.similarDocumentIds(), ","));
}
if (queryOptions.similarFields() != null) {
builder.query("similar.fields", RequestUtils.join(queryOptions.similarFields(), ","));
}
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(QueryResponse.class));
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method updateDocumentWithMetadataIsSuccessful.
@Test
@Ignore("Pending implementation of 'processing' after document update")
public void updateDocumentWithMetadataIsSuccessful() {
Collection collection = createTestCollection();
String collectionId = collection.getCollectionId();
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
String myDocumentJson = "{\"field\":\"value2\"}";
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
JsonObject myMetadata = new JsonObject();
myMetadata.add("foo", new JsonPrimitive("bar"));
UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentAccepted.getDocumentId());
updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
updateBuilder.metadata(myMetadata.toString());
DocumentAccepted updateResponse = discovery.updateDocument(updateBuilder.build()).execute();
WaitFor.Condition waitForDocumentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, updateResponse.getDocumentId());
WaitFor.waitFor(waitForDocumentAccepted, 5, TimeUnit.SECONDS, 500);
QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId).build();
QueryResponse queryResponse = discovery.query(queryOptions).execute();
assertTrue(queryResponse.getResults().get(0).getMetadata() != null);
}
Aggregations