# Lucen/Elasticsearch: a query for a fiield with multiple values

**URL:** <https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253>\
**Category:** Elasticsearch\
**Created:** [May 29, 2016, 6:25pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253 "2016-05-29T18:25:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [May 29, 2016, 6:25pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/1 "2016-05-29T18:25:05Z")

</div>

Hello everyone,

Lucen/Elasticsearch: a query for a filed with multiple valuesHere is what I would like to do. I have an index with several documents in Elasticsearch. In every document I have two field: deviceField (name of the device) and pressionField (the value of the pression periodically). I want to query in my index the average pression per device. Do you know a way to do it in a single query? Indeed, I do not want to do a kind of loop 'for' in order to the query per deviceName. It will take too much time due to the fact that I have millions of devices.

Thank you for your attention and your help.

S

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [May 29, 2016, 10:57pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/2 "2016-05-29T22:57:32Z")

</div>

Look at aggregations. They are built to do that sort of thing. They aren't  
a query but instead something to do with the hits.

---

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [May 30, 2016, 11:08am UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/3 "2016-05-30T11:08:37Z")

</div>

Hi Nik,

I used your advice and I wrote this code but I found an error:

```
  "type" : "search_parse_exception",
  "reason" : "Could not find aggregator type [avg_pression] in [aggs]",

```

{  
"aggs" : {  
"devices" : {  
"terms" : { "field" : "deviceField" }  
},  
"aggs" : {  
"avg\_pression" : { "avg" : { "field" : "pressionField" } }  
}  
}  
}

Do you know how to fix it ?

Regards,

S

---

<div class="post-metadata">

**Author:** ![byronvoorbach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/byronvoorbach/32/8283_2.png) [@byronvoorbach](https://discuss.elastic.co/u/byronvoorbach)\
**Post date:** [May 30, 2016, 12:09pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/4 "2016-05-30T12:09:11Z")

</div>

Hi S,

If you want your avg\_pression aggregation to be a sub aggregation to your devices aggregation, change your code to the following:

```
{
   "aggs": {
     "devices": {
       "terms": {
         "field": "deviceField"
       },
       "aggs": {
         "avg_pression": {
           "avg": {
             "field": "pressionField"
           }
         }
       }
     }
   }
 }

```

If you want your avg\_pression to also be a top level aggregation change it to the following:

```
{
  "aggs": {
    "devices": {
      "terms": {
        "field": "deviceField"
      }
    },
    "avg_pression": {
      "avg": {
        "field": "pressionField"
      }
    }
  }
}

```

Happy coding! 🙂

---

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [May 30, 2016, 12:48pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/5 "2016-05-30T12:48:40Z")

</div>

Thank you so much, it works very well 😃

---

<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:** [July 5, 2017, 10:48pm UTC](https://discuss.elastic.co/t/lucen-elasticsearch-a-query-for-a-fiield-with-multiple-values/51253/6 "2017-07-05T22:48:04Z")

</div>


