# Getting incorrect results while doing nested must and should

**URL:** <https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788>\
**Category:** Elasticsearch\
**Created:** [November 30, 2017, 4:01pm UTC](https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788 "2017-11-30T16:01:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rama\_krishna](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rama\_krishna](https://discuss.elastic.co/u/rama_krishna)\
**Post date:** [November 30, 2017, 4:01pm UTC](https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788/1 "2017-11-30T16:01:51Z")

</div>

If written in SQL it would be:

Select \* from Cars where id=10 and((color='blue' and company='BMW') OR (color='red' and company ='Mercedes'))

I've tried with Nested must and should like:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "id": [
              10
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "nested": {
                  "path": "cars",
                  "query": {
                    "bool": {
                      "must": [
                        {
                         "terms": {
                           "cars.company": [
						   "BMW"
						   ]
                         }
                        },
                        {
                          "terms": {
                            "cars.color": [
                              "blue"
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              }, 
              {
                "nested": {
                  "path": "cars",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "cars.company": [
                              "Mercedes"
                            ]
                          }
                        },
                        {
                          "terms": {
                            "cars.color": [
                              "red"
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

When I did this, I am only getting the results for first part of query(i.e., id=10 and car.company=BMW and car.color = blue) even though the index consists data for mercedes and red color. Do I need to modify anything in the query?

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [November 30, 2017, 8:51pm UTC](https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788/2 "2017-11-30T20:51:37Z")

</div>

Hi rama\_krishna,

Can you provide one example of document you indexed? And if possible the relevant part of the mapping. That would help someone to reproduce the behavior you described.

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![rama\_krishna](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rama\_krishna](https://discuss.elastic.co/u/rama_krishna)\
**Post date:** [December 1, 2017, 2:06pm UTC](https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788/3 "2017-12-01T14:06:55Z")

</div>

Hi Luiz,

Found the fix. Actually, the issue is with the data as it has special characters in the data. There is no change in the query; it is bringing back the correct set of results.

Thanks for your reply.

---

<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:** [December 29, 2017, 2:07pm UTC](https://discuss.elastic.co/t/getting-incorrect-results-while-doing-nested-must-and-should/109788/4 "2017-12-29T14:07:15Z")

</div>

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