# Wrong result in Bool Query

**URL:** <https://discuss.elastic.co/t/wrong-result-in-bool-query/241136>\
**Category:** Elasticsearch\
**Created:** [July 14, 2020, 12:55pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136 "2020-07-14T12:55:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 12:55pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/1 "2020-07-14T12:55:59Z")

</div>

> Mapping

```
PUT index
PUT /index/_mapping
{
  "properties": {
    "categories": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "deleted": {
      "type": "boolean"
    },
    "id": {
      "type": "long"
    },
    "manufacturers": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "vendors": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "isDeletedVendor": {
          "type": "boolean"
        },
        "isPublishedVendor": {
          "type": "boolean"
        }
      }
    },
    "published": {
      "type": "boolean"
    }
  }
}

```

> Fill with Data

```
PUT index/_doc/1
{
  "categories": [
    {
      "id": 9
    }
  ],
  "manufacturers": [
    {
      "id": 2452
    }
  ],
  "vendors": [
    {
      "id": 8,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    },
    {
      "id": 9,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    }
  ],
  "published": true,
  "deleted": false
}

```

Query which returns wrong data

```
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "categories",
            "query": {
              "terms": {
                "categories.id": [
                  9
                ]
              }
            }
          }
        },
        {
          "terms": {
            "deleted": [
              false
            ]
          }
        },
        {
          "terms": {
            "published": [
              true
            ]
          }
        },
        {
          "nested": {
            "path": "vendors",
            "query": {
              "terms": {
                "vendors.isDeletedVendor": [
                  false
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "vendors",
            "query": {
              "terms": {
                "vendors.isPublishedVendor": [
                  true
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturers"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              87
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturers"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              56
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 1000,
  "_source": {
    "includes": [
      "modelName",
      "vendors.id", 
      "manufacturers.id", 
      "id"
    ]
  }
}

```

The logic of the query in should is like this

Select \* from index where(manufacturers is null and vendorsid in(87)) or (manufacturers is null and vendorsidin(56)) but it gives me hits where manufacturers id are not null and vendors id are not in 87 or 56

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 1:16pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/2 "2020-07-14T13:16:50Z")

</div>

It gives me result like this why?

```
"hits" : [
      {
        "_index" : "onoff-live",
        "_type" : "_doc",
        "_id" : "130011",
        "_score" : 5.0,
        "_source" : {
          "modelName" : "Galaxy A20",
          "manufacturers" : [
            {
              "id" : 216
            }
          ],
          "id" : 130011,
          "vendors" : [
            {
              "id" : 23
            }
          ]
        }
      }]
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 14, 2020, 1:40pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/3 "2020-07-14T13:40:25Z")

</div>

Quick sanity check - do the manufacturers and categories each have just a single property (id)?

There's no need to use "nested" types if you have single properties - just send arrays of values.  
Nested is designed to solve the cross-matching problem if you query objects with multiple properties like people with a firstName and lastName

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 1:42pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/4 "2020-07-14T13:42:32Z")

</div>

I have many properties on category and manufacturer nested object, but I wrote only ids...

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 1:43pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/5 "2020-07-14T13:43:37Z")

</div>

Problem in this query is in must part... When i dont have must part there is only 3 hits, but with must part it is 9980... they should be combined with and logic....

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 14, 2020, 1:51pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/6 "2020-07-14T13:51:32Z")

</div>

Your example query matches neither of the example docs so I'm having a hard time understanding the problem.  
Can you share the exact JSON for the docs and query that fail to behave as expected?

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 1:55pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/7 "2020-07-14T13:55:13Z")

</div>

i have shared exact shared exact json for query.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 14, 2020, 2:01pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/8 "2020-07-14T14:01:54Z")

</div>

> [@buka\_bidzina](#):
>
> ```auto
> "vendors.isDeletedVendor": [
> false
> ]
> 
> ```

That query doesn't match the example doc because the doc only has `isDeleletedVendor: true`

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 2:02pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/9 "2020-07-14T14:02:41Z")

</div>

okey, sorry I will give you in a minute

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 2:07pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/10 "2020-07-14T14:07:21Z")

</div>

try this query it doesn't need ispublishedvendor

```
  {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "categories",
                "query": {
                  "terms": {
                    "categories.id": [
                      9
                    ]
                  }
                }
              }
            },
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "must_not": [
                              {
                                "nested": {
                                  "path": "manufacturers",
                                  "query": {
                                    "exists": {
                                      "field": "manufacturers"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "nested": {
                            "path": "vendors",
                            "query": {
                              "terms": {
                                "vendors.id": [
                                  87
                                ]
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "must_not": [
                              {
                                "nested": {
                                  "path": "manufacturers",
                                  "query": {
                                    "exists": {
                                      "field": "manufacturers"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "nested": {
                            "path": "vendors",
                            "query": {
                              "terms": {
                                "vendors.id": [
                                  56
                                ]
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "size": 1000,
      "_source": {
        "includes": [
          "modelName",
          "vendors.id", 
          "manufacturers.id", 
          "id"
        ]
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 14, 2020, 2:10pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/11 "2020-07-14T14:10:57Z")

</div>

That matches your example doc. Were you expecting it not to?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 14, 2020, 2:19pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/12 "2020-07-14T14:19:02Z")

</div>

> [@buka\_bidzina](#):
>
> Problem in this query is in must part... When i dont have must part there is only 3 hits, but with must part it is 9980... they should be combined with and logic

You may have misunderstood the nature of shoulds and musts.  
When you have `must`s (or `filter`s) the `should`s are optional - "nice to haves" for extra scoring points.  
When you only have `should` clauses it is mandatory at least one should match.  
So a `should` clause may well match only 3 hits but when combined with a must clause it could bring in 9980.  
Working as designed I suspect

---

<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:** [August 11, 2020, 2:30pm UTC](https://discuss.elastic.co/t/wrong-result-in-bool-query/241136/13 "2020-08-11T14:30:30Z")

</div>

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