Home > Software design >  Elasticsearch constant_score wrapped inside must does not return expected result
Elasticsearch constant_score wrapped inside must does not return expected result

Time:10-26

I have the following ES query :

{
    "query": {
        "bool": {
            "should": [
                {
                    "constant_score": {
                        "boost": 5,
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "ids": {
                                            "values": [
                                                "winnerAthlete-A"
                                            ]
                                        }
                                    },
                                    {
                                        "dis_max": {
                                            "queries": [
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isAthlete": true
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isWinner": true
                                                            }
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "constant_score": {
                        "boost": 4,
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "ids": {
                                            "values": [
                                                "winnerAthlete-B"
                                            ]
                                        }
                                    },
                                    {
                                        "dis_max": {
                                            "queries": [
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isAthlete": true
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    "bool": {
                                                        "filter": {
                                                            "term": {
                                                                "isWinner": true
                                                            }
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

It does return the result I expect : the 2 documents winnerAthlete-A and winnerAthlete-B, assigning a score of 5.0 to winnerAthlete-A and a score of 4.0 to winnerAthlete-B.

Now, when I turn the should on the third line of the query into a must, the query does not match any document whereas I would expect the exact same result. I can't wrap my head around why. I have tried using the ES _explain keyword to understand why this query doesn't match when using must but it didn't help me.

Any idea why this query rewritten with a must does not return anything whereas the should version does return the expected result ?

CodePudding user response:

Should works like "OR" . It will return a document which matches any of clauses.

Must works like "AND" . Document must satisfy both clauses.

Your query is not returning any result because there is no single document which has ids as winnerAthlete-A as well as winnerAthlete-B

  • Related