# Search in both parent and child

**URL:** <https://discuss.elastic.co/t/search-in-both-parent-and-child/83015>\
**Category:** Elasticsearch\
**Created:** [April 20, 2017, 8:51am UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015 "2017-04-20T08:51:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jul](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jul](https://discuss.elastic.co/u/Jul)\
**Post date:** [April 20, 2017, 8:51am UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015/1 "2017-04-20T08:51:51Z")

</div>

Hey,  
I'm looking for a solution for the following problem, maybe someone can help me:

I'd like to index documents with a parent-child relationship and be able to search for parent-documents where the search string (provided by users, it could be something like "car sharing") can be only in the parent, only in the child or spreaded in parent and child.

The only solution I could think of so far is splitting the search string and creating a query in which every single term must be in either parent or child. This could result in quiet complex queries, depending on the user input (e.g. "(car AND (sharing OR dealer)").

Is there a way to do this without splitting the search string before and creating a complex query?

There is normaly only one parent and one child document but frequent updates to the child document (that's also the reason for splitting it in parent and child).

Thanks in advance,  
Jul

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 20, 2017, 3:39pm UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015/2 "2017-04-20T15:39:02Z")

</div>

Why do you need to split the search string first? You could do something that looks like:

```auto
{
  "query": {
    "bool": {
      "should": [
        {
           // your query on children goes here
         },
        {
          "has_child": {
            "type" : "your_child_type",
            // your query on children goes here again
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 20, 2017, 3:40pm UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015/3 "2017-04-20T15:40:42Z")

</div>

Oh maybe I get why now, if your query is `foo AND bar`, you want to match if `foo` can be found in the parent and `bar` in a child? If yes, then this is more complicated indeed. My advice would be to not do it and instead look into whether attributes of the parent can be copied in the child documents to avoid having to do a join for every token in the query string.

---

<div class="post-metadata">

**Author:** ![Jul](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jul](https://discuss.elastic.co/u/Jul)\
**Post date:** [April 21, 2017, 9:23am UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015/4 "2017-04-21T09:23:31Z")

</div>

I'd like to seperate a document into two as part of the document is updated quite frequently. The part which is not updated frequently takes a lot of time to analyze, so I'd like to avoid updating it if not neccessary (this also rules out nested objects).

Any other recomendations?

Thanks!

---

<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:** [May 19, 2017, 9:34am UTC](https://discuss.elastic.co/t/search-in-both-parent-and-child/83015/5 "2017-05-19T09:34:25Z")

</div>

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