這裏是一個工作示例,這裏的關鍵因素是boostvalue必須足夠高,並且不能在查詢中使用wildcharts。
require 'tire'
require 'yajl/json_gem'
articles = [
{ :id => '0', :type => 'article', :title => 'nothing funny'},
{ :id => '1', :type => 'article', :title => 'funny'},
{ :id => '2', :type => 'article', :title => 'funny funny funny'}
]
Tire.index 'articles' do
import articles
end
Tire.index 'articles' do
delete
create :mappings => {
:article => {
:properties => {
:id => { :type => 'string', :index => 'not_analyzed', :include_in_all => false },
:title => { :type => 'string', :boost => 50.0, :analyzer => 'snowball' },
:tags => { :type => 'string', :analyzer => 'keyword' },
:content => { :type => 'string', :analyzer => 'snowball' }
}
}
}
import articles do |documents|
documents.map { |document| document.update(:title => document[:title].downcase) }
end
refresh
end
s = Tire.search('articles') do
query do
string "title:funny"
end
end
s.results.each do |document|
puts "* id:#{ document.id } #{ document.title } score: #{document._score}"
end
給
* id:2 funny funny funny score: 14.881571
* id:1 funny score: 14.728935
* id:0 nothing funny score: 9.81929
你能解釋一下多一點你會如何實現這一目標?提升標題字段? – javanna
有關boost的含義,請參閱http://www.elasticsearch.org/guide/reference/mapping/boost-field.html,基本上它決定了與其他字段/標籤等結果相比得出的得分,命中率越高*提升級別這個命中率在索引排名中的可能性更大 – peter
不幸的是,它對我來說不起作用。我爲類似於標題的字段添加了很高的提升,但它沒有按預期正確排序。 – RubyFanatic