# Java: Exact match on nested field

**URL:** <https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845>\
**Category:** Elasticsearch\
**Created:** [July 19, 2016, 9:12am UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845 "2016-07-19T09:12:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [July 19, 2016, 9:12am UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/1 "2016-07-19T09:12:41Z")

</div>

Hi,

until now i had to identify a datastructure by a single sef-url. In my json doc i had:  
"sefUrl" : "cat-13"

I had to set sefUrl as not\_analyzed. To identify this datastructure with an exact match, i used following code:

```
final String sefUrl = "cat-13";
final SearchResponse response = getClient().prepareSearch(INDEX)
			.setTypes(TYPE).setSearchType(SearchType.QUERY_THEN_FETCH)
		        .setQuery(QueryBuilders.termQuery("sefUrl", sefUrl))
		        .execute()
		        .actionGet();

```

That worked fine until i now have the requirement for a multilanguage support on the sefUrl which i realized with a HashMap. In my json doc now i have:  
"sefUrlMap" : {  
"en" : "encat-13",  
"de" : "cat-13"  
}

How can i now do an exact match on this map to identify this datastructure?

With kind regards  
David

---

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [July 19, 2016, 12:29pm UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/2 "2016-07-19T12:29:36Z")

</div>

Now i defined the nested map as not\_analyzed and the mapping looks like:

> ```
> "sefUrlMap": {
> "type": "nested",
> "properties": {
> "de": {
> "type": "string",
> "index": "not_analyzed"
> },
> "en": {
> "type": "string",
> "index": "not_analyzed"
> }
> }
> }
> 
> ```

and i use the nestedquery:

```
final String sefUrl = "cat-13";
final SearchResponse response = _elasticSearchClient.getClient().prepareSearch(INDEX)
	.setTypes(TYPE).setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.nestedQuery("sefUrlMap", QueryBuilders.termQuery("de", sefUrl)))
        .execute()
        .actionGet();

```

But i still don't get any results.

What am i doing wrong?

Kind regards  
David

---

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [July 19, 2016, 1:48pm UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/3 "2016-07-19T13:48:57Z")

</div>

on i've kicked the nested type from my mapping and replaced it with the object type and use my termquery from before and access the inner field with the dot notation and it works

> **[Managing Relations Inside Elasticsearch
	  	 | Elastic](https://www.elastic.co/blog/managing-relations-inside-elasticsearch)**
>
> Data in the real world is rarely simple - often times it is a jumble of interlocking relations. How do you represent relational data in Elasticsearch? There are a few mechanisms that can be used to pr...

  
this i can read 100 times, i can not understand when to use object and when to use nested

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [July 19, 2016, 1:57pm UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/4 "2016-07-19T13:57:47Z")

</div>

You need to specify the full path in the query

```auto
.setQuery(QueryBuilders.nestedQuery("sefUrlMap", QueryBuilders.termQuery("sefUrlMap.de", sefUrl)))

```

See [https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-query.html](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-query.html) for more info.

However, using a nested type here looks like an overkill. It will be more efficient to implement it by using a standard object:

```auto
"sefUrlMap": {
	"type": "object",
	"properties": {
		"de": {
			"type": "string",
			"index": "not_analyzed"
		},
		"en": {
			"type": "string",
			"index": "not_analyzed"
		}
	}
}

```

and

```auto
.setQuery(QueryBuilders.termQuery("sefUrlMap.de", sefUrl))

```

Nested objects are typically used, when there is an array of such objects and you need to preserve relationship between fields within an object. Like in case of make and model in the example from the guide. If you have only one instance of an object, there is no relationship and therefore there is no need to use nested.

---

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [July 19, 2016, 2:11pm UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/5 "2016-07-19T14:11:29Z")

</div>

aaahhh,

when using the nested type i must specify the full path in the TERM query... that was not clear to me.

yeah, your 2nd is exactly what i do now.

thanks for clarification on the nested objects

---

<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 5, 2017, 10:34pm UTC](https://discuss.elastic.co/t/java-exact-match-on-nested-field/55845/6 "2017-07-05T22:34:22Z")

</div>


