Search in sources :

Example 16 with QueryResponse

use of com.ibm.watson.discovery.v2.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 17 with QueryResponse

use of com.ibm.watson.discovery.v2.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 18 with QueryResponse

use of com.ibm.watson.discovery.v2.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)

Example 19 with QueryResponse

use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method federatedQueryIsSuccessful.

@Test
public void federatedQueryIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(queryResp));
    FederatedQueryOptions.Builder builder = new FederatedQueryOptions.Builder(environmentId, new ArrayList<>(Arrays.asList(collectionId)));
    QueryResponse response = discoveryService.federatedQuery(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(Q3_PATH, request.getPath());
    assertEquals(GET, request.getMethod());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) FederatedQueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.FederatedQueryOptions) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 20 with QueryResponse

use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method queryWithAggregationTermIsSuccessful.

@Test
public void queryWithAggregationTermIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(queryResp));
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    StringBuilder sb = new StringBuilder();
    sb.append(AggregationType.TERM);
    sb.append(Operator.OPENING_GROUPING);
    sb.append("field");
    sb.append(Operator.CLOSING_GROUPING);
    String aggregation = sb.toString();
    queryBuilder.aggregation(aggregation);
    QueryResponse response = discoveryService.query(queryBuilder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(Q2_PATH, 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) 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)

Aggregations

QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)23 Test (org.junit.Test)21 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)20 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 QueryResponse (com.ibm.watson.discovery.v1.model.QueryResponse)7 LogQueryResponse (com.ibm.watson.discovery.v1.model.LogQueryResponse)6 JsonObject (com.google.gson.JsonObject)5 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)5 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 Calculation (com.ibm.watson.developer_cloud.discovery.v1.model.Calculation)4 FederatedQueryOptions (com.ibm.watson.discovery.v1.model.FederatedQueryOptions)4 QueryOptions (com.ibm.watson.discovery.v1.model.QueryOptions)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)3 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 GetMetricsQueryOptions (com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions)3