# Elastic Search

**URL:** <https://discuss.elastic.co/t/elastic-search/343614>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql, docker\
**Created:** [September 22, 2023, 10:59am UTC](https://discuss.elastic.co/t/elastic-search/343614 "2023-09-22T10:59:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopal\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_gupta/32/123130_2.png) [@Gopal\_Gupta](https://discuss.elastic.co/u/Gopal_Gupta)\
**Post date:** [September 22, 2023, 10:59am UTC](https://discuss.elastic.co/t/elastic-search/343614/1 "2023-09-22T10:59:52Z")

</div>

PROBLEM STATEMENT

I am using Join field type

Sample Mapping

```auto
 {
  "mappings": {
    "properties": {
      "firstname": {
        "type": "keyword"
      },
"lastname": {
        "type": "keyword"
      },
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}

```

I have a scenario where i want to search based on first name and include all documents (question and answers)  
Also if a question is in the search result i want to include its answers also.  
Result set should have unique values

My Search Query is

```auto
{
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "prefix": {
                        "firstName": "J"
                    }
                }
            ]
        }
    },
    "size": 2,
    "sort": [
        {
            "lastName": {
                "order": "desc"
            }
        },
        {
            "firstName": {
                "order": "asc"
            }
        }
          ]
}

```

Problem: How to get exact matched (including child) hit count.  
The from and size does not work for child, also sort is not working

Sample Data:-

```auto
curl --location 'members/_doc/1' \
--header 'Content-Type: application/json' \
--data '{
  
   "firstname": "John",
  "lastname": "Doe",
  "relationship": {
    "name": "question"
  }
}'

curl -X POST "members/_doc/2?routing=1"
{
  "firstname": "Jane",
  "lastname": "Doe",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}

curl -X POST "members/_doc/3?routing=1"
{
  "firstname": "Joe",
  "lastname": "XYZ",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}

curl -X POST members/_doc/4?routing=1"
{
  "firstname": "Ken",
  "lastname": "Blanton",
  "relationship": {
    "name": "answer",
    "parent": "1" 
  }
}

```

---

<div class="post-metadata">

**Author:** ![Gopal\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_gupta/32/123130_2.png) [@Gopal\_Gupta](https://discuss.elastic.co/u/Gopal_Gupta)\
**Post date:** [October 9, 2023, 7:00am UTC](https://discuss.elastic.co/t/elastic-search/343614/2 "2023-10-09T07:00:24Z")

</div>

Any Update on this?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 9, 2023, 7:15am UTC](https://discuss.elastic.co/t/elastic-search/343614/3 "2023-10-09T07:15:17Z")

</div>

Welcome!

You can look at

> **[Has child query | Elasticsearch Guide \[8.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.10/query-dsl-has-child-query.html)**

and

> **[Retrieve inner hits | Elasticsearch Guide \[8.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.10/inner-hits.html)**

That should help.

But depending on your use case, it might be worth to index the question and all the answers in a single document...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 6, 2023, 7:15am UTC](https://discuss.elastic.co/t/elastic-search/343614/4 "2023-11-06T07:15:44Z")

</div>

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