# Query to get all the children

**URL:** <https://discuss.elastic.co/t/query-to-get-all-the-children/368932>\
**Category:** Elasticsearch\
**Created:** [October 16, 2024, 4:08pm UTC](https://discuss.elastic.co/t/query-to-get-all-the-children/368932 "2024-10-16T16:08:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yulinxp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yulinxp/32/138393_2.png) [@yulinxp](https://discuss.elastic.co/u/yulinxp)\
**Post date:** [October 16, 2024, 4:08pm UTC](https://discuss.elastic.co/t/query-to-get-all-the-children/368932/1 "2024-10-16T16:08:20Z")

</div>

Here is mapping

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "my_join_field": {
        "type": "join",
        "relations": {
          "parent": "child"
        }
      },
      "name": { "type": "keyword" },
      "age": { "type": "integer" },
      "hobby": { "type": "keyword" }
    }
  }
}

```

I want to get all the children.

```auto
GET my_index/_search
{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "query": {
        "match_all": {}
      }
    }
  }
}

```

It returns all the children in below response format

```auto
{
        "_index": "my_index",
        "_id": "child1",
        "_score": 1,
        "_routing": "parent1",
        "_source": {
          "name": "Alice Doe",
          "age": 10,
          "hobby": "reading",
          "my_join_field": {
            "name": "child",
            "parent": "parent1"
          }
        }
      }

```

I run a different query

```auto
GET my_index/_search
{
  "query": {
    "term": {
      "my_join_field": "child"
    }
  }
}

```

It returns all the children in below response format. Notice the score is different than previous result

```auto
{
        "_index": "my_index",
        "_id": "child1",
        "_score": 0.44183272,
        "_routing": "parent1",
        "_source": {
          "name": "Alice Doe",
          "age": 10,
          "hobby": "reading",
          "my_join_field": {
            "name": "child",
            "parent": "parent1"
          }
        }
      }

```

Is the 2nd query still correct? Why score is different.

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [October 17, 2024, 11:36am UTC](https://discuss.elastic.co/t/query-to-get-all-the-children/368932/2 "2024-10-17T11:36:35Z")

</div>

Hi @yulinxp :

Scoring will be different in the two queries as they are different queries.

- The first is a [`match_all`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html) query, which has a constant score of 1.
- The second is a `term query`, which actually calculates a score based on the term frequency and document frequency.

Your queries seem correct, but as they are different, different score is returned by them.

If you just want to retrieve your parents / children and not to actually perform scoring depending on a text query, you should use a [filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html) or wrap your term query in a [constant score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) so it's more efficient (as it won't calculate scores that way).

Hope that helps!

---

<div class="post-metadata">

**Author:** ![yulinxp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yulinxp/32/138393_2.png) [@yulinxp](https://discuss.elastic.co/u/yulinxp)\
**Post date:** [October 17, 2024, 1:38pm UTC](https://discuss.elastic.co/t/query-to-get-all-the-children/368932/3 "2024-10-17T13:38:43Z")

</div>

Thank you!
