# With changes to \_id field how do I retrieve the \_ids matching a query

**URL:** https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313
**Category:** Elasticsearch
**Created:** [April 28, 2011, 8:32pm UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313 "2011-04-28T20:32:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![byuva](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@byuva](https://discuss.elastic.co/u/byuva)
#### Post date: [April 28, 2011, 8:32pm UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313/1 "2011-04-28T20:32:08Z")

</div>

I often need to get the \_ids matching a query so I can retrieve the  
appropriate children (still waiting on that has\_parent query 🙂 I've  
found that faceting \_id is faster than iterating/scanning through the  
query results and extracting the \_id. The new changes to \_id seem to  
make this difficult. Am I missing an alternative way to do what seems  
to be a common task?

-brian

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 28, 2011, 8:37pm UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313/2 "2011-04-28T20:37:15Z")

</div>

You get the \_id for each document back in the search result, or am I missing something else?

Note, you can always enable indexing the \_id as well. But lets see if you really need to.  
On Thursday, April 28, 2011 at 11:32 PM, merrellb wrote:

> I often need to get the \_ids matching a query so I can retrieve the  
> appropriate children (still waiting on that has\_parent query 🙂 I've  
> found that faceting \_id is faster than iterating/scanning through the  
> query results and extracting the \_id. The new changes to \_id seem to  
> make this difficult. Am I missing an alternative way to do what seems  
> to be a common task?
> 
> -brian

---

<div class="post-metadata">

### Author: ![byuva](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@byuva](https://discuss.elastic.co/u/byuva)
#### Post date: [April 28, 2011, 9:21pm UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313/3 "2011-04-28T21:21:00Z")

</div>

Before SCAN I got in the habit of faceting \_id whenever I needed the  
\_ids from a query, it was much faster than iterating through the query  
results (I often have several hundred thousand records in my  
results). Using scan to extract the ids still seems a bit slower than  
faceting (I imagine because there is more overhead per item  
returned). However in the long run I realize I need to be careful  
with faceting because of the potential for excessive memory usage.

I suppose my question is, what is the best workflow for what (in my  
cases at least) is a common operation (ie performing queries on  
parents and joining them to their children). My current process is:

1. Iterate through a scan query on the parent making sure to set  
fields = 
2. Extract the id field from the results
3. For each \_id, perform a term query on the \_parent field to  
retrieve the proper child.

a) Is this the best approach using currently available methods?  
b) Any more though to having a simple "join" or "has\_parent" method?

Thanks

-brian

On Apr 28, 4:37 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> You get the \_id for each document back in the search result, or am I missing something else?
> 
> Note, you can always enable indexing the \_id as well. But lets see if you really need to.
> 
> On Thursday, April 28, 2011 at 11:32 PM, merrellb wrote:
> 
> > I often need to get the \_ids matching a query so I can retrieve the  
> > appropriate children (still waiting on that has\_parent query 🙂 I've  
> > found that faceting \_id is faster than iterating/scanning through the  
> > query results and extracting the \_id. The new changes to \_id seem to  
> > make this difficult. Am I missing an alternative way to do what seems  
> > to be a common task?
> 
> > -brian

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 29, 2011, 9:49pm UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313/4 "2011-04-29T21:49:33Z")

</div>

Hey,

I am still not sure how, based on what you specify, faceting on the id helps? What is the search query that you execute when you do the faceting?

When you say join, you mean that each document will also include the child documents, right? This is possible, just need to be slated to be implemented.  
On Friday, April 29, 2011 at 12:21 AM, merrellb wrote:

> Before SCAN I got in the habit of faceting \_id whenever I needed the  
> \_ids from a query, it was much faster than iterating through the query  
> results (I often have several hundred thousand records in my  
> results). Using scan to extract the ids still seems a bit slower than  
> faceting (I imagine because there is more overhead per item  
> returned). However in the long run I realize I need to be careful  
> with faceting because of the potential for excessive memory usage.
> 
> I suppose my question is, what is the best workflow for what (in my  
> cases at least) is a common operation (ie performing queries on  
> parents and joining them to their children). My current process is:
> 
> 1. Iterate through a scan query on the parent making sure to set  
> fields = 
> 2. Extract the id field from the results
> 3. For each \_id, perform a term query on the \_parent field to  
> retrieve the proper child.
> 
> a) Is this the best approach using currently available methods?  
> b) Any more though to having a simple "join" or "has\_parent" method?
> 
> Thanks
> 
> -brian
> 
> On Apr 28, 4:37 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > You get the \_id for each document back in the search result, or am I missing something else?
> > 
> > Note, you can always enable indexing the \_id as well. But lets see if you really need to.
> > 
> > On Thursday, April 28, 2011 at 11:32 PM, merrellb wrote:
> > 
> > > I often need to get the \_ids matching a query so I can retrieve the  
> > > appropriate children (still waiting on that has\_parent query 🙂 I've  
> > > found that faceting \_id is faster than iterating/scanning through the  
> > > query results and extracting the \_id. The new changes to \_id seem to  
> > > make this difficult. Am I missing an alternative way to do what seems  
> > > to be a common task?
> > 
> > > -brian

---

<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, 4:07am UTC](https://discuss.elastic.co/t/with-changes-to--id-field-how-do-i-retrieve-the--ids-matching-a-query/4313/5 "2017-07-06T04:07:20Z")

</div>


