Oh, I'm sorry, I completely missed your question, I thought you wanted to
merge the counts for different fields.

Would this aggregation do what you are looking for?
{
    "terms": {
        "field": "fieldA"
    },
    "aggs": {
        "size_sum": {
            "sum": {
                "field": "size"
            }
        }
    }
}

See
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.htmlfor
the documentation of the sum aggregation. I hope I got your question
right this time!




On Fri, May 2, 2014 at 4:08 PM, Jose A. Garcia <[email protected]> wrote:

> Hi Adrien,
>
> Thanks for your answer, but I have a question. Wouldn't that give me the
> different sums of the values of those fields?
>
> What I need is, using the example from before:
>
> Doc1 : {  field1 : A,  field2: B, field3: C, size: 1, }
> Doc2: { field1 : A,  field2: B2,  field3: C2, size: 2}
> Doc3: { field1 : Z, field2: B3, field3: C3, size: 99 }
>
> If I search in my index and those three documents match my query I want a
> list of the possible values that 'field1' can take and the sum of the
> 'size' fields for all documents with each value in my result set. So in
> this case I would expect:
>
> field1: {
>  {value: 'A', sum_of_sizes: 3}
>  {value: 'Z', sum_of_sizes: 99}
> }
>
> Thanks,
> Jose.
>
> On Friday, 2 May 2014 14:51:36 UTC+1, Adrien Grand wrote:
>
>> Hi Jose,
>>
>> There are two ways to do so: either with a script (slow because term
>> ordinals can't be used):
>>
>> "terms" : {
>>     "script": "doc['A'].values + doc['B'].values + doc['C'].values"
>> }
>>
>> Or by having all values in a single field at indexing time (potentially
>> using `copy_to`[1]).
>>
>> [1] http://www.elasticsearch.org/guide/en/elasticsearch/
>> reference/current/mapping-core-types.html#copy-to
>>
>>
>>
>> On Fri, May 2, 2014 at 11:44 AM, Jose A. Garcia <[email protected]>wrote:
>>
>>> Hi,
>>>
>>> I have a question about Aggregations. I have documents with several
>>> fields:
>>>
>>> {
>>>   field1 : A,
>>>   field2: B,
>>>   field3: C,
>>>   size: 1,
>>> }
>>>
>>> {
>>>   field1 : A,
>>>   field2: B2,
>>>   field3: C2,
>>>   size: 2,
>>> }
>>>
>>> {
>>>   field1 : Z,
>>>   field2: B3,
>>>   field3: C3,
>>>   size: 99,
>>> }
>>>
>>> And I need to be able to calculate aggregations for each one of those
>>> fields, and get the sum of the sizes for each field, so for example,
>>> aggregating by field1 should get me { A, size = 3 }, {Z, size = 99}.
>>>
>>> Looking at the documentation for aggregations I can see how to get the
>>> sum for a field and how to get the terms and their counts, but I need a
>>> combination of both, what is the best way to do this?
>>>
>>> Thanks in advance,
>>> Jose.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "elasticsearch" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/elasticsearch/1deb1eb1-a5dc-40cb-8689-c5518869f40a%
>>> 40googlegroups.com<https://groups.google.com/d/msgid/elasticsearch/1deb1eb1-a5dc-40cb-8689-c5518869f40a%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Adrien Grand
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "elasticsearch" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elasticsearch/ec381c10-45cd-4406-8aac-2e542097cf49%40googlegroups.com<https://groups.google.com/d/msgid/elasticsearch/ec381c10-45cd-4406-8aac-2e542097cf49%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Adrien Grand

-- 
You received this message because you are subscribed to the Google Groups 
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j44%2Ba_JXWzWLWwNkHxy0%3DaGiyaWmXgy-JmxPY3h2nmQ%2Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to