gpt4 book ai didi

ruby-on-rails - ElasticSearch 评分(轮胎 gem )

转载 作者:数据小太阳 更新时间:2023-10-29 07:36:37 27 4
gpt4 key购买 nike

我希望 ElasticSearch(具体来说是 Tire gem)根据关键字在字段中出现的次数返回结果。例如,我在名为 Article 的模型中索引字段 title。我有两个对象,第一个对象的 title 值为 'Funny Funny subject' 而第二个对象的标题值为 'Funny subject' .我想以这样一种方式建立索引:如果我搜索关键字 'Funny',第一个对象将首先返回,因为它的标题中出现了两个“Funny”字样。是否可以通过轮胎做到这一点?索引方法又叫什么?

最佳答案

这是一个工作示例,这里的关键因素是 boostvalue 必须足够高并且不能在查询中使用通配符。

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

关于ruby-on-rails - ElasticSearch 评分(轮胎 gem ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12192913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com