# Elasticsearch query

**URL:** <https://discuss.elastic.co/t/elasticsearch-query/300708>\
**Category:** Elasticsearch\
**Created:** [March 25, 2022, 2:01pm UTC](https://discuss.elastic.co/t/elasticsearch-query/300708 "2022-03-25T14:01:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisml](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisml/32/103533_2.png) [@chrisml](https://discuss.elastic.co/u/chrisml)\
**Post date:** [March 25, 2022, 2:01pm UTC](https://discuss.elastic.co/t/elasticsearch-query/300708/1 "2022-03-25T14:01:47Z")

</div>

Could anyone shed any light on the nested bool should/musts here? Is it _necessary_ or just poorly written?  
(the query is generated, depending on user-input)

 ![Screenshot 2022-03-25 at 12.05.30 pm](https://us1.discourse-cdn.com/elastic/original/3X/4/2/423d50d89b4b91549c1eae9f50ba74d497dea765.png)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 26, 2022, 4:41am UTC](https://discuss.elastic.co/t/elasticsearch-query/300708/2 "2022-03-26T04:41:48Z")

</div>

Welcome!

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with `</>` icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

That being said, for this exact request, this could be indeed simplified a lot as only one bool query is needed I think.

---

<div class="post-metadata">

**Author:** ![chrisml](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisml/32/103533_2.png) [@chrisml](https://discuss.elastic.co/u/chrisml)\
**Post date:** [March 28, 2022, 3:22pm UTC](https://discuss.elastic.co/t/elasticsearch-query/300708/4 "2022-03-28T15:22:37Z")

</div>

Thanks for the reply. Will do in future.

Would one bool query still only be needed to be able to search multiple vehicle makes and models?

I am trying to refactor it but the following returns no hits as its asking for a vehicle with a make BMW _and_ Bentley and model 1 Series, 2 Series, etc. How do I change those to OR?

```auto
GET _search
{
    "from": 0,
    "size": "11",
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "body_type.keyword": [
                            "Hatchback",
                            "Coupe"
                        ]
                    }
                },
                {
                    "terms": {
                        "make.name.keyword": [
                            "BMW",
                            "Bentley"
                        ]
                    }
                },
                {
                    "term": {
                        "make.name.keyword": "Bentley"
                    }
                },
                {
                    "term": {
                        "make.name.keyword": "BMW"
                    }
                },
                {
                    "term": {
                        "model.name.keyword": "1 Series"
                    }
                },
                {
                    "term": {
                        "model.name.keyword": "2 Series"
                    }
                },
                {
                    "term": {
                        "fuel_type.keyword": "Petrol"
                    }
                },
                {
                    "range": {
                        "price": {
                            "lte": "70000"
                        }
                    }
                },
                {
                    "range": {
                        "price": {
                            "gte": "0"
                        }
                    }
                },
                {
                    "exists": {
                        "field": "geolocation"
                    }
                }
            ]
        }
    },
    "sort": {
        "date_first_registered": {
            "order": "desc"
        }
    },
    "_source": true
}

```

**EDIT** I think I just had that _click_ moment. JSON object children of a bool.must query are joined by AND. bool.should is OR. So I can do this for multiple makes and models:

```auto
GET _search
{
    "from": 0,
    "size": "11",
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "body_type.keyword": [
                            "Hatchback",
                            "Coupe"
                        ]
                    }
                },
                {
                    "terms": {
                        "make.name.keyword": [
                            "BMW",
                            "Bentley"
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "make.name.keyword": "Bentley"
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "make.name.keyword": "BMW"
                                            }
                                        },
                                        {
                                            "term": {
                                                "model.name.keyword": "1 Series"
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "make.name.keyword": "BMW"
                                            }
                                        },
                                        {
                                            "term": {
                                                "model.name.keyword": "2 Series"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "term": {
                        "fuel_type.keyword": "Petrol"
                    }
                },
                {
                    "range": {
                        "price": {
                            "lte": "70000"
                        }
                    }
                },
                {
                    "range": {
                        "price": {
                            "gte": "0"
                        }
                    }
                },
                {
                    "exists": {
                        "field": "geolocation"
                    }
                }
            ]
        }
    },
    "sort": {
        "date_first_registered": {
            "order": "desc"
        }
    },
    "_source": true
}

```

I'm not sure why the original query would have used constant\_score for this though. Any ideas?

---

<div class="post-metadata">

**Author:** ![CoreyFournier](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@CoreyFournier](https://discuss.elastic.co/u/CoreyFournier)\
**Post date:** [March 28, 2022, 8:41pm UTC](https://discuss.elastic.co/t/elasticsearch-query/300708/5 "2022-03-28T20:41:24Z")

</div>

Different matches generate difference scores. If you want everything to have the same weight, you use constant\_score. Not trying to be a jerk, but you should really read the documentation. It's actually really good for the questions you asked.

---

<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:** [April 25, 2022, 8:42pm UTC](https://discuss.elastic.co/t/elasticsearch-query/300708/6 "2022-04-25T20:42:21Z")

</div>

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