2

I am using function_score so that i can use its score_mode as maximum score of the bool query i am using actually i have two boolean query inside should now i want the score of the document to be the maximum score among both queries my code is given below but when i am passing a string for matching both then scores are being added not be taken maximum can anyone please tell me how can i acheive that.

"function_score": {
      "boost_mode": "max",
      "score_mode": "max",
      "query": {
        bool: {
          "disable_coord": true,
          "should": [
            {
              bool: {
                "disable_coord": true,
                "must": [
                  {
                    "constant_score": { // here i am using this because to remove tf/idf factors from my scoring
                      boost: 1.04,
                      "query": {
                        query_string: {
                          query: location_search,
                          fields: ['places_city.city'],
          //               boost: 1.04
                        }
                      }
                    }
                  }
                ]
              }
            },
            {
              "constant_score": { // here i am using this because to remove tf/idf factors from my scoring
              boost: 1,
                "query": {
                  "fuzzy_like_this" : {
                    "fields" : ["places_city.city"],
                    "like_text" : "bangaloremn",
                    "prefix_length": 3,
                    "fuzziness": 2
                  }
                }
              }
            }
         ], "minimum_should_match": 1
       }
     }
  }    
Doug T.
  • 64,223
  • 27
  • 138
  • 202
aman verma
  • 732
  • 1
  • 8
  • 26

1 Answers1

3

Yes boolean query takes a sum by design. If you want the maximum score of two queries, you ought to look at the dismax query. Dismax is designed to pick a "winner".

Roughly speaking, this would look like

{"query":
    "dismax": {
        "queries": [
             { /* your first constant_score query above */},
             {/* your second constant_score query from above */}
        ]
    }
}

Unfortunately, function score query doesn't have a great way of operating on more than one text query at a time. See this question. If you want to do any complex math with the scores of multiple queries, Solr actually has a lot more flexibility in this area.

Community
  • 1
  • 1
Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • can i use dismax query for comparing score with one boolean query and with a costant score query ?? – aman verma Sep 28 '15 at 00:48
  • Yes you can compare any type of query. – Doug T. Sep 28 '15 at 00:50
  • can you also please tell me one thing under fuzzy_like_this query how to acces field of a type like above i want to access a field of name "city" of "places_city" type but wasn't able to do that ?? – aman verma Sep 28 '15 at 00:50
  • "query": { "fuzzy_like_this" : { HERE "fields" : ["places_city.city"], "like_text" : "bangaloremn", "prefix_length": 3, "fuzziness": 2 } } – aman verma Sep 28 '15 at 00:51
  • actually what is happening is that i have places as index and i am refering to field city of places_city type but ES is automatically searching in all types under places why this is happening is the problem ?? – aman verma Sep 28 '15 at 01:08
  • I believe the name is now `dis_max` – technomage Jun 30 '23 at 14:32