# Fetching documents with calendarItems.minNights first value greater than 2 using Elasticsearch DSL

**URL:** <https://discuss.elastic.co/t/fetching-documents-with-calendaritems-minnights-first-value-greater-than-2-using-elasticsearch-dsl/335411>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 7, 2023, 8:44am UTC](https://discuss.elastic.co/t/fetching-documents-with-calendaritems-minnights-first-value-greater-than-2-using-elasticsearch-dsl/335411 "2023-06-07T08:44:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Engin\_KARTAL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/engin_kartal/32/121937_2.png) [@Engin\_KARTAL](https://discuss.elastic.co/u/Engin_KARTAL)\
**Post date:** [June 7, 2023, 8:44am UTC](https://discuss.elastic.co/t/fetching-documents-with-calendaritems-minnights-first-value-greater-than-2-using-elasticsearch-dsl/335411/1 "2023-06-07T08:44:53Z")

</div>

Hello,

I would like to learn how to fetch documents with the first value of the `calendarItems.minNights` field greater than 2 using Elasticsearch DSL. The data structure looks like this:

In the above data structure, I want to retrieve documents where the first value of `calendarItems.minNights` is greater than 2. What should be the Elasticsearch DSL query for this?

Thank you.

Data

```auto
{
  "_index": "list",
  "_id": "5150",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "id": 5150,
    "title": "test title",
    "calendarItems": [
      {
        "actualDate": "2023-07-10T00:00:00+03:00",
        "price": 458,
        "minNights": 4,
        "status": "booked",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-11T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "available",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-12T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "available",
        "reason": null,
        "isBlock": null
      },
      {
        "actualDate": "2023-07-12T00:00:00+03:00",
        "price": 458,
        "minNights": 2,
        "status": "booked",
        "reason": null,
        "isBlock": null
      }
    ]
  }
}

```

Query

```auto
 {
	"query": {
		"bool": {
			"must": [
				{
					"nested": {
						"path": "calendarItems",
						"query": {
							"bool": {
								"must": [
									{
										"range": {
											"calendarItems.actualDate": {
												"gte": "2023-07-07T00:00:00+03:00",
												"lte": "2023-07-12T23:59:59+03:00"
											}
										}
									},
									{
										"term": {
											"calendarItems.status": "available"
										}
									},
									{
										"script": {
											"script": {
												"source": "2 >= doc['calendarItems.minNights'].value",
												"lang": "painless"
											}
										}
									}
								]
							}
						}
					}
				}
			],
			"must_not": [
				{
					"nested": {
						"path": "calendarItems",
						"query": {
							"bool": {
								"must": [
									{
										"term": {
											"calendarItems.status": "booked"
										}
									},
									{
										"range": {
											"calendarItems.actualDate": {
												"gte": "2023-07-07T00:00:00+03:00",
												"lte": "2023-07-12T23:59:59+03:00"
											}
										}
									}
								]
							}
						}
					}
				}
			]
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![Engin\_KARTAL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/engin_kartal/32/121937_2.png) [@Engin\_KARTAL](https://discuss.elastic.co/u/Engin_KARTAL)\
**Post date:** [June 22, 2023, 7:51am UTC](https://discuss.elastic.co/t/fetching-documents-with-calendaritems-minnights-first-value-greater-than-2-using-elasticsearch-dsl/335411/2 "2023-06-22T07:51:24Z")

</div>

**I solved this**

```auto
{
	"query": {
		"bool": {
			"must": [
				{
					"nested": {
						"path": "calendarItems",
						"query": {
							"bool": {
								"must": [
									{
										"range": {
											"calendarItems.actualDate": {
												"gte": "2023-07-07T00:00:00+03:00",
												"lte": "2023-07-15T23:59:59+03:00"
											}
										}
									},
									{
										"term": {
											"calendarItems.status": "available"
										}
									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "calendarItems",
						"query": {
							"bool": {
								"must": [
									{
										"range": {
											"calendarItems.actualDate": {
												"gte": "2023-07-07T00:00:00+03:00",
												"lte": "2023-07-07T23:59:59+03:00"
											}
										}
									},
									{
										"term": {
											"calendarItems.status": "available"
										}
									},
									{
										"script": {
											"script": {
												"source": "8 >= doc['calendarItems.minNights'].value",
												"lang": "painless"
											}
										}
									}
								]
							}
						}
					}
				}
			],
			"must_not": [
				{
					"nested": {
						"path": "calendarItems",
						"query": {
							"bool": {
								"must": [
									{
										"term": {
											"calendarItems.status": "booked"
										}
									},
									{
										"range": {
											"calendarItems.actualDate": {
												"gte": "2023-07-07T00:00:00+03:00",
												"lte": "2023-07-15T23:59:59+03:00"
											}
										}
									}
								]
							}
						}
					}
				}
			]
		}
	}
}

```

---

<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 20, 2023, 7:52am UTC](https://discuss.elastic.co/t/fetching-documents-with-calendaritems-minnights-first-value-greater-than-2-using-elasticsearch-dsl/335411/3 "2023-07-20T07:52:01Z")

</div>

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