1

I went through the following links before pasting the ques

Elasticsearch has_child returning no results

ElasticSearch 7.3 has_parent/has_child don't return any hits

ES documentation

I created a simple mapping with text_doc as the parent and flag_doc as the child.

{
  "doc_index_ap3" : {
    "mappings" : {
      "properties" : {
        "domain" : {
          "type" : "keyword"
        },
        "email_text" : {
          "type" : "text"
        },
        "id" : {
          "type" : "keyword"
        },
        "my_join_field" : {
          "type" : "join",
          "eager_global_ordinals" : true,
          "relations" : {
            "text_doc" : "flag_doc"
          }
        }
      }
    }
  }
}

The query with parent_id works fine & returns 1 doc as expected

GET doc_index_ap3/_search
{
  "query": {
      "parent_id": {
        "type": "flag_doc",
        "id":"f0d2cb3c-bf4b-11eb-9f67-93a282921115"
      }
  }
}

But none of the below queries return any results.

GET doc_index_ap3/_search
{
  "query": {
    "has_parent": {
      "parent_type": "text_doc",
      "query": {
        "match_all": {
        }
      }
    }
  }
}

GET doc_index_ap3/_search
{
  "query": {
    "has_child": {
      "type": "flag_doc",
      "query": {
        "match_all": {}
      }
    }
  }
}
A_G
  • 2,260
  • 3
  • 23
  • 56

1 Answers1

1

There must be some issue in the way you have indexed the parent and child documents. Refer to this official documentation, to know more about parent-child relationship

Adding a working example using the same index mapping as given in the question above

Parent document in the text_doc context

PUT /index-name/_doc/1

{
  "domain": "ab",
  "email_text": "ab",
  "id": "ab",
  "my_join_field": {
    "name": "text_doc"
  }
}

Child document

PUT /index-name/_doc/2?routing=1&refresh



 {
  "domain": "cs",
  "email_text": "cs",
  "id": "cs",
  "my_join_field": {
    "name": "flag_doc",
    "parent": "1"
  }
}

Search Query:

{
  "query": {
    "has_parent": {
      "parent_type": "text_doc",
      "query": {
        "match_all": {
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67731507",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_routing": "1",
        "_source": {
          "domain": "cs",
          "email_text": "cs",
          "id": "cs",
          "my_join_field": {
            "name": "flag_doc",
            "parent": "1"
          }
        }
      }
    ]

Search Query:

{
  "query": {
    "has_child": {
      "type": "flag_doc",
      "query": {
        "match_all": {}
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67731507",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "domain": "ab",
          "email_text": "ab",
          "id": "ab",
          "my_join_field": {
            "name": "text_doc"
          }
        }
      }
    ]
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • Hey! Yeah, I am using a python script to index the docs. The structure of parent doc is ` parent_doc = { "id":"1", "email_text":"", 'my_join_field': {"name":"text_doc"} } ` And that of child doc ` child_doc = { "id":"child_1", "domain":"abc.com", "routing":"", 'my_join_field': { "name":"flag_doc", "parent":"1" } } ` – A_G May 28 '21 at 18:34
  • I populate `routing` with parent's `id` – A_G May 28 '21 at 18:37
  • Oh actually, I just noticed that in your query for indexing child doc you have used `"parent": "1"` which is the `_id` of parent doc and not `id`. Let me update my script & try again – A_G May 28 '21 at 19:32