简体   繁体   中英

ElasticSearch Scoring (Tire gem)

I want ElasticSearch (Tire gem to be specific) to return the result based on the number of times a keyword appears in the fields. For example, I index the field in a model called Article. 建立索引。 I have two objects, the first object has the value 'Funny Funny subject' while the second object has the title value 'Funny subject' . 值为“ Funny Funny subject” ,第二个对象的标题值为“ Funny subject”I want to index in such a way that if I search for the keyword , the first object will return first since it has two 'Funny' words appearing in the title. ,则第一个对象将首先返回,因为它的标题中出现了两个“ Funny”字样。 Is it possible to do this via Tire? What is the indexing method called as well?

Here a working sample, the key factor here is the boostvalue that has to be high enough and you can't use wildcharts in the query.

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

gives

* id:2 funny funny funny score: 14.881571
* id:1 funny score: 14.728935
* id:0 nothing funny score: 9.81929

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