Aggregation sum return long value as double


(CHI WAI HO) #1

I have tested the below query on ECHead plugin and results were returned perfectly. However, when I come to the Java API (Transport client), the result only give me the value in double format. May I ask how I can get the sum value in long type? The Java API only return value in the "value_as_string" as returned by the ECHead plugin.

Java API Result Example:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 50,
    "successful" : 50,
    "failed" : 0
  },
  "hits" : {
    "total" : 2678,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "stockIds" : {
      "doc_count_error_upper_bound" : 42,
      "sum_other_doc_count" : 2673,
      "buckets" : [ {
        "key" : "1",
        "doc_count" : 1,
        "tvrSum" : {
          "value" : 5.61380157E8
        }
      }, {
        "key" : "10",
        "doc_count" : 1,
        "tvrSum" : {
          "value" : 3.9533725E7
        }
      }, {
        "key" : "100",
        "doc_count" : 1,
        "tvrSum" : {
          "value" : 115290.0
        }
      }, {
        "key" : "1000",
        "doc_count" : 1,
        "tvrSum" : {
          "value" : 2.3194835E7
        }
      }, {
        "key" : "1001",
        "doc_count" : 1,
        "tvrSum" : {
          "value" : 4303780.0
        }
      } ]
    }
  }
}

ECHead Results Example:

 .....
"aggregations": {
    "stockIds": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 1764,
    "buckets": [
    {
    "key": "1",
    "doc_count": 1,
    "tvrSum": {
    "value": 917095962,
    "value_as_string": "9.17095962E8"
    }
    }
    ,
    {
    "key": "10",
    "doc_count": 1,
    "tvrSum": {
    "value": 66764987,
    "value_as_string": "6.6764987E7"
    }
    }
    ,
    {
    "key": "100",
    "doc_count": 1,
    "tvrSum": {
    "value": 1614200,
    "value_as_string": "1614200.0"
    }
    }

My Query:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "DailyData.trans_date": [
              "150714"
            ]
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "size": "0",
  "aggs": {
    "stockIds": {
      "terms": {
        "field": "stockId",
        "size": 5
      },
      "aggs": {
        "tvrSum": {
          "sum": {
            "field": "tvr"
          }
        }
      }
    }
  }
}

My Java Code:

SearchResponse response = SC.getTransportClient().prepareSearch(SC.getEsIndexName()).setTypes("DailyData")
														.setQuery(
															QueryBuilders.boolQuery()
															.must(
																QueryBuilders.termsQuery("DailyData.trans_date", DD.getNewestDaysHash().get(day))
																)
														)
														.setSize(0)
														.addAggregation(
															AggregationBuilders
															    .terms("stockIds")
															    .size(5)
																.field("stockId")
																.subAggregation(
																	AggregationBuilders
																	.sum("tvrSum")
																	.field("tvr")
																)
														)
														.execute().actionGet();




Terms terms = response.getAggregations().get("stockIds");
			Collection<Bucket> buckets = terms.getBuckets();

			for (Bucket bucket : buckets) {
				Aggregations aggs = bucket.getAggregations();

				InternalSum tvrs = aggs.get("tvrSum");

				M.messageln("Tvr for Stock <" + bucket.getKeyAsText() + "> in last " + day + " day(s) = getValue():" + tvrs.getValue() + ", value(): " + tvrs.value() + ", getValueAsString(): " + tvrs.getValueAsString() + ", toString(): ", !SC.isDebug() );
			}

Getting max value of a long field without losing precision
(Colin Goodheart-Smithe) #2

The Sum Aggregation (and most of the numeric metric aggregations) compute results as a double regardless of the field type.. To get a long value from this, you will need to do the conversion from double to long in your application.


(system) #3