# Stats aggregation using two or more fields

**URL:** https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408
**Category:** Elasticsearch
**Created:** [April 24, 2017, 11:49am UTC](https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408 "2017-04-24T11:49:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mukolajko25](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mukolajko25/32/17191_2.png) [@Mukolajko25](https://discuss.elastic.co/u/Mukolajko25)
#### Post date: [April 24, 2017, 11:49am UTC](https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408/1 "2017-04-24T11:49:19Z")

</div>

Hi.

I have aggregations like this

```
"aggs":{
	"fandex_lists":{"terms":{"field":"lists","size":5},
		"aggs":{
			"fandex_overall":{
				"stats":{"field": "scores.fandex.overall.value" }
			}
		}
	}
}

"aggs":{
	"fandex_lists":{"terms":{"field":"lists","size":5},
		"aggs":{
			"revenue_overall":{
				"stats":{"field": "revenue.overall.total" }
			}
		}
	}
}

```

As you can see those two aggregations uses same terms field to match documents, just have different stats field to collect results.

I have a question about stats aggregation.

Can i collect stats for both fields in same query like so

```
   "aggs":{
    	"fandex_lists":{"terms":{"field":"lists","size":5},
    		"aggs":{
    			"overall":{
    				"stats":{"field": "scores.fandex.overall.value" },
                    "stats":{"field": "revenue.overall.total" }
    			}
    		}
    	}
    }

```

Or something similar. I read documentation and try to google but cant find solution. Maybe im missing something obvious.

Thank you.

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [April 24, 2017, 4:34pm UTC](https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408/2 "2017-04-24T16:34:08Z")

</div>

Yes, you can. The following should get you what you want:

```auto
   "aggs":{
    	"fandex_lists":{"terms":{"field":"lists","size":5},
    		"aggs":{
    			"overall":{
    				"stats":{"field": "scores.fandex.overall.value" }
    			},
                "fandex": {
                    "stats":{"field": "revenue.overall.total" }
                }
    		}
    	}
    }

```

Hope that helps

---

<div class="post-metadata">

### Author: ![Mukolajko25](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mukolajko25/32/17191_2.png) [@Mukolajko25](https://discuss.elastic.co/u/Mukolajko25)
#### Post date: [April 24, 2017, 5:20pm UTC](https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408/3 "2017-04-24T17:20:52Z")

</div>

Damn... I tried like that :D. Just don't check results correctly.

Thank you very much.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 22, 2017, 5:26pm UTC](https://discuss.elastic.co/t/stats-aggregation-using-two-or-more-fields/83408/4 "2017-05-22T17:26:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
