# Search by max(time) possible?

**URL:** <https://discuss.elastic.co/t/search-by-max-time-possible/196084>\
**Category:** Elasticsearch\
**Created:** [August 21, 2019, 10:25am UTC](https://discuss.elastic.co/t/search-by-max-time-possible/196084 "2019-08-21T10:25:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avinash\_D\_Silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_d_silva/32/45312_2.png) [@Avinash\_D\_Silva](https://discuss.elastic.co/u/Avinash_D_Silva)\
**Post date:** [August 21, 2019, 10:25am UTC](https://discuss.elastic.co/t/search-by-max-time-possible/196084/1 "2019-08-21T10:25:41Z")

</div>

if parent(p) with id 1 has two children(c): 1,2

time 111 is the first logs for parent-1  
whereas time 114 is the latest log values for parent-1  
is there a way to get the latest log values for parent-1,

records looks something like this:  
{p:1,c:1,k:v,time:114}, {p:1,c:2,k:v,time:114}, {p:1,c:1,k:v,time:111}, {p:1,c:2,k:v,time:111}

SQL would be something like:  
select \* from log\_index where p=1 and time=(select max(time) where p=1)

---

<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:** [August 26, 2019, 11:15pm UTC](https://discuss.elastic.co/t/search-by-max-time-possible/196084/2 "2019-08-26T23:15:30Z")

</div>

If you just need a single result of the max time, you can use `nested` aggregation with max aggregation.

Provided that you have index with nested fields:

```auto
{
    "mappings": {
        "_doc": {
            "properties": {
                "p": {
                    "type": "keyword"
                },
                "p_children": {
                    "type": "nested"
                }
            }
        }
    }
}

```

You index your documents:

```auto
POST my_index/_doc/_bulk?refresh=true
{ "index" : {"_id" : "1" } }
{"p":"p1", "p_children": [{"c":1,"k":"v","time":111}, {"c":2,"k":"v","time":114}]}
{ "index" : {"_id" : "2" } }
{"p":"p2", "p_children": [{"c":1,"k":"v","time":200}, {"c":2,"k":"v","time":201}]}

```

You can run the following nested aggregation to get a max time of the children of `p1`:

```auto
{
	"size": 0,
	"query" : {
		"match" : {"p" : "p1"}
	}, 
    "aggs": {
        "max_time_p": {
            "nested": {
                "path": "p_children"
            },
            "aggs": {
                "max_time": {
                    "max": {
                        "field": "p_children.time"
                    }
                }
            }
        }
    }
}

```

---

<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:** [September 23, 2019, 11:15pm UTC](https://discuss.elastic.co/t/search-by-max-time-possible/196084/3 "2019-09-23T23:15:42Z")

</div>

Closed on September 23, 2019, 11:15pm UTC

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