Term query to get children based on parent id


(Rukshan Perera) #1

Hello,

I have uploaded curl scripts to gist
https://gist.github.com/rukshanperera/9492610.

I have a three level parent/child types as shown in the following mappings

{ "Parent" :
{
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"Child" :{
"_parent": {
"type": "Parent"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"GrandChild" :{
"_parent": {
"type": "Child"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

I want to search for *GrandChild *documents based on its parent id (which
is the id of a Child in this example). I used the following query but it
does not return any hits. ( "bob" - id of *Child *document. "alice" - id
of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/GrandChild/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "bob" }
}
}'

However, I was able to get *Child *documents based on *Parent *id using the
following ( "alice" - id of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/Child/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "alice" }
}

}

Can someone please help me to get the first query working?

Thanks,
Rukshan

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Clinton Gormley) #2

Hi Rukshan

Very nicely laid out question. Thanks for providing all of the steps.

I agree that it doesn't work and (at least according to the docs) it
should, so I've opened an issue here:

Curiously, performing the same lookup using a has_parent query does work:

POST /myindex/GrandChild/_search
{
"query": {
"has_parent": {
"parent_type": "Child",
"query": {
"filtered": {
"filter": {
"term": {
"_id": "bob"
}
}
}
}
}
}
}

Clint

On 11 March 2014 20:20, Rukshan Perera rukshanperera@gmail.com wrote:

Hello,

I have uploaded curl scripts to gist
https://gist.github.com/rukshanperera/9492610.

I have a three level parent/child types as shown in the following mappings

{ "Parent" :
{
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"Child" :{
"_parent": {
"type": "Parent"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"GrandChild" :{
"_parent": {
"type": "Child"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

I want to search for *GrandChild *documents based on its parent id (which
is the id of a Child in this example). I used the following query but it
does not return any hits. ( "bob" - id of *Child *document. "alice" - id
of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/GrandChild/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "bob" }
}
}'

However, I was able to get *Child *documents based on *Parent *id using
the following ( "alice" - id of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/Child/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "alice" }
}

}

Can someone please help me to get the first query working?

Thanks,
Rukshan

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.comhttps://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAPt3XKQ5uOmhddsLBr%3D7hb%2BbMKGMtGng-FiyGUW3ps3%2B0UCtBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Rukshan Perera) #3

Hi Clint,

Thanks for the answer. 'has_parent' query you have suggested works and I
should be able to use it in my application.

Regards,
Rukshan

On Wednesday, March 12, 2014 3:31:19 AM UTC-7, Clinton Gormley wrote:

Hi Rukshan

Very nicely laid out question. Thanks for providing all of the steps.

I agree that it doesn't work and (at least according to the docs) it
should, so I've opened an issue here:
https://github.com/elasticsearch/elasticsearch/issues/5399

Curiously, performing the same lookup using a has_parent query does work:

POST /myindex/GrandChild/_search
{
"query": {
"has_parent": {
"parent_type": "Child",
"query": {
"filtered": {
"filter": {
"term": {
"_id": "bob"
}
}
}
}
}
}
}

Clint

On 11 March 2014 20:20, Rukshan Perera <ruksha...@gmail.com <javascript:>>wrote:

Hello,

I have uploaded curl scripts to gist
https://gist.github.com/rukshanperera/9492610.

I have a three level parent/child types as shown in the following mappings

{ "Parent" :
{
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"Child" :{
"_parent": {
"type": "Parent"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

{
"GrandChild" :{
"_parent": {
"type": "Child"
},
"properties" : {
"name" : {
"type" :"string",
"index":"analyzed"
}
}
}
}

I want to search for *GrandChild *documents based on its parent id
(which is the id of a Child in this example). I used the following query
but it does not return any hits. ( "bob" - id of *Child *document.
"alice" - id of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/GrandChild/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "bob" }
}
}'

However, I was able to get *Child *documents based on *Parent *id using
the following ( "alice" - id of *Parent *document)

curl -s -XPOST "http://localhost:9200/myindex/Child/_search?routing=alice" -d '
{
"query" : {
"term" : { "_parent" : "alice" }
}

}

Can someone please help me to get the first query working?

Thanks,
Rukshan

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearc...@googlegroups.com <javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.comhttps://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f2f6864b-b49f-4707-98af-8f7b58a01782%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4