# Array of objects Max value

**URL:** <https://discuss.elastic.co/t/array-of-objects-max-value/263849>\
**Category:** Elasticsearch\
**Created:** [February 10, 2021, 10:18am UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849 "2021-02-10T10:18:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sivanzakay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sivanzakay/32/83562_2.png) [@sivanzakay](https://discuss.elastic.co/u/sivanzakay)\
**Post date:** [February 10, 2021, 10:18am UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849/1 "2021-02-10T10:18:07Z")

</div>

According the documents below :  
How do I get all documents that  
field driver.vehicle.make = Powell Motors" and this field has the **max** year in the array ?  
(It should return only doc 1 and doc 3)  
I would like it will be executed in one query

**doc 1 :**

```
"driver": {
  "last_name": "McQueen",
  "vehicle": [
    {
      "make": "Powell Motors",
      "model": "Canyonero",
      "year": 2020
    },
    {
      "make": "Miller-Meteor",
      "model": "Ecto-1",
      "year": 2005
    }
  ]
}

```

**doc 2 :**

```
 "driver": {
  "last_name": "McQueen",
  "vehicle": [
    {
      "make": "Powell Motors",
      "model": "Canyonero",
      "year": 1999
    },
    {
      "make": "Miller-Meteor",
      "model": "Ecto-1",
      "year": 2018
    }
  ]
}

```

**doc 3 :**

```
"driver": {
"last_name": "McQueen",
"vehicle": [
  {
    "make": "Powell Motors",
    "model": "Canyonero",
    "year": 2003
  },
  {
    "make": "Miller-Meteor",
    "model": "Ecto-1",
    "year": 1998
  }
]}
```

---

<div class="post-metadata">

**Author:** ![sivanzakay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sivanzakay/32/83562_2.png) [@sivanzakay](https://discuss.elastic.co/u/sivanzakay)\
**Post date:** [February 14, 2021, 2:42pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849/2 "2021-02-14T14:42:34Z")

</div>

I'll appreciate your help.  
Thanks  
Sivan

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 14, 2021, 2:52pm UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849/3 "2021-02-14T14:52:55Z")

</div>

Can you explain `field has the max year in the array` part? Not sure why document 1 and 3 would be returned or what you are looking for.

---

<div class="post-metadata">

**Author:** ![sivanzakay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sivanzakay/32/83562_2.png) [@sivanzakay](https://discuss.elastic.co/u/sivanzakay)\
**Post date:** [February 15, 2021, 8:37am UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849/4 "2021-02-15T08:37:42Z")

</div>

Hi Aaron ,  
Let's look at the array of "vehicle" objects for each document.  
I want to get all documents in which has object that meets the following conditions :  
Field "year" of that object is maximum in the array and the Field "make" of this object == "Powell Motors"

doc 1 : Max year is 2020 (\>2005) .  
The object with the max year is :

> {  
> "make": "Powell Motors",  
> "model": "Canyonero",  
> "year": 2020  
> }

Field "make" of this object equals to " **Powell Motors**" `(conditions are met)`

doc 2 : Max year is 2018.  
The object with the max year is :

> {  
> "make": "Miller-Meteor",  
> "model": "Ecto-1",  
> "year": 2018  
> }

Field "make" of this object equals to "Miller-Meteor". `(Does not meet the requirement. Object "make" field equals to "Miller-Meteor" and not "Powell Motors" as required)`

doc 3: Max year is 2003.  
The object with the max year is :

> {  
> "make": "Powell Motors",  
> "model": "Canyonero",  
> "year": 2003  
> }  
> Field "make" of this object equals to " **Powell Motors**"`(conditions are met)`

Thanks,

Sivan

---

<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 15, 2021, 8:37am UTC](https://discuss.elastic.co/t/array-of-objects-max-value/263849/5 "2021-03-15T08:37:57Z")

</div>

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