# Boolean queries across multiple types in an index

**URL:** <https://discuss.elastic.co/t/boolean-queries-across-multiple-types-in-an-index/9494>\
**Category:** Elasticsearch\
**Created:** [October 29, 2012, 3:23pm UTC](https://discuss.elastic.co/t/boolean-queries-across-multiple-types-in-an-index/9494 "2012-10-29T15:23:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan\_Seidt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephan_seidt/32/2336_2.png) [@Stephan\_Seidt](https://discuss.elastic.co/u/Stephan_Seidt)\
**Post date:** [October 29, 2012, 3:23pm UTC](https://discuss.elastic.co/t/boolean-queries-across-multiple-types-in-an-index/9494/1 "2012-10-29T15:23:38Z")

</div>

Hi,

I'm trying to figure out whether I'm modeling and querying my fairly large  
data set correctly.

Given these conditions:

- One index
- Thousands of es-types which share some common fields
- One of these shared fields is "bucket" (effectively partitions everything  
like an S3 bucket)
- Millions of documents evenly distributed across types and buckets (note:  
buckets don't share any types)

Now, the most common query is of this nature:

Find documents where bucket=B and ((type=TypeA and field1=value1 and  
field2\>value2) or (type=TypeB and field3=value1 and field4\>value2))

The characteristics are basically: Search is always bound to a single  
bucket but it happens across a certain number of types within the index.

The way I translated this into the Query DSL:

{  
"filtered": {  
"filter": {  
"and": [  
{"term": {"bucket": "B"}},  
{"or": [  
{"and": [{"type": {"value": "TypeA"}},  
{"term": {"field1": "value1"}}  
{"range": {"field2": {"gt": value2}}}]},  
{"and": [{"type": {"value": "TypeB"}},  
{"term": {"field3": "value1"}}  
{"range": {"field4": {"gt": value2}}}]}  
]}  
]  
}  
}  
}

Questions:

- Is this already too much wishful thinking on my side, as in, does this  
kind of query perform reasonably well with the amount of data I mentioned?
- Is there a better way to formulate the query?
- Do I have to use filtered or can I put the and into the query's toplevel?

Regards  
Stephan

--

---

<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:** [October 29, 2012, 6:39pm UTC](https://discuss.elastic.co/t/boolean-queries-across-multiple-types-in-an-index/9494/2 "2012-10-29T18:39:02Z")

</div>

If you are mostly searching within a single bucket and buckets have  
approximately the same size, it makes sense to either create an index per  
bucket (if you have relatively small number of buckets) or use bucket as a  
routing field. This way your queries will be limited to only a portion of  
your cluster.

On Monday, October 29, 2012 11:23:38 AM UTC-4, Stephan Seidt wrote:

> Hi,
> 
> I'm trying to figure out whether I'm modeling and querying my fairly large  
> data set correctly.
> 
> Given these conditions:
> 
> - One index
> - Thousands of es-types which share some common fields
> - One of these shared fields is "bucket" (effectively partitions  
> everything like an S3 bucket)
> - Millions of documents evenly distributed across types and buckets (note:  
> buckets don't share any types)
> 
> Now, the most common query is of this nature:
> 
> Find documents where bucket=B and ((type=TypeA and field1=value1 and  
> field2\>value2) or (type=TypeB and field3=value1 and field4\>value2))
> 
> The characteristics are basically: Search is always bound to a single  
> bucket but it happens across a certain number of types within the index.
> 
> The way I translated this into the Query DSL:
> 
> {  
> "filtered": {  
> "filter": {  
> "and": [  
> {"term": {"bucket": "B"}},  
> {"or": [  
> {"and": [{"type": {"value": "TypeA"}},  
> {"term": {"field1": "value1"}}  
> {"range": {"field2": {"gt": value2}}}]},  
> {"and": [{"type": {"value": "TypeB"}},  
> {"term": {"field3": "value1"}}  
> {"range": {"field4": {"gt": value2}}}]}  
> ]}  
> ]  
> }  
> }  
> }
> 
> Questions:
> 
> - Is this already too much wishful thinking on my side, as in, does this  
> kind of query perform reasonably well with the amount of data I mentioned?
> - Is there a better way to formulate the query?
> - Do I have to use filtered or can I put the and into the query's toplevel?
> 
> Regards  
> Stephan

--

---

<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:06am UTC](https://discuss.elastic.co/t/boolean-queries-across-multiple-types-in-an-index/9494/3 "2017-07-06T03:06:53Z")

</div>


