gpt4 book ai didi

ruby-on-rails - rails 4,elasticsearch-rails

转载 作者:行者123 更新时间:2023-12-03 01:07:26 31 4
gpt4 key购买 nike

我正在寻找有关通过我的应用程序前进的最佳方式的建议,这是我首次开始集成Elasticsearch。我是一名初学者,但是热衷于深入研究,以便原谅任何明显的错误!

我遵循了http://www.sitepoint.com/full-text-search-rails-elasticsearch/教程,并且还通过阅读文档等实现了一些额外的elasticsearch dsl功能。我只是不相信我在那里。 (我当然需要退出模型,因为当前大多数人都处于产品有效记录模型中。)

我正在尝试对Product模型实现搜索,以实现部分单词搜索,模糊搜索(拼写错误)的功能。据我了解,我能够为我的Elasticsearch设置自己的分析器和过滤器,这已经完成并且目前位于Product模型中。一旦确定是否确实正确地执行了此操作,我也希望将它们移至更明智的位置。我在搜索时确实得到了结果,但是我包括删除索引,在产品模型的末尾全部映射来创建新索引之类的事情,如果我下面的内容不是“正确的方法”,那还有什么更好的方法呢?我必须要做的1,使用rails进行 flex 搜索2,更有效地分离关注点。

谢谢,非常感谢

码:

lib / tasks / elasticsearch.rake:

   require 'elasticsearch/rails/tasks/import'

View :
    <%= form_tag search_index_path, class: 'search', method: :get do %>
<%= text_field_tag :query, params[:query], autocomplete: :off, placeholder: 'Search', class: 'search' %>
<% end %>

我使用的 gem :
 gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'

搜索 Controller :
class SearchController < ApplicationController
def index
if params[:query].nil?
@products = []
else
@products = Product.search(params[:query])
end
end
end

产品型号:
require 'elasticsearch/model'
class Product < ActiveRecord::Base
# ElasticSearch
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

settings index: {
number_of_shards: 1,
analysis: {
filter: {
trigrams_filter: {
type: 'ngram',
min_gram: 2,
max_gram: 10
},
content_filter: {
type: 'ngram',
min_gram: 4,
max_gram: 20
}
},
analyzer: {
index_trigrams_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['lowercase', 'trigrams_filter']
},
search_trigrams_analyzer: {
type: 'custom',
tokenizer: 'whitespace',
filter: ['lowercase']
},
english: {
tokenizer: 'standard',
filter: ['standard', 'lowercase', 'content_filter']
}
}
}
} do
mappings dynamic: 'false' do
indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
indexes :description, index_analyzer: 'english', search_analyzer: 'english'
indexes :manufacturer_name, index_analyzer: 'english', search_analyzer: 'english'
indexes :type_name, analyzer: 'snowball'
end
end

# Gem Plugins
acts_as_taggable
has_ancestry
has_paper_trail
@@ -99,6 +146,33 @@ def all_sizes
product_attributes.where(key: 'Size').map(&:value).join(',').split(',')
end

def self.search(query)
__elasticsearch__.search(
{
query: {
query_string: {
query: query,
fuzziness: 2,
default_operator: "AND",
fields: ['name^10', 'description', 'manufacturer_name', 'type_name']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
name: {},
description: {}
}
}
}
)
end

def as_indexed_json(options={})
as_json(methods: [:manufacturer_name, :type_name])
end

end

# Delete the previous products index in Elasticsearch
Product.__elasticsearch__.client.indices.delete index: Product.index_name rescue nil

# Create the new index with the new mapping
Product.__elasticsearch__.client.indices.create \
index: Product.index_name,
body: { settings: Product.settings.to_hash, mappings: Product.mappings.to_hash }

# Index all article records from the DB to Elasticsearch
Product.import(force: true)
end

最佳答案

如果您使用Elasticsearch进行搜索,那么我将为Elasticsearch服务器推荐gem'chewy'。

有关更多信息,请转到下面提供的链接。

耐嚼

https://github.com/toptal/chewy

将耐嚼性与elasticsearch集成:

http://www.toptal.com/ruby-on-rails/elasticsearch-for-ruby-on-rails-an-introduction-to-chewy

谢谢

关于ruby-on-rails - rails 4,elasticsearch-rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392300/

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