# Filter by min nested value

**URL:** <https://discuss.elastic.co/t/filter-by-min-nested-value/94220>\
**Category:** Elasticsearch\
**Created:** [July 23, 2017, 1:49am UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220 "2017-07-23T01:49:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![IsaacInstrumental](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isaacinstrumental/32/19807_2.png) [@IsaacInstrumental](https://discuss.elastic.co/u/IsaacInstrumental)\
**Post date:** [July 23, 2017, 1:49am UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/1 "2017-07-23T01:49:20Z")

</div>

I have a bunch of documents of the form:

```auto
[
    {
        title: string,
        comments: [
            comment_body: string,
            created_date: date,
        ],
    },
    ...
]

```

How can I get the posts whose first comment is after a specific date? I've searched for a solution and read the documentation, and I've found a bunch of things about aggregations and querying nested values, but I haven't found a way to put everything together.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [July 23, 2017, 12:40pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/2 "2017-07-23T12:40:23Z")

</div>

Hi,

Have a look to this doc: [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-sort.html#nested-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-sort.html#nested-sorting)

hope this help

---

<div class="post-metadata">

**Author:** ![IsaacInstrumental](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isaacinstrumental/32/19807_2.png) [@IsaacInstrumental](https://discuss.elastic.co/u/IsaacInstrumental)\
**Post date:** [July 23, 2017, 10:36pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/3 "2017-07-23T22:36:27Z")

</div>

Thank you for responding.

I did find that page about nested sorting in the documentation, but it doesn't solve the problem of filtering to posts whose first comment is after a specific date. Currently I have only discovered how to filter to posts that have any comment after a specific date, sorted by the earliest comment's date. However, that includes posts whose earliest comment is after the specified date, which is not what I want.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [July 24, 2017, 12:28pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/4 "2017-07-24T12:28:24Z")

</div>

Hi,

And if you do a boolean query like this:

must have nested comments after "the date"  
must NOT have nested comment BEFORE "the date"

order by nested comment date

Does it solve the problem ?

---

<div class="post-metadata">

**Author:** ![IsaacInstrumental](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isaacinstrumental/32/19807_2.png) [@IsaacInstrumental](https://discuss.elastic.co/u/IsaacInstrumental)\
**Post date:** [July 24, 2017, 7:16pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/5 "2017-07-24T19:16:14Z")

</div>

That worked! Can't believe I didn't think of that. Thanks for your help 🙂

For others who are looking for this in the future, here's an example query:

```auto
{
	query: {
		bool: {
			must: [
				{
					nested: {
						path: 'comments',
						query: {
							bool: {
								must: [
									{
										range: {
											'comments.created_date': {
												gte: '2017-07-24T15:57:25.145Z',
											},
										},
									},
								],
								must_not: [
									{
										range: {
											'comments.created_date': {
												lt: '2017-07-24T15:57:25.145Z',
											},
										},
									},
								],
							},
						},
					},
				},
			],
		},
	},
	size: 10000,
	sort: {
		'comments.created_date': {
			order: 'desc',
			mode: 'max',
			nested_path: 'comments',
		},
	},
}

```

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [July 24, 2017, 8:00pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/6 "2017-07-24T20:00:19Z")

</div>

> [@IsaacInstrumental](#):
>
> That worked!

👍 good to hear that ! You can mark as Resolved 😉

---

<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:** [August 21, 2017, 8:00pm UTC](https://discuss.elastic.co/t/filter-by-min-nested-value/94220/7 "2017-08-21T20:00:41Z")

</div>

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