George Eracleous
George Eracleous

Reputation: 4412

Weird value returned for sum aggregation in Elasticsearch Javascript client

I'm trying to perform a sum aggregation on a numeric field (with type double) using the Javascript client for Elasticsearch.

Here's my code:

    this.client.search({
        index: "customers",
        body: {
            aggs: {
                counts_in_range: {
                    filter: {
                        range: {
                            timestamp : {
                                gte : startDate,
                                lt : endDate
                            }
                        }
                    },
                    aggs: {
                        counts: {
                            sum : {
                                field : "price"
                            }
                        }
                    }
                }
            }
        }
    }).then(function (resp) {
        cb(resp.aggregations, null);
    }, function (err) {
        cb(null, err);
    });

Example document:

{
    _index: "customers",
    _type: "purchase",
    _id: "98cb1066-057b-48e1-adff-eb32d9ed75a5",
    _score: 1,
    _source: {
        timestamp: "2014-06-11T18:14:36+03:00",
        itemId: 1,
        price: 0.54
    }
}

What I get back from the aggregation is a very long number e.g. 27549779928520990000 instead of a decimal number. The problem seems to be that in my document I store decimal numbers and not integers. If I store an integer in the price field the aggregation works just fine.

Not sure if this is a parsing issue with the Javascript client.

Upvotes: 0

Views: 1710

Answers (1)

Iirina Iancu
Iirina Iancu

Reputation: 36

When you're first indexing a document, the type for each field is decided by elasticsearch if none is specified. In your case, elasticsearch thinks you're storing integers in your document, even if you will later store decimal numbers. So when it's computing the sum, it will try to work only with integers, but it came across to decimal numbers, hence the long number returned. To avoid that from happening, map each field to a core value when creating a new type of document.

Upvotes: 2

Related Questions