# Same type name and object key name

**URL:** <https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042>\
**Category:** Elasticsearch\
**Created:** [December 11, 2012, 4:29pm UTC](https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042 "2012-12-11T16:29:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_VIELLARD](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_viellard/32/2578_2.png) [@Benjamin\_VIELLARD](https://discuss.elastic.co/u/Benjamin_VIELLARD)\
**Post date:** [December 11, 2012, 4:29pm UTC](https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042/1 "2012-12-11T16:29:25Z")

</div>

I have a strange behaviour with ES (but I think my schema/query is wrong).

For example a simple category \> article index like this :

curl -XPUT '[http://localhost:9200/mm/article/\_mapping](http://localhost:9200/mm/article/_mapping)' -d '  
{  
"article" : {  
"properties" : {  
"cat" : {  
"type" : "object",  
"properties" : {  
"cat\_id" : {  
"type" : "long"  
}  
}  
},  
"article\_id" : {  
"type" : "long"  
}  
}  
}  
}'

curl -XPUT '[http://localhost:9200/mm/cat/\_mapping](http://localhost:9200/mm/cat/_mapping)' -d '  
{  
"cat" : {  
"properties" : {  
"cat\_id" : {  
"type" : "long"  
},  
"title" : {  
"type" : "string"  
}  
}  
}  
}'

And some data :

curl -XPUT '[http://localhost:9200/mm/article/1](http://localhost:9200/mm/article/1)' -d  
'{"article\_id":1,"cat":{"cat\_id":1}}'  
curl -XPUT '[http://localhost:9200/mm/article/2](http://localhost:9200/mm/article/2)' -d  
'{"article\_id":2,"cat":{"cat\_id":1}}'  
curl -XPUT '[http://localhost:9200/mm/cat/1](http://localhost:9200/mm/cat/1)' -d '{"cat\_id":1,"title":"cat  
title"}'

I want to filter articles by category ID :

curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
'{"filter":{"and":[{"type":{"value":"article"}},{"term":{"cat.cat\_id":1}}]}}'

return 0 hits instead of 2.

I guess this is because cat.cat\_id is at the same time a {sub}.{field} of  
"article" type and {type}.{field} of "cat" type.

So I found that a query like this :

curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
'{"filter":{"and":[{"type":{"value":"article"}},{"term":{"_article._  
cat.cat\_id":1}}]}}'

returns my 2 articles, good !

Is it safe to \_search on a full index and use type prefixed name in filters  
? Can't find in the documentation.

Of course I could rewrite the query like this :

curl -XGET '[http://localhost:9200/mm/article/\_search](http://localhost:9200/mm/article/_search)' -d  
'{"filter":{"term":{"cat.cat\_id":1}}}'

and it works, but I do need a \_search on multiple types.

Thanks for your help.

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [December 12, 2012, 11:10am UTC](https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042/2 "2012-12-12T11:10:28Z")

</div>

Hello Benjamin,

On Tue, Dec 11, 2012 at 6:29 PM, Benjamin VIELLARD [gbicou@gmail.com](mailto:gbicou@gmail.com) wrote:

> I have a strange behaviour with ES (but I think my schema/query is wrong).
> 
> For example a simple category \> article index like this :
> 
> curl -XPUT '[http://localhost:9200/mm/article/\_mapping](http://localhost:9200/mm/article/_mapping)' -d '  
> {  
> "article" : {  
> "properties" : {  
> "cat" : {  
> "type" : "object",  
> "properties" : {  
> "cat\_id" : {  
> "type" : "long"  
> }  
> }  
> },  
> "article\_id" : {  
> "type" : "long"  
> }  
> }  
> }  
> }'
> 
> curl -XPUT '[http://localhost:9200/mm/cat/\_mapping](http://localhost:9200/mm/cat/_mapping)' -d '  
> {  
> "cat" : {  
> "properties" : {  
> "cat\_id" : {  
> "type" : "long"  
> },  
> "title" : {  
> "type" : "string"  
> }  
> }  
> }  
> }'
> 
> And some data :
> 
> curl -XPUT '[http://localhost:9200/mm/article/1](http://localhost:9200/mm/article/1)' -d  
> '{"article\_id":1,"cat":{"cat\_id":1}}'  
> curl -XPUT '[http://localhost:9200/mm/article/2](http://localhost:9200/mm/article/2)' -d  
> '{"article\_id":2,"cat":{"cat\_id":1}}'  
> curl -XPUT '[http://localhost:9200/mm/cat/1](http://localhost:9200/mm/cat/1)' -d '{"cat\_id":1,"title":"cat  
> title"}'
> 
> I want to filter articles by category ID :
> 
> curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
> '{"filter":{"and":[{"type":{"value":"article"}},{"term":{"cat.cat\_id":1}}]}}'
> 
> return 0 hits instead of 2.
> 
> I guess this is because cat.cat\_id is at the same time a {sub}.{field} of  
> "article" type and {type}.{field} of "cat" type.
> 
> So I found that a query like this :
> 
> curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
> '{"filter":{"and":[{"type":{"value":"article"}},{"term":{"_article._  
> cat.cat\_id":1}}]}}'
> 
> returns my 2 articles, good !
> 
> Is it safe to \_search on a full index and use type prefixed name in  
> filters ? Can't find in the documentation.

Yes, it's a perfectly fine way to do it. It's actually written in here:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

Under the "Mapping Types" section.

However, as written in the same page, you might run into issues if you try  
to do faceting on that field. So I think the safest way to go is to rename  
one of the fields if it's possible.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

--

---

<div class="post-metadata">

**Author:** ![Benjamin\_VIELLARD](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_viellard/32/2578_2.png) [@Benjamin\_VIELLARD](https://discuss.elastic.co/u/Benjamin_VIELLARD)\
**Post date:** [December 12, 2012, 1:21pm UTC](https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042/3 "2012-12-12T13:21:59Z")

</div>

Thanks ! Didn't notice this in the "mapping" section of doc, i was looking  
in "search" and "filter".

I'm not faceting on this field so it's OK.

On Wednesday, December 12, 2012 12:10:28 PM UTC+1, Radu Gheorghe wrote:

> Hello Benjamin,
> 
> On Tue, Dec 11, 2012 at 6:29 PM, Benjamin VIELLARD \<[gbi...@gmail.com](mailto:gbi...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > I have a strange behaviour with ES (but I think my schema/query is wrong).
> > 
> > For example a simple category \> article index like this :
> > 
> > curl -XPUT '[http://localhost:9200/mm/article/\_mapping](http://localhost:9200/mm/article/_mapping)' -d '  
> > {  
> > "article" : {  
> > "properties" : {  
> > "cat" : {  
> > "type" : "object",  
> > "properties" : {  
> > "cat\_id" : {  
> > "type" : "long"  
> > }  
> > }  
> > },  
> > "article\_id" : {  
> > "type" : "long"  
> > }  
> > }  
> > }  
> > }'
> > 
> > curl -XPUT '[http://localhost:9200/mm/cat/\_mapping](http://localhost:9200/mm/cat/_mapping)' -d '  
> > {  
> > "cat" : {  
> > "properties" : {  
> > "cat\_id" : {  
> > "type" : "long"  
> > },  
> > "title" : {  
> > "type" : "string"  
> > }  
> > }  
> > }  
> > }'
> > 
> > And some data :
> > 
> > curl -XPUT '[http://localhost:9200/mm/article/1](http://localhost:9200/mm/article/1)' -d  
> > '{"article\_id":1,"cat":{"cat\_id":1}}'  
> > curl -XPUT '[http://localhost:9200/mm/article/2](http://localhost:9200/mm/article/2)' -d  
> > '{"article\_id":2,"cat":{"cat\_id":1}}'  
> > curl -XPUT '[http://localhost:9200/mm/cat/1](http://localhost:9200/mm/cat/1)' -d '{"cat\_id":1,"title":"cat  
> > title"}'
> > 
> > I want to filter articles by category ID :
> > 
> > curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
> > '{"filter":{"and":[{"type":{"value":"article"}},{"term":{"cat.cat\_id":1}}]}}'
> > 
> > return 0 hits instead of 2.
> > 
> > I guess this is because cat.cat\_id is at the same time a {sub}.{field} of  
> > "article" type and {type}.{field} of "cat" type.
> > 
> > So I found that a query like this :
> > 
> > curl -XGET '[http://localhost:9200/mm/\_search](http://localhost:9200/mm/_search)' -d  
> > '{"filter":{"and":[{"type":{"value":"article"}},{"term":{"_article._  
> > cat.cat\_id":1}}]}}'
> > 
> > returns my 2 articles, good !
> > 
> > Is it safe to \_search on a full index and use type prefixed name in  
> > filters ? Can't find in the documentation.
> 
> Yes, it's a perfectly fine way to do it. It's actually written in here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/)
> 
> Under the "Mapping Types" section.
> 
> However, as written in the same page, you might run into issues if you try  
> to do faceting on that field. So I think the safest way to go is to rename  
> one of the fields if it's possible.
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

--

---

<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 6, 2017, 3:00am UTC](https://discuss.elastic.co/t/same-type-name-and-object-key-name/10042/4 "2017-07-06T03:00:05Z")

</div>

Closed on July 6, 2017, 3:00am UTC


