Same type name and object key name

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' -d '
{
"article" : {
"properties" : {
"cat" : {
"type" : "object",
"properties" : {
"cat_id" : {
"type" : "long"
}
}
},
"article_id" : {
"type" : "long"
}
}
}
}'

curl -XPUT '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' -d
'{"article_id":1,"cat":{"cat_id":1}}'
curl -XPUT 'http://localhost:9200/mm/article/2' -d
'{"article_id":2,"cat":{"cat_id":1}}'
curl -XPUT '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' -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' -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' -d
'{"filter":{"term":{"cat.cat_id":1}}}'

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

Thanks for your help.

--

Hello Benjamin,

On Tue, Dec 11, 2012 at 6:29 PM, Benjamin VIELLARD 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' -d '
{
"article" : {
"properties" : {
"cat" : {
"type" : "object",
"properties" : {
"cat_id" : {
"type" : "long"
}
}
},
"article_id" : {
"type" : "long"
}
}
}
}'

curl -XPUT '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' -d
'{"article_id":1,"cat":{"cat_id":1}}'
curl -XPUT 'http://localhost:9200/mm/article/2' -d
'{"article_id":2,"cat":{"cat_id":1}}'
curl -XPUT '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' -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' -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:
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/ -- ElasticSearch -- Solr -- Lucene

--

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<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' -d '
{
"article" : {
"properties" : {
"cat" : {
"type" : "object",
"properties" : {
"cat_id" : {
"type" : "long"
}
}
},
"article_id" : {
"type" : "long"
}
}
}
}'

curl -XPUT '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' -d
'{"article_id":1,"cat":{"cat_id":1}}'
curl -XPUT 'http://localhost:9200/mm/article/2' -d
'{"article_id":2,"cat":{"cat_id":1}}'
curl -XPUT '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' -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' -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:
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/ -- ElasticSearch -- Solr -- Lucene

--