# Array of objects max value

**URL:** <https://discuss.elastic.co/t/array-of-objects-max-value/116481>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 12:23pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481 "2018-01-22T12:23:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arkkanoid](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@arkkanoid](https://discuss.elastic.co/u/arkkanoid)\
**Post date:** [January 22, 2018, 12:23pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481/1 "2018-01-22T12:23:02Z")

</div>

For a set of documents like this:  
{  
name: "php",  
related:[  
{  
"count": 6806,  
"name": "c#"  
},  
{  
"count": 4080,  
"name": "c"  
},  
{  
"count": 9745,  
"name": "java"  
},  
{  
"count": 9141,  
"name": "javascript"  
}  
]  
}

I would like to get the name of the documents that match:  
[related.name](http://related.name): "javascript"  
and  
related.count is the max value in the array.

It's possible to do?

Thanks

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [February 1, 2018, 10:04pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481/2 "2018-02-01T22:04:21Z")

</div>

Hi there,  
yes it is possible. You can index your `related` field as [nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) :

```
{
  "mappings": {
    "my_type": {
      "properties": {
        "related": {
          "type": "nested" 
        }
      }
    }
  }
}

```

to find a documents that have `related.name: "javascript"`, use [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html):

```
{
  "query": {
    "nested": {
      "path": "related",
      "query": {
        "match": { "related.name": "javascript" }
       }
    }
  }
}

```

For finding max values, you can use [nested aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html):

```
"aggs" : {
    "myagg" : {
        "nested" : {
            "path" : "related"
        },
        "aggs" : {
            "max_value" : { "max" : { "field" : "related.count" } }
        }
    }
}

```

You can combine a search query and aggregation in a single request depending on your needs.

---

<div class="post-metadata">

**Author:** ![arkkanoid](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@arkkanoid](https://discuss.elastic.co/u/arkkanoid)\
**Post date:** [February 2, 2018, 10:12am UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481/3 "2018-02-02T10:12:58Z")

</div>

Hi Mayya,  
Thanks for your answer.  
Trying the code you provided me I get:  
"aggregations": {  
"myagg": {  
"doc\_count": 19805,  
"max\_value": {  
"value": 1110971  
}  
}  
}

What I wanted was to get the name of the results that match with related.name == javascript AND related.count if the maximum in the array.

So, the mapping actually is:

```
{
  "mappings": {
    "my_type": {
      "properties": {
       "name":{type:"keyword"},
        "related": {
          "type": "nested" ,
         "properties":{
          "name":{type:"keyword"},
          "count":{type:"long"}
         }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [February 3, 2018, 10:06pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481/4 "2018-02-03T22:06:52Z")

</div>

It is not very clear to me what you mean "related.count if the maximum in the array."?  
Can you please provide an example of the response you expect based on your original example data?

---

<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:** [March 3, 2018, 10:07pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/116481/5 "2018-03-03T22:07:08Z")

</div>

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