# Find parent object for a specific child

**URL:** <https://discuss.elastic.co/t/find-parent-object-for-a-specific-child/16103>\
**Category:** Elasticsearch\
**Created:** [March 3, 2014, 11:22am UTC](https://discuss.elastic.co/t/find-parent-object-for-a-specific-child/16103 "2014-03-03T11:22:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poornima\_Powar](https://avatars.discourse-cdn.com/v4/letter/p/858c86/32.png) [@Poornima\_Powar](https://discuss.elastic.co/u/Poornima_Powar)\
**Post date:** [March 3, 2014, 11:22am UTC](https://discuss.elastic.co/t/find-parent-object-for-a-specific-child/16103/1 "2014-03-03T11:22:41Z")

</div>

Sample data in an index is shown below –

[

{

```
                            PID =”PID1”,

                            …

                            CHILDREN = 

                            [

                                            {

                                                            CID=”CID1”

                                                            … 

                                                            FLAG = false

                                            },

                                            {

                                                            CID=”CID2”

                                                            … 

                                                            FLAG = true

                                            },

                                            …

                            ]

```

},

{

```
                            PID =”PID2”,

                            …

                            CHILDREN = 

                            [

                                            {

                                                            CID=”CID1”

                                                            … 

                                                            FLAG = true

                                            },

                                            {

                                                            CID=”CID2”

                                                            … 

                                                            FLAG = false

                                            },

                                            …

                            ]

```

},

…

]

_RULE:_ Each specific CID object will have FLAG = true in one and only one  
PID object; in other PID objects, FLAG = false may exist.

\*QUERY: \* Find PID object for which CID= and FLAG  
corresponding to CID object is set to true. For example, it shall be PID2  
object if CID1 is searched and it shall be PID1 obejct If CID2 is searched. _What  
is the best solution to solve it?_

_NOT A SOLUTION_

es\_client.prepareSearch(\<index\_name\>)

.setSearchType(SearchType.QUERY\_THEN\_FETCH)

.setQuery(

QueryBuilders.boolQuery()

```
                    .must(QueryBuilders.termQuery("CHILDREN.CID ", 

```

“CID1”))

```
                    .must(QueryBuilders.termQuery("CHILDREN .FLAG", 

```

true)))

```
             )

```

_ONE SOLUTION_

If we use only QueryBuilders.termQuery("CHILDREN.CID ", “CiD1”)) in  
setQuery, it returns all PID objects having CID = “CID1”. Parse the result  
and check which PID object is actually having CID1 object where FLAG=true. _Any  
better solution than this?_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [March 3, 2014, 12:13pm UTC](https://discuss.elastic.co/t/find-parent-object-for-a-specific-child/16103/2 "2014-03-03T12:13:47Z")

</div>

Did you look into the nested type[1] and nested query[2]? I think that  
would be a solution for you.

1:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

2:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On 3 March 2014 12:22, Poornima Powar [poornimapowar@gmail.com](mailto:poornimapowar@gmail.com) wrote:

> Sample data in an index is shown below –
> 
> [
> 
> {
> 
> ```
> PID =”PID1”,
> 
> …
> 
> CHILDREN =
> 
> [
> 
> {
> 
> CID=”CID1”
> 
> …
> 
> FLAG =
> 
> ```
> 
> false
> 
> ```
> },
> 
> {
> 
> CID=”CID2”
> 
> …
> 
> FLAG = true
> 
> },
> 
> …
> 
> ]
> 
> ```
> 
> },
> 
> {
> 
> ```
> PID =”PID2”,
> 
> …
> 
> CHILDREN =
> 
> [
> 
> {
> 
> CID=”CID1”
> 
> …
> 
> FLAG = true
> 
> },
> 
> {
> 
> CID=”CID2”
> 
> …
> 
> FLAG =
> 
> ```
> 
> false
> 
> ```
> },
> 
> …
> 
> ]
> 
> ```
> 
> },
> 
> …
> 
> ]
> 
> _RULE:_ Each specific CID object will have FLAG = true in one and only  
> one PID object; in other PID objects, FLAG = false may exist.
> 
> \*QUERY: \* Find PID object for which CID= and FLAG  
> corresponding to CID object is set to true. For example, it shall be PID2  
> object if CID1 is searched and it shall be PID1 obejct If CID2 is searched. _What  
> is the best solution to solve it?_
> 
> _NOT A SOLUTION_
> 
> es\_client.prepareSearch(\<index\_name\>)
> 
> .setSearchType(SearchType.QUERY\_THEN\_FETCH)
> 
> .setQuery(
> 
> QueryBuilders.boolQuery()
> 
> ```
> .must(QueryBuilders.termQuery("CHILDREN.CID ",
> 
> ```
> 
> “CID1”))
> 
> ```
> .must(QueryBuilders.termQuery("CHILDREN .FLAG",
> 
> ```
> 
> true)))
> 
> ```
> )
> 
> ```
> 
> _ONE SOLUTION_
> 
> If we use only QueryBuilders.termQuery("CHILDREN.CID ", “CiD1”)) in  
> setQuery, it returns all PID objects having CID = “CID1”. Parse the result  
> and check which PID object is actually having CID1 object where FLAG=true. _Any  
> better solution than this?_
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/582d2e01-50bb-45b8-8dfa-7ea6b28ece53%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tx3EkWmNbmZTBD%3DbssDYSbN7yAhxPAtJm1b9O9XqE6hPw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tx3EkWmNbmZTBD%3DbssDYSbN7yAhxPAtJm1b9O9XqE6hPw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 1:46am UTC](https://discuss.elastic.co/t/find-parent-object-for-a-specific-child/16103/3 "2017-07-06T01:46:13Z")

</div>

Closed on July 6, 2017, 1:46am UTC


