Search code examples
ruby-on-railsrubyelasticsearchelasticsearch-rails

elasticsearch match_phrase returns same score for fields with exact and not exact match


I have an elasticsearch company index spreaded among 6 shards. In my index i got a sorted name field that is indexed like this:

indexes target_model.sorted_name,
                    type: :keyword,
                    fields: {
                      as_text: {
                        type: :text,
                        norms: false,
                      }
                    },
                    as: :sorted_name
...

def sorted_name
  "COALESCE(LOWER(TRIM(LEADING FROM COALESCE(companies.name))), '')"
end

No special analyzers have been used. Disabled norms to not to count field length

I need to perform a search query that would boost results with the exact match. For example, within documents that have sorted_name fields like Aliance, Aliance-B2B New York, New Aliance Company - I need 'Aliance' to be with the highest score.

Using query like this gives all this fields the same score.

{
  :should => [
    {
      :match_phrase => {
        :"sorted_name.as_text" => {
          :query => "альянс",
          :boost => 5
        }
      }
    },
    {
      :match => {
        :"sorted_name.as_text" => {
          :query => "альянс",
          :boost => 2
        }
      }
    },
    {
      :match_phrase_prefix => {
        :"sorted_name.as_text" => {
          :query => "альянс"
        }
       }
    }
  ],
  :minimum_should_match => 1
}

Can't figure out why this happens


Solution

  • All 3 terms have an exact text match on one of the stored tokens ("Aliance"). And you disabled norms, so they all score equally

    For the mentioned case you could add a field with keyword (noop) analyzer and query on that. This would boost exact matches on the full term above the rest