# How can i make that query? is that a nested query?

**URL:** <https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687>\
**Category:** Elasticsearch\
**Created:** [June 25, 2020, 2:03pm UTC](https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687 "2020-06-25T14:03:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mateusd24](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mateusd24/32/137523_2.png) [@mateusd24](https://discuss.elastic.co/u/mateusd24)\
**Post date:** [June 25, 2020, 2:03pm UTC](https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687/1 "2020-06-25T14:03:56Z")

</div>

Hi people,

I want to make a query for car with price between 50000 and 100000.  
from that json:

```auto
[{
	"_id": "aaa",
	"_source": {
		"objs": [{
				"name": "car",
				"price": 60000
			},
			{
				"name": "milk",
				"price": 2
			}
		]
	}
}, {
	"_id": "bbb",
	"_source": {
		"objs": [{
				"name": "car",
				"price": 20000
			},
			{
				"name": "truck",
				"price": 75000
			}
		]
	}

}]

```

If i use match : "car" with range of price gte 50000 and lte 100000 it will return the both \_ids but the correct will be only \_id: "aaa".

Making some search i found that: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)

is that is correct to my use? have others way without change the mapping to make that query?

Can anyone have some examples of how can i do that?

Appreciate any help

---

<div class="post-metadata">

**Author:** ![spdoes](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@spdoes](https://discuss.elastic.co/u/spdoes)\
**Post date:** [June 25, 2020, 7:17pm UTC](https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687/2 "2020-06-25T19:17:55Z")

</div>

Very simple!

You are to created a nested type of mapping for "objs" field.  
You can check more info here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)

1. First you have create a nested field:

```auto

    PUT /my_index/_mapping
     {
        "properties": {
          "objs": {
            "type": "nested" 
          }
        }
      }

```

1. Then you add an object normally as if you didn´t had this mapping

2. Now you just make a query of nested type:

```auto
GET my_index/_search
{
  "query": {
    "nested": {
      "path": "objs",
      "query": {
        "bool": {
          "must": [
            { "match": { "objs.name": "car" }},
            { "match": { "objs.price": "6000" }} 
          ]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![mateusd24](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mateusd24/32/137523_2.png) [@mateusd24](https://discuss.elastic.co/u/mateusd24)\
**Post date:** [June 25, 2020, 8:38pm UTC](https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687/3 "2020-06-25T20:38:37Z")

</div>

Hey spdoes,

That solution shiny my day, i can imagine that was possible to do like that. i have been using an old guide.

thanks so much, have a nice day 🙂

---

<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 23, 2020, 8:38pm UTC](https://discuss.elastic.co/t/how-can-i-make-that-query-is-that-a-nested-query/238687/4 "2020-07-23T20:38:38Z")

</div>

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