# Conditional query based on index/type

**URL:** <https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067>\
**Category:** Elasticsearch\
**Created:** [September 19, 2012, 3:55pm UTC](https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067 "2012-09-19T15:55:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remy\_Gendron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_gendron/32/2719_2.png) [@Remy\_Gendron](https://discuss.elastic.co/u/Remy_Gendron)\
**Post date:** [September 19, 2012, 3:55pm UTC](https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067/1 "2012-09-19T15:55:53Z")

</div>

Hi all,

I have fields (same names) that are present on all document types. Doing a  
polymorphic search on these fields is therefore not a problem.

But, I have fields that are present only on some document types.

Is it possible to do a single search query (a boolean query) targeted at  
multiple document types, that includes a field query for a field not  
present on all document types.

I would like it to be a kind of conditional query where that field\_query  
would not be applied against document types where that field is not  
present/mapped.

Rémy

--

---

<div class="post-metadata">

**Author:** ![Remy\_Gendron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_gendron/32/2719_2.png) [@Remy\_Gendron](https://discuss.elastic.co/u/Remy_Gendron)\
**Post date:** [September 19, 2012, 4:00pm UTC](https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067/2 "2012-09-19T16:00:22Z")

</div>

Just saw the Indices Query... I guess I probably could use this.

I'd need to check every sub-query within the boolean query and explode the  
main query as an indices query with as many query variants as necessary.

--

---

<div class="post-metadata">

**Author:** ![Remy\_Gendron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_gendron/32/2719_2.png) [@Remy\_Gendron](https://discuss.elastic.co/u/Remy_Gendron)\
**Post date:** [September 19, 2012, 4:07pm UTC](https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067/3 "2012-09-19T16:07:16Z")

</div>

Looking at the code, I see that there is only one query matched against a  
list of indices and another nomatch/defautl query.

What I would need is to be able to specify a different query for each  
indices in the multi-indices query...

--

---

<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:12am UTC](https://discuss.elastic.co/t/conditional-query-based-on-index-type/9067/4 "2017-07-06T03:12:16Z")

</div>


