0
votes

This is a story of 2 queries. One returns results, while the other does not. Why?

Query that returns results:

{
   "index":"my_index",
   "type":"places",
   "body":{
      "from":0,
      "size":"12",
      "sort":{
         "_score":{
            "order":"desc"
         }
      },
      "query":{
         "function_score":{
            "query":{
               "bool":{
                  "must":[

                  ],
                  "should":[

                  ],
                  "must_not":[

                  ],
                  "filter":[

                  ]
               }
            },
            "functions":[
               {
                  "gauss":{
                     "location":{
                        "origin":{
                           "lat":"41.243368",
                           "lon":"-116.79711"
                        },
                        "offset":"0mi",
                        "scale":"100mi"
                     }
                  }
               }
            ],
            "score_mode":"sum",
            "boost_mode":"sum"
         }
      }
   }
}

Query that does NOT return results:

{
   "index":"my_index",
   "type":"places",
   "body":{
      "from":0,
      "size":"12",
      "sort":{
         "_score":{
            "order":"desc"
         }
      },
      "query":{
         "function_score":{
            "query":{
               "bool":{
                  "must":[

                  ],
                  "should":[
                     {
                        "terms":{
                           "region_id":[
                              32273
                           ],
                           "boost":2
                        }
                     }
                  ],
                  "must_not":[

                  ],
                  "filter":[

                  ]
               }
            },
            "functions":[
               {
                  "gauss":{
                     "location":{
                        "origin":{
                           "lat":"41.243368",
                           "lon":"-116.79711"
                        },
                        "offset":"0mi",
                        "scale":"100mi"
                     }
                  }
               }
            ],
            "score_mode":"sum",
            "boost_mode":"sum"
         }
      }
   }
}

The Bool Query has only a Should Clause.

Also, I am summing the scores, not multiplying them.

It's true that I do not have any items in the index that have the specified "region_id", but I fail to see how the results of the first query are excluded from the second query. It seems like the 0 score on the Bool Query is acting like a filter.

1
Do you have a document with region_id = 32273? - sramalingam24

1 Answers

2
votes

The first query acts as a match_all query so it returns everything and applies the function score. In the second query you have a should clause, but since there are no must or filter clause it must match.

From ES docs: "In a boolean query with no must or filter clauses, one or more should clauses must match a document"