Hi, there.

I have a problem with nested documents aggregation. The problem is next. 
Imagine that we have index full of next simple documents

{name: "aaa",
 count: 3,
 companyEmotions: [{
     emotion: 1,
     company: "foo"
 }, {
     emotion: 2,
     company: "bar"
 }]
}

{name: "bbb",
 count: 10,
 companyEmotions: [{
     emotion: 2,
     company: "foo"
 }, {
     emotion: 2,
     company: "bar"
 }]
}



and we want to receive sum aggregation of "count" field grouped by emotion 
for some company. Speaking by our example with two documents we want to get 
answer for the question: "what is the sum of "count" for documents where 
foo company emotion is 1, is 2.
Result buckets should be something like that:

          "buckets" : [ {
            "key" : 1,                  // This is our emotion
            "doc_count" : 1,
            "someAggrName" : {
              "value" : 3.0             // sum of all counts.
            }
          }, {
            "key" : 2,
            "doc_count" : 1,
            "emotionAggr4" : {
              "value" : 10.0
            }
          } ]

And here is the mapping which I'm using.

{
    "emotion" : {
        "properties" : {
            "name" : {"type" : "string", "index" : "not_analyzed" },
            "count" : {"type" : "integer" },
            "companyEmotions" : {
                "type" : "nested",
                "_parent": {
                    "type": "emotion"
                },
                "properties": {
                    "company" : {"type" : "string", "index" : 
"not_analyzed" },
                    "emotion" : { "type" : "integer" }
                }
            }
        }
    }
}

I have experimented a lot with next aggregation request, and can't find the 
way how to refer to the "count" field. I have tried _parent.count, 
_doc["_parent.count"].value, etc. Most of the time, if some error is not 
happen, sum is 0. Ho to correctly refer to the parent "count" field in 
script?
Or maybe problem in the mapping or something?

curl -XPOST "http://localhost:9200/emotions/emotion/_search?pretty=true"; -d 
'{
    "aggregations": {
        "emotionAggr": {
            "nested": {
                "path": "companyEmotions"
            },
            "aggregations": {
                "emotionAggr2": {
                    "filter": {
                        "term": {"companyEmotions.company": "foo"}
                    },
                    "aggregations": {
                        "emotionAggr3": {
                            "terms": {
                                "field": "companyEmotions.emotion"
                            },
                            "aggregations": {
                                "emotionAggr4": {
                                    "sum": {"script": 
"doc[\"count\"].value"} // How to reffer _parent.count here?
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}'

Thanks for helping.

P. S.
If you want to experiment here is the list of commands which you can 
execute to create an index full of documents.
https://gist.github.com/vchimishuk/2c324e2be71c23b7f4c7


-- 
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/2da1f10a-235b-4866-8162-e1bf9846e6c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to