# Querying Nested Datatype

**URL:** https://discuss.elastic.co/t/querying-nested-datatype/116408
**Category:** Elasticsearch
**Created:** [January 21, 2018, 10:35pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408 "2018-01-21T22:35:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [January 21, 2018, 10:35pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/1 "2018-01-21T22:35:43Z")

</div>

If I have following index mapping for nested type, can I filter on two different fields in the same document?

**Mapping:**

```
{
  "mappings": {
    "records": {
      "properties": {
        "record": {
          "type": "nested",
          "properties": {
            "IntFieldName": { "type": "string" },
            "IntFieldValue": { "type": "integer" },
            "BoolFieldName": { "type": "string" },
            "BoolFieldValue": { "type": "boolean" },
            "KeywordFieldName": { "type": "string" },
            "KeywordFieldValue": { "type": "keyword" }
          }
        }
      }
    }
  }
}

```

**Index:**

```
  {
    "record": [
    	{
	    	"IntFieldName": "myint1",
	    	"IntFieldValue": 1,
	    	"BoolFieldName": "mybool1",
	    	"BoolFieldValue": true,
	    	"KeywordFieldName": "mykeyword1",
	    	"KeywordFieldValue": "foo"
    	},
    	{
	    	"IntFieldName": "myint2",
	    	"IntFieldValue": 2,
	    	"BoolFieldName": "mybool2",
	    	"BoolFieldValue": false,
	    	"KeywordFieldName": "mykeyword2",
	    	"KeywordFieldValue": "bar"
    	}
    ]
}

```

I want to run query equal to something like this:

```
WHERE IntFieldName = "myint1" 
AND IntFieldValue = 1 
AND IntFieldName = "myInt2" 
AND IntFieldValue = 2

```

Following is the query, but it does not fetch any results. Is my query incorrect or is it not possible in ES?

**Query:**

```
{
  "query": {
    "nested": {
      "path": "record",
      "query": {
        "bool": {
          "filter": [
            { "match": { "record.IntFieldName": "myint1" }},
            { "match": { "record.IntFieldValue": 1 }},
            { "match": { "record.IntFieldName": "myint2" }},
            { "match": { "record.IntFieldValue": 2 }}            
          ]
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![Arvind\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arvind_rao/32/26758_2.png) [@Arvind\_Rao](https://discuss.elastic.co/u/Arvind_Rao)
#### Post date: [January 22, 2018, 7:13am UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/2 "2018-01-22T07:13:53Z")

</div>

A filter acts like a "AND" clause. In the context of nested structures, the query you provided will not return any results because an element inside the array can either match "myint1" or "myint2", not both.

So this will work:

```auto
{
	"query": {
		"nested": {
			"path": "record",
			"query": {
				"bool": {
					"filter": [{
							"match": {
								"record.IntFieldName": "myint1"
							}
						},
						{
							"match": {
								"record.IntFieldValue": 1
							}
						}
					]
				}
			},
			"inner_hits": {}
		}
	}
}

```

I added inner\_hits which is a useful clause to see which elements in the array matched.

If you need to return arrays that contain both "myint1" and "myint2", I don't think it is supported directly but I would try the following (which works like an OR) and then check the inner hits object to see if it contains n records (where n = number of terms you are matching, in this case 2).

```auto
{
	"query": {
		"nested": {
			"path": "record",
			"query": {
				"bool": {
					"filter": [{
							"terms": {
								"record.IntFieldName": [
									"myint1", "myint2"
								]
							}
						},
						{
							"terms": {
								"record.IntFieldValue": [
									1, 2
								]
							}
						}
					]
				}
			},
			"inner_hits": {}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [January 22, 2018, 7:31am UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/3 "2018-01-22T07:31:46Z")

</div>

@Arvind_Rao Thank you so much for looking into and and adding examples.

If this is not supported directly, I was wondering if there is an alternative schema that you can suggest to achieve the following.

- An index has two fields for each data type. The intent is to limit the number of fields in an index. For example, for `integer` type, first field is the name of the field and 2nd field is the value of the field like described in my original example (i.e. `IntFieldName` and `IntFieldValue`)
- There could be multiple fields for each type
- Support filtering and aggregations. For example, filter with multiple `AND` or `must` clauses like explained in the original example.

---

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 22, 2018, 2:17pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/4 "2018-01-22T14:17:30Z")

</div>

I've the same issue, if you can get a solution i beg you to public in this thread or in [mine](https://discuss.elastic.co/t/nested-filter-problem/116351)

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Arvind\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arvind_rao/32/26758_2.png) [@Arvind\_Rao](https://discuss.elastic.co/u/Arvind_Rao)
#### Post date: [January 24, 2018, 7:41am UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/5 "2018-01-24T07:41:53Z")

</div>

I guess you learn something new every day 🙂

This [StackOverflow post](https://stackoverflow.com/a/24540656) has the solution to your original question.

```auto
{
	"query": {
		"bool": {
			"must": [{
					"nested": {
						"path": "record",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"record.IntFieldName": "myint1"
										}

									},
									{
										"term": {
											"record.IntFieldValue": 1
										}
									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "record",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"record.IntFieldName": "myint2"
										}

									},
									{
										"term": {
											"record.IntFieldValue": 2
										}
									}
								]
							}
						}
					}
				}
			]
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![awakevzla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/awakevzla/32/26901_2.png) [@awakevzla](https://discuss.elastic.co/u/awakevzla)
#### Post date: [January 24, 2018, 2:57pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/6 "2018-01-24T14:57:26Z")

</div>

> [@Arvind\_Rao](#):
>
> I guess you learn something new every day 🙂

That's very true! Thanks for the support!

---

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [January 27, 2018, 5:56pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/7 "2018-01-27T17:56:13Z")

</div>

True. We ended up finding the same solution. Thanks so much @Arvind_Rao

---

<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: [February 24, 2018, 5:56pm UTC](https://discuss.elastic.co/t/querying-nested-datatype/116408/8 "2018-02-24T17:56:35Z")

</div>

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