我正在寻找一些关于我的应用程序前进的最佳方式的建议,我已经开始第一次集成elasticsearch。我是rails的初学者,但热衷于潜入其中,所以请原谅任何明显的错误!
我遵循了一个教程http://www.sitepoint.com/full-text-search-rails-elasticsearch/,还通过阅读文档等实现了一些附加的elasticsearch dsl特性。我只是还不确定我已经做到了。(我当然需要走出这个模型,因为目前大多数都位于产品活动记录模型中。)
我正在尝试实现一个搜索产品模型与部分词搜索,模糊搜索(拼写错误)的能力。据我所知,我能够为elasticsearch设置自己的分析器和过滤器,这是我已经完成的,目前驻留在Product模型中。我也想把它们移到一个更合理的位置,一旦我确定了我是否真的正确地这样做了。当我搜索时,我确实得到了结果,但我包括了一些事情,比如删除索引,在产品模型的末尾创建一个新的索引和映射,如果我下面的方法不是“正确的方法”,还有什么比我必须的1更好的方法,使用rails 2实现弹性搜索,更有效地分离关注点。
谢谢,非常感谢
代码:
lib/task/tasks ticsearch.rake:
require 'elasticsearch/rails/tasks/import'查看:
<%= form_tag search_index_path, class: 'search', method: :get do %>
<%= text_field_tag :query, params[:query], autocomplete: :off, placeholder: 'Search', class: 'search' %>
<% end %>我使用的Gems:
gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'搜索控制器:
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发布于 2015-04-20 20:43:20
如果你正在使用elasticsearch进行搜索,那么我推荐使用elasticsearch服务器的gem 'chewy‘。
有关更多信息,请转到下面提供的链接。
对于耐嚼的:
https://github.com/toptal/chewy
将chewy与elasticsearch集成:
http://www.toptal.com/ruby-on-rails/elasticsearch-for-ruby-on-rails-an-introduction-to-chewy
谢谢
发布于 2015-05-28 21:59:55
我可以推荐searchkick:
https://github.com/ankane/searchkick
生产中的几个应用程序都运行在searchkick上,而且使用起来很简单。
还可以查看searchkick的文档,其中详细描述了对产品的搜索,包括方面、建议等。
https://stackoverflow.com/questions/25392300
复制相似问题