# Is it possible to make a query based on another query

**URL:** https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596
**Category:** Elasticsearch
**Created:** [November 7, 2012, 8:48am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596 "2012-11-07T08:48:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Eugene\_Nikolaev](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@Eugene\_Nikolaev](https://discuss.elastic.co/u/Eugene_Nikolaev)
#### Post date: [November 7, 2012, 8:48am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/1 "2012-11-07T08:48:39Z")

</div>

Hi! Is it possible in elastic to make one request with two linked queries -  
so a first one returns some values and second one uses these values as  
parameters? For example - search for users based on some input parameters  
and current user's settings. Settings could be very large and they'd better  
stay in elastic than be specified in each request body. Is it possible?

--

---

<div class="post-metadata">

### Author: ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)
#### Post date: [November 7, 2012, 10:34am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/2 "2012-11-07T10:34:33Z")

</div>

Hi Eugene,

So what you're hoping to avoid is returning the user settings from the  
first query only to send have to send them again as a second query?  
Unfortunately Elasticsearch doesn't provide functionality for this (I was  
thinking maybe using scripting however it doesnt provide the necessary API).

If you're able to provide a little more information we might be able to see  
where optimizations can be made.

On Wednesday, November 7, 2012 7:48:39 PM UTC+11, Eugene Nikolaev wrote:

> Hi! Is it possible in elastic to make one request with two linked queries
> 
> - so a first one returns some values and second one uses these values as  
> parameters? For example - search for users based on some input parameters  
> and current user's settings. Settings could be very large and they'd better  
> stay in elastic than be specified in each request body. Is it possible?

--

---

<div class="post-metadata">

### Author: ![Eugene\_Nikolaev](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@Eugene\_Nikolaev](https://discuss.elastic.co/u/Eugene_Nikolaev)
#### Post date: [November 7, 2012, 11:54am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/3 "2012-11-07T11:54:41Z")

</div>

Thanks for your reply! I am hoping to avoid some network traffic, because  
user settings could include for example array of identies of rejected  
users. So if a user performs search for possible friends, these rejected  
users should not be included in the search result. A user can reject as  
many users as he or she wants. Say, 100-1000 ids. And I was hoping ther was  
a way to avoid this traffic overhead - first query loads a user, then  
second query fetches users using some input parameters and the array of the  
rejected user ids from first query result.

--

---

<div class="post-metadata">

### Author: ![otisg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otisg/32/492_2.png) [@otisg](https://discuss.elastic.co/u/otisg)
#### Post date: [November 7, 2012, 7:39pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/4 "2012-11-07T19:39:06Z")

</div>

Chris,

Could one not write a custom request action for this sort of stuff?  
This is not currently pluggable, so one would have to modify and rebuild  
ES, but it could be done, no?

## Thanks, Otis

Search Analytics - [Cloud Monitoring Tools & Services | Sematext](http://sematext.com/search-analytics/index.html)  
Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)

On Wednesday, November 7, 2012 5:34:33 AM UTC-5, Chris Male wrote:

> Hi Eugene,
> 
> So what you're hoping to avoid is returning the user settings from the  
> first query only to send have to send them again as a second query?  
> Unfortunately Elasticsearch doesn't provide functionality for this (I was  
> thinking maybe using scripting however it doesnt provide the necessary API).
> 
> If you're able to provide a little more information we might be able to  
> see where optimizations can be made.
> 
> On Wednesday, November 7, 2012 7:48:39 PM UTC+11, Eugene Nikolaev wrote:
> 
> > Hi! Is it possible in elastic to make one request with two linked queries
> > 
> > - so a first one returns some values and second one uses these values as  
> > parameters? For example - search for users based on some input parameters  
> > and current user's settings. Settings could be very large and they'd better  
> > stay in elastic than be specified in each request body. Is it possible?

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [November 8, 2012, 2:23am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/5 "2012-11-08T02:23:02Z")

</div>

Otis,

I think it can be done as a plugin. It's quite different from search. So it  
would make sense to implement it as a new request. As a result it wouldn't  
have to intersept anything. It would be somewhat similar to More Like This  
API [http://www.elasticsearch.org/guide/reference/api/more-like-this.html](http://www.elasticsearch.org/guide/reference/api/more-like-this.html)but instead of creating query based on a document content, it would build a  
query based on query template and results of another query. I just don't  
see how to formulate such request in such a way that it would  
be consistent with other es requests, easy to understand and flexible. It  
looks like someone would have to design some sort of a templating language  
to make such plugin really useful.

On Wednesday, November 7, 2012 2:39:06 PM UTC-5, Otis Gospodnetic wrote:

> Chris,
> 
> Could one not write a custom request action for this sort of stuff?  
> This is not currently pluggable, so one would have to modify and rebuild  
> ES, but it could be done, no?
> 
> ## Thanks, Otis
> 
> Search Analytics - [Cloud Monitoring Tools & Services | Sematext](http://sematext.com/search-analytics/index.html)  
> Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)
> 
> On Wednesday, November 7, 2012 5:34:33 AM UTC-5, Chris Male wrote:
> 
> > Hi Eugene,
> > 
> > So what you're hoping to avoid is returning the user settings from the  
> > first query only to send have to send them again as a second query?  
> > Unfortunately Elasticsearch doesn't provide functionality for this (I was  
> > thinking maybe using scripting however it doesnt provide the necessary API).
> > 
> > If you're able to provide a little more information we might be able to  
> > see where optimizations can be made.
> > 
> > On Wednesday, November 7, 2012 7:48:39 PM UTC+11, Eugene Nikolaev wrote:
> > 
> > > Hi! Is it possible in elastic to make one request with two linked  
> > > queries - so a first one returns some values and second one uses these  
> > > values as parameters? For example - search for users based on some input  
> > > parameters and current user's settings. Settings could be very large and  
> > > they'd better stay in elastic than be specified in each request body. Is it  
> > > possible?

--

---

<div class="post-metadata">

### Author: ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)
#### Post date: [November 8, 2012, 3:42am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/6 "2012-11-08T03:42:15Z")

</div>

I agree with Igor, I think it could definitely be done via a plugin.

One way for a generic solution would be to allow a script to be used to  
build the request based on the results of a previous request.

On Thursday, November 8, 2012 1:23:02 PM UTC+11, Igor Motov wrote:

> Otis,
> 
> I think it can be done as a plugin. It's quite different from search. So  
> it would make sense to implement it as a new request. As a result it  
> wouldn't have to intersept anything. It would be somewhat similar to More  
> Like This API[http://www.elasticsearch.org/guide/reference/api/more-like-this.html](http://www.elasticsearch.org/guide/reference/api/more-like-this.html)but instead of creating query based on a document content, it would build a  
> query based on query template and results of another query. I just don't  
> see how to formulate such request in such a way that it would  
> be consistent with other es requests, easy to understand and flexible. It  
> looks like someone would have to design some sort of a templating language  
> to make such plugin really useful.
> 
> On Wednesday, November 7, 2012 2:39:06 PM UTC-5, Otis Gospodnetic wrote:
> 
> > Chris,
> > 
> > Could one not write a custom request action for this sort of stuff?  
> > This is not currently pluggable, so one would have to modify and rebuild  
> > ES, but it could be done, no?
> > 
> > ## Thanks, Otis
> > 
> > Search Analytics - [Cloud Monitoring Tools & Services | Sematext](http://sematext.com/search-analytics/index.html)  
> > Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)
> > 
> > On Wednesday, November 7, 2012 5:34:33 AM UTC-5, Chris Male wrote:
> > 
> > > Hi Eugene,
> > > 
> > > So what you're hoping to avoid is returning the user settings from the  
> > > first query only to send have to send them again as a second query?  
> > > Unfortunately Elasticsearch doesn't provide functionality for this (I was  
> > > thinking maybe using scripting however it doesnt provide the necessary API).
> > > 
> > > If you're able to provide a little more information we might be able to  
> > > see where optimizations can be made.
> > > 
> > > On Wednesday, November 7, 2012 7:48:39 PM UTC+11, Eugene Nikolaev wrote:
> > > 
> > > > Hi! Is it possible in elastic to make one request with two linked  
> > > > queries - so a first one returns some values and second one uses these  
> > > > values as parameters? For example - search for users based on some input  
> > > > parameters and current user's settings. Settings could be very large and  
> > > > they'd better stay in elastic than be specified in each request body. Is it  
> > > > possible?

--

---

<div class="post-metadata">

### Author: ![Eugene\_Nikolaev](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@Eugene\_Nikolaev](https://discuss.elastic.co/u/Eugene_Nikolaev)
#### Post date: [November 8, 2012, 5:47am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/7 "2012-11-08T05:47:26Z")

</div>

Thank you all for your replies! We have guessed how to make a workaround  
for this. We just have to inverse rejected users -\> user relation and save  
ids of users who has ignored a user in his or her entry. So we only need to  
pass a current user id to exclude users who was ignored by him or her from  
search result. I think some scripting will be enough. Thanks again!

--

---

<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, 3:05am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-a-query-based-on-another-query/9596/8 "2017-07-06T03:05:30Z")

</div>


