Elasticsearch query

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)

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.

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?

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:

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?

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.

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