use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method queryIsSuccessful.
// Query tests
@Test
public void queryIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(queryResp));
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.count(5L);
queryBuilder.offset(5L);
List<String> fieldNames = new ArrayList<>();
fieldNames.add("field");
queryBuilder.returnFields(fieldNames);
queryBuilder.query("field" + Operator.CONTAINS + 1);
queryBuilder.filter("field" + Operator.CONTAINS + 1);
queryBuilder.similar(true);
queryBuilder.similarDocumentIds(Arrays.asList("doc1", "doc2"));
queryBuilder.similarFields(Arrays.asList("field1", "field2"));
QueryResponse response = discoveryService.query(queryBuilder.build()).execute();
RecordedRequest request = server.takeRequest();
assertEquals(Q1_PATH + "&similar=true&similar.document_ids=doc1,doc2&similar.fields=field1,field2", request.getPath());
assertEquals(GET, request.getMethod());
assertEquals(GsonSingleton.getGson().toJsonTree(queryResp), GsonSingleton.getGson().toJsonTree(response));
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse 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.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithQueryIsSuccessful.
@Ignore
@Test
public void queryWithQueryIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.query("field" + Operator.CONTAINS + 1);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
assertEquals(new Long(1), queryResponse.getMatchingResults());
assertEquals(1, queryResponse.getResults().size());
assertTrue(queryResponse.getResults().get(0).getScore() > 1.0);
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithSortIsSuccessful.
@Test
public void queryWithSortIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
ArrayList<String> sortList = new ArrayList<>();
sortList.add("field");
queryBuilder.sort(sortList);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
assertTrue(queryResponse.getResults().size() > 1);
Double v0 = (Double) (queryResponse.getResults().get(0)).get("field");
Double v1 = (Double) (queryResponse.getResults().get(1)).get("field");
assertTrue(v0 <= v1);
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithAggregationHistogramIsSuccessful.
@Test
public void queryWithAggregationHistogramIsSuccessful() throws InterruptedException {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
StringBuilder sb = new StringBuilder();
sb.append(AggregationType.HISTOGRAM);
sb.append(Operator.OPENING_GROUPING);
sb.append("field");
sb.append(Operator.AND);
sb.append(5L);
sb.append(Operator.CLOSING_GROUPING);
String aggregation = sb.toString();
queryBuilder.aggregation(aggregation);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
Histogram histogram = (Histogram) queryResponse.getAggregations().get(0);
Long interval = histogram.getInterval();
assertEquals(new Long(5), interval);
assertEquals(2, histogram.getResults().size());
}
Aggregations