# How does ES perform if the query does boolean operation on nested types

**URL:** <https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142>\
**Category:** Elasticsearch\
**Created:** [November 16, 2018, 10:31pm UTC](https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142 "2018-11-16T22:31:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepak643](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepak643/32/45940_2.png) [@deepak643](https://discuss.elastic.co/u/deepak643)\
**Post date:** [November 16, 2018, 10:31pm UTC](https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142/1 "2018-11-16T22:31:11Z")

</div>

I would like to know, if there will be any difference in performance between these queries? Both queries brings up the same result.

1. OR operation in a nested type between two fields.  
nested {  
path : "blah"  
should : {  
match : {"k1" : "v1"},  
match : {"k2" : "v2"}  
}

and

1. OR operation between two nested type fields.  
should [{  
nested {  
"path" : "blah"  
match {  
"k1" : "v1"  
},  
{  
nested {  
"path" : "blah"  
match {  
"k2" : "v2"  
},  
}]

Lets say the query happens on millions of records.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [November 30, 2018, 10:45pm UTC](https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142/2 "2018-11-30T22:45:16Z")

</div>

You can use [\_valide API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html) with an `explain` option to see what your queries translate into.

```auto
GET my-index/my-type/_validate/query?explain=true

```

For the 1st query, I have got:

```auto
"explanation": "+ToParentBlockJoinQuery (my-path.k1:v1 my-path.k2:v2) 
#DocValuesFieldExistsQuery [field=_primary_term]"

```

For the 2nd query, I have got:

```auto
"explanation": "
+(ToParentBlockJoinQuery (my-path.k1:v1) ToParentBlockJoinQuery (my-path.k2:v2)) 
#DocValuesFieldExistsQuery [field=_primary_term]"

```

Then **theoretically** we can reason, if there is a big overlap between documents returned from `match : {"k1" : "v1"}` and `match : {"k2" : "v2"}`, then 1st query should execute faster.

For example, if:  
match : {"k1" : "v1"} produces 10k docs  
match : {"k2" : "v2"} produces 10k docs  
should : { match : {"k1" : "v1"}, match : {"k2" : "v2"}} produces 15k docs

Then for the 1st query: ToParentBlockJoinQuery execute on 15k docs.  
For the 2nd query: ToParentBlockJoinQuery execute on 10k docs for k1, then another ToParentBlockJoinQuery will execute on 10k docs for k2, which overall will be slower.

But this is all theoretical, and the best way to measure the difference performance is to run experiments and measure performance based on real data.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [December 7, 2018, 9:26am UTC](https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142/3 "2018-12-07T09:26:40Z")

</div>

Complexity is the same, the difference should insignificant.

---

<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:** [January 4, 2019, 9:26am UTC](https://discuss.elastic.co/t/how-does-es-perform-if-the-query-does-boolean-operation-on-nested-types/157142/4 "2019-01-04T09:26:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
