0

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.

  • When I use an equivalent sequence of curl commands, I can see has_parent query working. I have narrowed it down to some issue with indexing pages using es.index(). If I use curl commands instead of es.index() , things work. – user8378880 Dec 06 '19 at 20:31
  • 1
    Turned out to be a typo in the name of the join relation. I used my_join_field when I specified the mapping and used my-join-field when indexing pages. – user8378880 Dec 10 '19 at 22:38

0 Answers0