Search child by parent field

Hello, I'm exploring Parent-Child feature in elasticsearch, but can't
understand whether it's possible to find child by parent's field name? Sort
of reverse 'has_child' query.

Here is example:

Author (parent)
-name
-country

Book (child of Author)

  • title
    ...

Can I search for all books author of which is from country:US for example?

Thanks.

Would it be an option in these cases to do a query on books with a
filter on author's country?

On 5 feb., 22:40, Igor Semenko i...@semenko.com wrote:

Hello, I'm exploring Parent-Child feature in elasticsearch, but can't
understand whether it's possible to find child by parent's field name? Sort
of reverse 'has_child' query.

Here is example:

Author (parent)
-name
-country

Book (child of Author)

  • title
    ...

Can I search for all books author of which is from country:US for example?

Thanks.

That would work, but I'm missing how to refer to the author in
search/filter.

Mapping:

curl -XPUT 'http://localhost:9200/test/'

curl -XPUT 'http://localhost:9200/test/author/_mapping' -d '{

"author" : {
    "properties" : {
        "name" : {"type" : "string"},
        "country" : {"type" : "string"}
    }
}

}
'

curl -XPUT 'http://localhost:9200/test/book/_mapping' -d '{

"book" : {
    "_parent" : {"type" : "author"},
    "properties" : {
        "title" : {"type" : "string"}
    }
}

}

Query/Filter??

curl -XGET 'http://localhost:9200/test/book/_search' -d '{
"explain" : true,
"query" : {
"bool" : {
"must" : {
"text" : {"title" : "book"}
},
"must" : {
"term" : {"_parent.country" : "us"}
}
}
}
}
';

curl -XGET 'http://localhost:9200/test/book/_search' -d '{
"explain" : true,
"query" : {
"text" : {"title" : "book"}
},
"filter" : {
"term": {"author.country" : "us"}
}
}
';

Sample data

curl -XPUT 'http://localhost:9200/test/author/1' -d '{
"name" : "author 1",
"country" : "us"
}'
curl -XPUT 'http://localhost:9200/test/author/2' -d '{
"name" : "author 2",
"country" : "uk"
}'
curl -XPUT 'http://localhost:9200/test/book/11?parent=1' -d '
{
"title": "book 1 author 1"
}'
curl -XPUT 'http://localhost:9200/test/book/12?parent=1' -d '
{
"title": "book 2 author 1"
}'
curl -XPUT 'http://localhost:9200/test/book/21?parent=2' -d '
{
"title": "book 3 author 2"
}'

I'm so sorry, I totally misunderstood your question. I thought you
were talking about nested documents, not parent/child types.

Now that I got it, I tried to find a solution, but failed miserably. I
guess I would just use nested documents if I would need such
functionality.

Apparently, you can refer to the parent's ID from within the child:
http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-td2166479.html

I just tried that and I couldn't make it work, but I guess it's just
my lack of knowledge. So, provided that you could make it work, you
can search for the authors in "country":"us" or whatever, then get the
IDs. Then search for books by adding an OR filter (or a bool query)
for the parent's IDs that you just found.

No, there isn't a way to search based on parent data and get back children, unless you embed the parent data in the children...

On Sunday, February 5, 2012 at 10:40 PM, Igor Semenko wrote:

Hello, I'm exploring Parent-Child feature in elasticsearch, but can't understand whether it's possible to find child by parent's field name? Sort of reverse 'has_child' query.

Here is example:

Author (parent)
-name
-country

Book (child of Author)

  • title
    ...

Can I search for all books author of which is from country:US for example?

Thanks.

HI,

I have a question about ES and Parent/Child :

is it possible to have nested has_child filter (or query) ? now, we are
trying to post a query with 2 levels of child (parent/child/grandchild).

example of mapping :
{
"settings":{
"analysis":{
"analyzer":{
"key_lowercase":{
"tokenizer":"keyword",
"filter":"lowercase"
}
}
},
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
},
"mappings":{
"type0":{
"properties":{...}
},
"type1":{
"_parent":{
"type":"type0"
},
"properties":{...}
}
"type2":{
"_parent":{
"type":"type1"
},
"properties":{...}
}
}
}

*example of query :
*
{
"query" : {
"filtered" : {
"query" : {
"bool" : {
"must" : [{...}, {...}]
}
},
"filter" : {
"has_child" : {
"type" : "type1",
"query" : {
"filtered" : {
"query" : {
"bool" : {
"must" : [{...}]
}
},
"filter" : {
"has_child" : {
"type" : "type2",
"query" : {
"term" : {...}
}
}
}
}
}
}
}
}
}
}

Thanks.

Hi Shay. Is this still the case?

On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:

No, there isn't a way to search based on parent data and get back
children, unless you embed the parent data in the children...

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Are you looking for the has_parent query/filter

http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter/
http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-query/

Thanks,
Matt Weber

On Tue, Jul 23, 2013 at 8:24 AM, Nick Hoffman nick@deadorange.com wrote:

Hi Shay. Is this still the case?

On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:

No, there isn't a way to search based on parent data and get back
children, unless you embed the parent data in the children...

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Indeed, I am. Thanks, Matt!

On Tuesday, 23 July 2013 11:48:10 UTC-4, Matt Weber wrote:

Are you looking for the has_parent query/filter

http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter/
http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-query/

Thanks,
Matt Weber

On Tue, Jul 23, 2013 at 8:24 AM, Nick Hoffman <ni...@deadorange.com<javascript:>

wrote:

Hi Shay. Is this still the case?

On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:

No, there isn't a way to search based on parent data and get back
children, unless you embed the parent data in the children...

--
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:>.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.