# Subqueries :(

**URL:** <https://discuss.elastic.co/t/subqueries/74260>\
**Category:** Elasticsearch\
**Created:** [February 7, 2017, 6:06pm UTC](https://discuss.elastic.co/t/subqueries/74260 "2017-02-07T18:06:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlovett1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tlovett1/32/13950_2.png) [@tlovett1](https://discuss.elastic.co/u/tlovett1)\
**Post date:** [February 7, 2017, 6:06pm UTC](https://discuss.elastic.co/t/subqueries/74260/1 "2017-02-07T18:06:35Z")

</div>

I am using the ingest mapper plugin to index PDFs:

```
POST /index-name

{
  "attachment_ID": 1,
  "data": "binary-data......",
  "type": "attachment"
}

```

Obviously, the data field is ingested. I am storing only "content".

I then have another document like this:

```
POST /index-name

{
  "type": "blog-post",
  "attachments": [
     {
        "attachment_ID": 1
     }
  ]
}

```

I need to query the blog post type but have content of the associated attachments affect relevancy. So really I need to subquery the associated attachments to each blog-post. I guess this is a JOIN. I need to query attachments by themselves AS WELL AS blog posts with associated attachments affecting relevancy. My best idea right now is to ingest twice: once under the blog post and one in the attachment document.

Any good solutions to this problem?

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [February 7, 2017, 11:25pm UTC](https://discuss.elastic.co/t/subqueries/74260/2 "2017-02-07T23:25:27Z")

</div>

I solve this while indexing, with my reference plugin.

Scenario: when indexing a `blog-post` doc, which is referencing an attachment doc by ID, the plugin can lookup the `attachment` doc for extracting fields from that doc to get them indexed also into the `blog-post` doc.

See

> **[jprante/elasticsearch-analysis-reference](https://github.com/jprante/elasticsearch-analysis-reference)**
>
> elasticsearch-analysis-reference - A reference mechanism for including content from other documents during the Elasticsearch analysis field mapping phase

---

<div class="post-metadata">

**Author:** ![equake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/equake/32/15716_2.png) [@equake](https://discuss.elastic.co/u/equake)\
**Post date:** [February 20, 2017, 9:52pm UTC](https://discuss.elastic.co/t/subqueries/74260/3 "2017-02-20T21:52:49Z")

</div>

@jprante any plans to make your plugin working with ES 5.x? It seems to be nice 🙂

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [February 21, 2017, 8:53am UTC](https://discuss.elastic.co/t/subqueries/74260/4 "2017-02-21T08:53:08Z")

</div>

Definitely yes, a 5.1.1 preview is already in the bundle plugin [https://github.com/jprante/elasticsearch-plugin-bundle](https://github.com/jprante/elasticsearch-plugin-bundle) but I have to factor the code out, update to latest 5.2.1 and update the docs 🙂

---

<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:** [March 21, 2017, 8:53am UTC](https://discuss.elastic.co/t/subqueries/74260/5 "2017-03-21T08:53:11Z")

</div>

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