Queries on has_parent / has_child don't return any hits

I have a very simple join relationship that has been defined as :
es.indices.create( index= "docpage", body=

{
     "mappings": {
            "properties": {
                 "my_document": {
                    "type": "text"
                 },
                "my_page": {
                    "type": "text"
                 },
                "my_join_field": { 
                   "type": "join",
                   "relations": {
                     "my_document": "my_page" 
                   }
                }
            }
          }
    }

)

I am indexing a parent document using
es_d = es.index(index="docpage",doc_type="_doc",id = 1, body={"text": "I am the parent", "my_join_field": "my_document"})

I am indexing child documents with
pages[0]='I am child 1'
pages[1]='I am child 2'
pages[2]='I am child 3'
pages[3]='I am child 4'
pages[4]='I am child 5'
for page in pages:

 res_c = es.index(index="docpage",doc_type="_doc",routing=1,body=

       {
           "text": page,
           "my-join-field": {
                "name": "my_page",
                "parent": "1"
            }
        }) 

When I look for all entries in the index using
res_doc = es.search(index="docpage", body=
{

    "query" : {       
        "match_all": {}
    }

})
I see the document and 5 pages in the index.
{'took': 3, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 6, 'relation': 'eq'}, 'max_score': 1.0, 'hits': [{'_index': 'docpage', '_type': '_doc', '_id': '1', '_score': 1.0, '_source': {'text': 'I am the parent', 'my_join_field': 'my_document'}}, {'_index': 'docpage', '_type': '_doc', '_id': 'QGxe0m4BLD2e5CPzVs6X', '_score': 1.0, '_routing': '1', '_source': {'text': 'I am child 1', 'my-join-field': {'name': 'my_page', 'parent': '1'}}}, {'_index': 'docpage', '_type': '_doc', '_id': 'QWxe0m4BLD2e5CPzVs79', '_score': 1.0, '_routing': '1', '_source': {'text': 'I am child 2', 'my-join-field': {'name': 'my_page', 'parent': '1'}}}, {'_index': 'docpage', '_type': '_doc', '_id': 'Qmxe0m4BLD2e5CPzV84L', '_score': 1.0, '_routing': '1', '_source': {'text': 'I am child 3', 'my-join-field': {'name': 'my_page', 'parent': '1'}}}, {'_index': 'docpage', '_type': '_doc', '_id': 'Q2xe0m4BLD2e5CPzV84X', '_score': 1.0, '_routing': '1', '_source': {'text': 'I am child 4', 'my-join-field': {'name': 'my_page', 'parent': '1'}}}, {'_index': 'docpage', '_type': '_doc', '_id': 'RGxe0m4BLD2e5CPzV84m', '_score': 1.0, '_routing': '1', '_source': {'text': 'I am child 5', 'my-join-field': {'name': 'my_page', 'parent': '1'}}}]}}

But when I try to use has_parent or has_child, I get no hits. This basic query doesn't return any hits.
res_doc = es.search(index="docpage", body=
{

   "query" : {

        "has_parent" : {         
            "parent_type": "my_document",
            "query": {
                "match": {
                   "id": "1"
                 }
             }
        } 
    }

}
)

Any ideas will be appreciated.
Thanks.

not sure your query is the right one, as there is no field named id in that document from what I have seen in the snippets?

Thanks for your reply. Yes, it should be _id and not id.
I tried "_id": "1" but it didn't work.

The weird thing is that an equivalent curl sequence of commands seems to work.

Narrowed it down to an issue with invoking es.index for each page.
When I do
!curl -XPOST 'localhost:9200/docpage/_doc/?routing=1' -H 'Content-Type: application/json' -d '{
"text": "I am child 1",
"my_join_field": {
"name": "my_page",
"parent": "1"
}
}'
I can see this page in the has_parent query(see above in thread).
When I do
res_c = es.index(index="docpage",doc_type="_doc",routing=1,
body={ "text": "I am child 2",
"my-join-field": {
"name": "my_page",
"parent": 1
}
}
)
I don't see this page in has_parent query.

hm. I am just speculating here, but this could mean, that the routing parameter is not applied properly?

when you execute a search (one that finds documents) and you add explain: true to it, you can see what shard the document is in, that might help to rule this out.

Thanks for your reply. It turned out to be a typo : I was using my-join-field while I had created the parent-child mapping using the name my_join_field !

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.