Search in sources :

Example 1 with QueryAggregation

use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation in project java-sdk by watson-developer-cloud.

the class AggregationDeserializer method deserialize.

/**
 * Deserializes JSON and converts it to the appropriate {@link QueryAggregation} subclass.
 *
 * @param json the JSON data being deserialized
 * @param typeOfT the type to deserialize to, which should be {@link QueryAggregation}
 * @param context additional information about the deserialization state
 * @return the appropriate {@link QueryAggregation} subclass
 * @throws JsonParseException signals that there has been an issue parsing the JSON
 */
@Override
public QueryAggregation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonReader in = new JsonReader(new StringReader(GsonSingleton.getGson().toJson(json)));
    HashMap<String, Object> aggregationMap = null;
    try {
        aggregationMap = getAggregationMap(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    QueryAggregation aggregation;
    String aggregationType = (String) aggregationMap.get(TYPE);
    if (aggregationType.equals(AggregationType.HISTOGRAM.getName())) {
        aggregation = GsonSerializationHelper.serializeDynamicModelProperty(aggregationMap, Histogram.class);
    } else if (aggregationType.equals(AggregationType.MAX.getName()) || aggregationType.equals(AggregationType.MIN.getName()) || aggregationType.equals(AggregationType.AVERAGE.getName()) || aggregationType.equals(AggregationType.SUM.getName())) {
        aggregation = GsonSerializationHelper.serializeDynamicModelProperty(aggregationMap, Calculation.class);
    } else if (aggregationType.equals(AggregationType.TERM.getName())) {
        aggregation = GsonSerializationHelper.serializeDynamicModelProperty(aggregationMap, Term.class);
    } else {
        aggregation = GsonSerializationHelper.serializeDynamicModelProperty(aggregationMap, QueryAggregation.class);
    }
    return aggregation;
}
Also used : Histogram(com.ibm.watson.developer_cloud.discovery.v1.model.Histogram) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) QueryAggregation(com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation) Term(com.ibm.watson.developer_cloud.discovery.v1.model.Term)

Example 2 with QueryAggregation

use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method queryWithNestedAggregationTermIsSuccessful.

@Test
public void queryWithNestedAggregationTermIsSuccessful() {
    Collection collection = createTestCollection();
    String collectionId = collection.getCollectionId();
    createTestDocument("test_document_1", collectionId);
    createTestDocument("test_document_2", collectionId);
    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);
    sb.append(Operator.NEST_AGGREGATION);
    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 queryResponse = discovery.query(queryBuilder.build()).execute();
    Term term = (Term) queryResponse.getAggregations().get(0);
    AggregationResult agResults = term.getResults().get(0);
    List<QueryAggregation> aggregations = agResults.getAggregations();
    assertFalse(aggregations.isEmpty());
}
Also used : AggregationResult(com.ibm.watson.developer_cloud.discovery.v1.model.AggregationResult) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Term(com.ibm.watson.developer_cloud.discovery.v1.model.Term) QueryAggregation(com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

QueryAggregation (com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation)2 Term (com.ibm.watson.developer_cloud.discovery.v1.model.Term)2 JsonReader (com.google.gson.stream.JsonReader)1 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 AggregationResult (com.ibm.watson.developer_cloud.discovery.v1.model.AggregationResult)1 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)1 Histogram (com.ibm.watson.developer_cloud.discovery.v1.model.Histogram)1 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)1 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Test (org.junit.Test)1