# Subquery?

**URL:** https://discuss.elastic.co/t/subquery/107376
**Category:** Elasticsearch
**Created:** [November 13, 2017, 11:37am UTC](https://discuss.elastic.co/t/subquery/107376 "2017-11-13T11:37:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mark-wade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark-wade/32/24130_2.png) [@mark-wade](https://discuss.elastic.co/u/mark-wade)
#### Post date: [November 13, 2017, 11:37am UTC](https://discuss.elastic.co/t/subquery/107376/1 "2017-11-13T11:37:43Z")

</div>

I have an index which contains posts made by a user:

```
{
    "user": 1,
    "thread": 1,
    "content": "This is the first post in a thread",
},
{
    "user": 2,
    "thread": 1,
    "content": "This is the second post in a thread",
},
{
    "user": 1,
    "thread": 2,
    "content": "This is a post in a different thread",
}

```

I want to construct a query with a filter to only return comments in a thread that the user has posted in (not necessarily comments the user themselves posted) - i.e. in SQL, it would be something like:

```
SELECT * FROM comments WHERE thread IN ( SELECT thread FROM comments WHERE user=1 )

```

From what I can see my options are:

- Index a list of every thread ID a user has posted in and use the "Terms lookup mechanism"
- Somehow create a parent-child relationship between the various comments and use a has\_parent/has\_child query... I'm not sure if this is possible though with the structure I have

Is there any other way I'm missing, or does anyone have any advice on what the best way to achieve what I'm looking for would be?

---

<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: [December 11, 2017, 11:37am UTC](https://discuss.elastic.co/t/subquery/107376/2 "2017-12-11T11:37:50Z")

</div>

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