use of com.ibm.watson.discovery.v1.model.FederatedQueryOptions 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);
}
use of com.ibm.watson.discovery.v1.model.FederatedQueryOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testFederatedQueryWOptions.
// Test the federatedQuery operation with a valid options model parameter
@Test
public void testFederatedQueryWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"matching_results\": 15, \"results\": [{\"id\": \"id\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"collection_id\": \"collectionId\", \"result_metadata\": {\"score\": 5, \"confidence\": 10}}], \"aggregations\": [{\"type\": \"filter\", \"match\": \"match\", \"matching_results\": 15}], \"passages\": [{\"document_id\": \"documentId\", \"passage_score\": 12, \"passage_text\": \"passageText\", \"start_offset\": 11, \"end_offset\": 9, \"field\": \"field\"}], \"duplicates_removed\": 17, \"session_token\": \"sessionToken\", \"retrieval_details\": {\"document_retrieval_strategy\": \"untrained\"}, \"suggested_query\": \"suggestedQuery\"}";
String federatedQueryPath = "/v1/environments/testString/query";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the FederatedQueryOptions model
FederatedQueryOptions federatedQueryOptionsModel = new FederatedQueryOptions.Builder().environmentId("testString").collectionIds("testString").filter("testString").query("testString").naturalLanguageQuery("testString").passages(true).aggregation("testString").count(Long.valueOf("26")).xReturn("testString").offset(Long.valueOf("26")).sort("testString").highlight(false).passagesFields("testString").passagesCount(Long.valueOf("100")).passagesCharacters(Long.valueOf("50")).deduplicate(false).deduplicateField("testString").similar(false).similarDocumentIds("testString").similarFields("testString").bias("testString").xWatsonLoggingOptOut(false).build();
// Invoke federatedQuery() with a valid options model and verify the result
Response<QueryResponse> response = discoveryService.federatedQuery(federatedQueryOptionsModel).execute();
assertNotNull(response);
QueryResponse responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, federatedQueryPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
Aggregations