2012-08-30 82 views
4

我希望ElasticSearch(輪胎寶石是特定的)根據關鍵字出現在字段中的次數返回結果。例如,我在名爲Article的模型中將字段標題編入索引。我有兩個對象,第一個對象具有標題'有趣搞笑主題'而第二個對象具有標題值'有趣主題'。我想索引的方式是,如果我搜索關鍵字'有趣',則第一個對象將首先返回,因爲它在標題中出現了兩個'有趣'字樣。是否可以通過Tyre做到這一點?什麼是索引方法也稱爲?ElasticSearch評分(輪胎寶石)

回答

2

這裏是一個工作示例,這裏的關鍵因素是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 
+0

你能解釋一下多一點你會如何實現這一目標?提升標題字段? – javanna

+0

有關boost的含義,請參閱http://www.elasticsearch.org/guide/reference/mapping/boost-field.html,基本上它決定了與其他字段/標籤等結果相比得出的得分,命中率越高*提升級別這個命中率在索引排名中的可能性更大 – peter

+0

不幸的是,它對我來說不起作用。我爲類似於標題的字段添加了很高的提升,但它沒有按預期正確排序。 – RubyFanatic

相關問題