title: Elastic search through multiple fields of 1-N related data in one index
body: <p>I've been stuck for a couple of days now with this issue, reading the Elastic docs back and forth to no avail. I hope someone could push me the right way.</p>
  
  <p>Suppose there are two types of data - a parent and a child, that have 1-N relationship. I store them in the same index, using <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html" rel="nofollow noreferrer">custom <code>type</code> field</a>. They have some common and some different fields. To give a concrete example, the index and some input data might look something like:</p>
  
  <pre><code>PUT /myindex
  {
    "mappings":
    {
      "properties": {
        "type": {"type": "keyword"},
        "fileid" : {"type": "keyword"},
        "dirid": {"type": "keyword"},
        "filename": {"type": "text"},
        "fileauthor": {"type": "text"},
        "dirpath": {"type": "text"}
      }
    }
  }
  PUT /myindex/_doc/1
  {
    "type": "dir",
    "dirid": 1,
    "dirpath": "/home/jd/"
  }
  PUT /myindex/_doc/2
  {
    "type": "dir",
    "dirid": 2,
    "dirpath": "/home/jd/Documents/CV/"
  }
  PUT /myindex/_doc/3
  {
    "type": "file",
    "dirid": 2,
    "fileid": 1,
    "filename": "My-Resume.pdf",
    "fileauthor": "John Doe"
  }
  PUT /myindex/_doc/4
  {
    "type": "file",
    "dirid": 1,
    "fileid": 2,
    "filename": "Some-CV.pdf",
    "fileauthor": "John Doe"
  }
  PUT /myindex/_doc/5
  {
    "type": "file",
    "dirid": 1,
    "fileid": 3,
    "filename": "book.pdf",
    "fileauthor": "John Doe"
  }
  </code></pre>
  
  <p>Now I want to search the text fields using rather complex (multi-word) queries, such as <code>John Doe CV</code>. For individual <code>type="dir"</code> or <code>type="file"</code> it is straightforward (and the query would return document with <code>_id=4</code>), but I would also like to get "mixed" results where part of the query is contained in the parent-specific fields and the rest in the child-specific fields. In my example data and query, I'd like it to return also <code>_id=3</code>, since the author is <code>John Doe</code> and parent <code>dirpath</code> contains the keyword <code>CV</code>.</p>
  
  <p>I'm happy just to get the parent, it's enough for what I need. (I would indeed prefer just to get the parent, but that is something on top of the current problem I'm having.)</p>
  
  <p>Somehow I haven't found a way to do it. I am aware of the <code>join</code> type, but that does not seem to help here, rather on the contrary. Many suggestions, SO questions and documentation items are outdated as the previously recommended practice become deprecated.</p>
  
  <p>Sure, I could just populate each entry with all data (each <code>type="file"</code> would just copy the parent's <code>dirpath</code> values), but that is against all my perception of optimal use of memory and space.</p>
  
  <p>Does anyone know where to look further, please? Thanks a lot.</p>