# Querying different types with combined criteria

**URL:** <https://discuss.elastic.co/t/querying-different-types-with-combined-criteria/11339>\
**Category:** Elasticsearch\
**Created:** [March 27, 2013, 4:13pm UTC](https://discuss.elastic.co/t/querying-different-types-with-combined-criteria/11339 "2013-03-27T16:13:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 27, 2013, 4:13pm UTC](https://discuss.elastic.co/t/querying-different-types-with-combined-criteria/11339/1 "2013-03-27T16:13:26Z")

</div>

Hi,

I'm struggling on how to best query my data when I have different types  
within the same index. Let me explain.

I have an index (poc) where I define several types representing different  
task types (taskA, taskB, etc.). All the task types share some common  
fields, like the "taskId", but then each individual task type may have its  
own specific fields:

taskA --\> milestone

taskB --\> legalstatus

Therefore, I want to provide my users with a search form where any  
combination of criteria can be entered, so for instance they can search  
something like:

"Give me all the taskA with milestone = 5, and all the taskB with  
legalstatus = 12"

As you can see from that query, I have to combine criteria based on  
different types, so basically I'm following this strategy:

- Use a filtered query
- If there are any common fields in the user's query, then use them for  
the "query" part; else use a "match\_all"
- For all the specific fields, use the "filter" part using an "or" array  
for each and every criterion

So following our example, I would have:

> <https://gist.github.com/emedina/5255474>

Now my questions:

1. Is this a valid way to perform such queries? Think about all sort of  
different combinations in my data, with more task types (up to 7 different  
ones), and heterogeneous fields in each one (plus the common fields for all  
of them).
2. If it is, how does ES actually run this query? Does ES sequentially  
scan each and every document in the index, and applies the filter on each,  
or does something different? I'm worried here whether being a valid  
approach, it's actually the optimal one.

Here I provide the full curl recreation with sample data for you evaluation:

> <https://gist.github.com/emedina/5255378>

Thanks!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:44am UTC](https://discuss.elastic.co/t/querying-different-types-with-combined-criteria/11339/2 "2017-07-06T02:44:09Z")

</div>


