Search in sources :

Example 11 with QueryResponse

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));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) ArrayList(java.util.ArrayList) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) FederatedQueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.FederatedQueryOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 12 with QueryResponse

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));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)

Example 13 with QueryResponse

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);
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Ignore(org.junit.Ignore) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 14 with QueryResponse

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);
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) ArrayList(java.util.ArrayList) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 15 with QueryResponse

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());
}
Also used : Histogram(com.ibm.watson.developer_cloud.discovery.v1.model.Histogram) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)23 Test (org.junit.Test)23 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)20 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)20 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)5 Calculation (com.ibm.watson.developer_cloud.discovery.v1.model.Calculation)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)3 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)3 FederatedQueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.FederatedQueryOptions)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Ignore (org.junit.Ignore)3 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)2 Term (com.ibm.watson.developer_cloud.discovery.v1.model.Term)2 ArrayList (java.util.ArrayList)2 AggregationResult (com.ibm.watson.developer_cloud.discovery.v1.model.AggregationResult)1