# Distinct queries across multiple types in one request?

**URL:** <https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474>\
**Category:** Elasticsearch\
**Created:** [January 24, 2012, 12:01am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474 "2012-01-24T00:01:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![qjh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qjh/32/2420_2.png) [@qjh](https://discuss.elastic.co/u/qjh)\
**Post date:** [January 24, 2012, 12:01am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/1 "2012-01-24T00:01:48Z")

</div>

Is it possible to issue distinct queries to multiple types within the  
same index? I have a few types with different mappings (some overlap)  
and I need to query some of the fields of each with the same query  
string, but the queries are inherently different because of the  
mappings. It seems like it is only possible to execute the same query  
against multiple types. If that is the case, how would you guys feel  
about extending the API to support multiple distinct queries?

Here is a gist with the current queries and below that is a rough idea  
of how the multiple queries could work.

> <https://gist.github.com/qhoxie/bcdeb73fb9b52b0f7564>

---

<div class="post-metadata">

**Author:** ![Mark\_Waddle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_waddle/32/2608_2.png) [@Mark\_Waddle](https://discuss.elastic.co/u/Mark_Waddle)\
**Post date:** [January 24, 2012, 4:05am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/2 "2012-01-24T04:05:04Z")

</div>

Could you search at the index level like your gist, but include the "\_type"  
field in each query, and combine the queries with an OR?

---

<div class="post-metadata">

**Author:** ![qjh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qjh/32/2420_2.png) [@qjh](https://discuss.elastic.co/u/qjh)\
**Post date:** [January 24, 2012, 4:14am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/3 "2012-01-24T04:14:21Z")

</div>

That is probably is fine query, but it would give me all the results  
mixed and scored together. I need to them to be separated based on  
the type in the results.

Thanks for the reply, though!

---

<div class="post-metadata">

**Author:** ![qjh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qjh/32/2420_2.png) [@qjh](https://discuss.elastic.co/u/qjh)\
**Post date:** [February 1, 2012, 11:02am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/4 "2012-02-01T11:02:21Z")

</div>

Anyone else have thoughts on this? Is there a way to accomplish it  
presently? Does it seem like a reasonable API to implement?

Thanks!

---

<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:** [February 1, 2012, 11:14am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/5 "2012-02-01T11:14:15Z")

</div>

There is no way to get distinct results per type with different queries except if you execute several search requests per type.

On Wednesday, February 1, 2012 at 1:02 PM, qjh wrote:

> Anyone else have thoughts on this? Is there a way to accomplish it  
> presently? Does it seem like a reasonable API to implement?
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![qjh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qjh/32/2420_2.png) [@qjh](https://discuss.elastic.co/u/qjh)\
**Post date:** [February 1, 2012, 11:20am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/6 "2012-02-01T11:20:39Z")

</div>

Thanks for the reply, Shay. Is that something you think is reasonable  
to implement in ES as opposed to in the client? I would picture it  
being similar to the bulk indexing/delete API but with queries, as  
demonstrated in the gist I posted.

---

<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:** [February 1, 2012, 11:24am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/7 "2012-02-01T11:24:36Z")

</div>

Effectively what you are after is "multi search" API, its definitely possible, but not high on the list (at least for now…) since you can execute several search requests, and its less acute compared to multi get / batch indexing.

On Tuesday, January 24, 2012 at 2:01 AM, qjh wrote:

> Is it possible to issue distinct queries to multiple types within the  
> same index? I have a few types with different mappings (some overlap)  
> and I need to query some of the fields of each with the same query  
> string, but the queries are inherently different because of the  
> mappings. It seems like it is only possible to execute the same query  
> against multiple types. If that is the case, how would you guys feel  
> about extending the API to support multiple distinct queries?
> 
> Here is a gist with the current queries and below that is a rough idea  
> of how the multiple queries could work.
> 
> [currently separate · GitHub](https://gist.github.com/bcdeb73fb9b52b0f7564)

---

<div class="post-metadata">

**Author:** ![Roly\_Vicaria](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roly_vicaria/32/506_2.png) [@Roly\_Vicaria](https://discuss.elastic.co/u/Roly_Vicaria)\
**Post date:** [June 1, 2012, 3:28pm UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/8 "2012-06-01T15:28:05Z")

</div>

What would this kind of query look like?

On Monday, January 23, 2012 11:05:04 PM UTC-5, Mark Waddle wrote:

> Could you search at the index level like your gist, but include the  
> "\_type" field in each query, and combine the queries with an OR?

---

<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:26am UTC](https://discuss.elastic.co/t/distinct-queries-across-multiple-types-in-one-request/6474/9 "2017-07-06T03:26:02Z")

</div>


