简体   繁体   中英

elasticsearch sort by score - same field searchable but score different?

I'm trying to sort my ES results by 2 fields: searchable and year.

The mapping in my Rails app:

# mapping
  def as_indexed_json(options={}) 
    as_json(only: [:id, :searchable, :year]) 
  end 

  settings index: { number_of_shards: 5, number_of_replicas: 1 } do
    mapping do
      indexes :id, index: :not_analyzed
      indexes :searchable
      indexes :year
    end
  end

The query:

 @records = Wine.search(query: {match: {searchable: {query:params[:search], fuzziness:2, prefix_length:1}}}, sort: {_score: {order: :desc}, year: {order: :desc}}, size:100)

The interesting thing in the query:

sort: {_score: {order: :desc}, year: {order: :desc}}

I think the query is working well with the 2 sort params. My problem is the score is not the same for 2 documents with the same name (searchable field).

For example, I'm searching for "winery":

在此处输入图片说明

You can see a very different score, even if the searchable field is the same. I think the issue is due to the ID field (it's an UUID in fact). Looks like this ID field influences the score. But in my schema mapping, I wrote that ID should not be analyzed and in my ES query, I ask to search ONLY in "searchable" field, not in ID too.

What did I miss to math the same score for same fields ? (actually, sorting by year after score is not useful cos' scores are different for same fields)

Scores are different, because they are calculated independently for each shard. See here for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM