gpt4 book ai didi

ruby-on-rails - 使用 Rails (Tire) 和 ElasticSearch 进行模糊字符串匹配

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

我有一个 Rails 应用程序,现在设置了 ElasticSearch 和 Tire gem 以在模型上进行搜索,我想知道我应该如何设置我的应用程序以对模型中的某些索引进行模糊字符串匹配。我将我的模型设置为索引标题、描述等内容,但我想对其中一些进行模糊字符串匹配,但我不确定在何处进行此操作。如果您想发表评论,我将在下面包含我的代码!谢谢!

在 Controller 中:

    def search
@resource = Resource.search(params[:q], :page => (params[:page] || 1),
:per_page =>15, load: true )
end

在模型中:

class Resource < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks

belongs_to :user
has_many :resource_views, :class_name => 'UserResourceView'

has_reputation :votes, source: :user, aggregated_by: :sum

attr_accessible :title, :description, :link, :tag_list, :user_id, :youtubeID
acts_as_taggable

mapping do
indexes :id, :index => :not_analyzed
indexes :title, :analyzer => 'snowball', :boost => 40
indexes :tag_list, :analyzer => 'snowball', :boost => 8
indexes :description, :analyzer => 'snowball', :boost => 2
indexes :user_id, :analyzer => 'snowball'
end
end

最佳答案

尝试创建自定义分析器以实现其他词干提取功能等。看我的例子(这个例子也用到了Mongoid & 附件,不需要就别看):

class Document
include Mongoid::Document
include Mongoid::Timestamps
include Tire::Model::Search
include Tire::Model::Callbacks

field :filename, type: String
field :md5, type: String
field :tags, type: String
field :size, type: String

index({md5: 1}, {unique: true})
validates_uniqueness_of :md5


DEFAULT_PAGE_SIZE = 10

settings :analysis => {
:filter => {
:ngram_filter => {
:type => "edgeNGram",
:min_gram => 2,
:max_gram => 12
},
:custom_word_delimiter => {
:type => "word_delimiter",
:preserve_original => "true",
:catenate_all => "true",
}
}, :analyzer => {
:index_ngram_analyzer => {
:type => "custom",
:tokenizer => "standard",
:filter => ["lowercase", "ngram_filter", "asciifolding", "custom_word_delimiter"]
},
:search_ngram_analyzer => {
:type => "custom",
:tokenizer => "standard",
:filter => ["standard", "lowercase", "ngram_filter", "custom_word_delimiter"]
},
:suggestions => {
:tokenizer => "standard",
:filter => ["suggestions_shingle"]
}
}
} do
mapping {
indexes :id, index: :not_analyzed
indexes :filename, :type => 'string', :store => 'yes', :boost => 100, :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer
indexes :tags, :type => 'string', :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer
indexes :attachment, :type => 'attachment',
:fields => {
:content_type => {:store => 'yes'},
:author => {:store => 'yes', :analyzer => 'keyword'},
:title => {:store => 'yes'},
:attachment => {:term_vector => 'with_positions_offsets', :boost => 90, :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer},
:date => {:store => 'yes'}
}
}
end


def to_indexed_json
self.to_json(:methods => [:attachment])
end

def attachment
path_to_file = "#{Rails.application.config.document_library}#{path}/#{filename}"
Base64.encode64(open(path_to_file) { |file| file.read })
end

def self.search(query, options)
tire.search do
query { string "#{query}", :default_operator => :AND, :default_field => 'attachment', :fields => ['filename', 'attachment', 'tags'] }
highlight :attachment
page = (options[:page] || 1).to_i
search_size = options[:per_page] || DEFAULT_PAGE_SIZE
from (page -1) * search_size
size search_size
sort { by :_score, :desc }
if (options[:facet])
filter :terms, :tags => [options[:facet]]
facet 'global-tags', :global => true do
terms :tags
end
facet 'current-tags' do
terms :tags
end
end
end
end
end

希望对你有帮助

关于ruby-on-rails - 使用 Rails (Tire) 和 ElasticSearch 进行模糊字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14391395/

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