# Subfield query not returning any data

**URL:** <https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561>\
**Category:** Elasticsearch\
**Created:** [August 5, 2022, 4:58pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561 "2022-08-05T16:58:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baerswords](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Baerswords](https://discuss.elastic.co/u/Baerswords)\
**Post date:** [August 5, 2022, 4:58pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/1 "2022-08-05T16:58:17Z")

</div>

New to Elasticsearch, so please bear with me if this is a simple question that I should know. I have an index with fairly simple documents. When I try to query against anything in AppDataDict, though, I don't ever get a return. What am I doing wrong? Document, Mapping, Query, and return are below.

**Document:**

```auto
{
	"_index": "reporting",
	"_type": "_doc",
	"_id": "XHw3QYIBF21eEEgTi_Aa",
	"_score": 4.6823745,
	"_source": {
		"PageSize": 0,
		"Page": 0,
		"AppDataDict": {
			"UserName": "Presley, Elvis",
			"Location1": "R",
			"Location2": "POPLAR",
			"DeviceDescription": null,
			"DeviceType": null,
			"DeviceIP": null
		},
		"EventType": 1,
		"EventDescription": "User logged out",
		"Success": true,
		"EventDate": "2022-06-20T13:59:14.893",
		"Id": 191
	}
}

```

**Mapping:**

```auto
{
    "reporting": {
        "mappings": {
            "properties": {
                "AppDataDict": {
                    "type": "nested",
                    "properties": {
                        "DeviceDescription": {
                            "type": "text"
                        },
                        "DeviceType": {
                            "type": "text"
                        },
                        "FacilityName": {
                            "type": "text"
                        },
                        "Location1": {
                            "type": "text"
                        },
                        "Location2": {
                            "type": "text"
                        },
                        "UserName": {
                            "type": "text"
                        }
                    }
                },
                "EventDate": {
                    "type": "date"
                },
                "EventDescription": {
                    "type": "text"
                },
                "EventType": {
                    "type": "integer"
                },
                "Id": {
                    "type": "long"
                },
                "Page": {
                    "type": "integer"
                },
                "PageSize": {
                    "type": "integer"
                },
                "Success": {
                    "type": "boolean"
                }
            }
        }
    }
}

```

**Query:**

```auto
{
    "query": {
        "bool": {
            "must": [
                {"match": {"AppDataDict.Location2": "POPLAR"}}
            ]
        }
    }
}

```

**Result:**

```auto
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [August 5, 2022, 5:18pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/2 "2022-08-05T17:18:06Z")

</div>

Hi @Baerswords

The "nested" type has a special query type. The [Nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query).

Try:

```auto
{
  "query": {
    "nested": {
      "path": "AppDataDict",
      "query": {
        "match": {
          "AppDataDict.Location2": "POPLAR"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Baerswords](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Baerswords](https://discuss.elastic.co/u/Baerswords)\
**Post date:** [August 5, 2022, 6:34pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/3 "2022-08-05T18:34:46Z")

</div>

Tried that and got the error below. Is my mapping off maybe?

```auto
"caused_by": {
	"type": "illegal_state_exception",
	"reason": "[nested] failed to find nested object under path [AppDataDict]"
}

```

Also, I've seen numerous posts where people read subfields with a simple match query like I initially had. I need to be able to put it together with other regular match queries in a bool return. This format will make that impossible, won't it?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 5, 2022, 6:38pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/4 "2022-08-05T18:38:10Z")

</div>

As AppDataDict is a simple object and not an array of objects you do not need the nested mapping. Remove this and you no longer need to use nested queries.

---

<div class="post-metadata">

**Author:** ![Baerswords](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Baerswords](https://discuss.elastic.co/u/Baerswords)\
**Post date:** [August 5, 2022, 6:44pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/5 "2022-08-05T18:44:21Z")

</div>

That's exactly what I needed. I took out the nested path and everything works well. TY!

---

<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 2, 2022, 6:44pm UTC](https://discuss.elastic.co/t/subfield-query-not-returning-any-data/311561/6 "2022-09-02T18:44:30Z")

</div>

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