Rails 4,elasticsearch-rails

我正在寻找关于我的应用程序的最佳前进方式的一些建议,我已经开始首次整合elasticsearch。 我是一个铁杆的初学者,但热衷于潜水,原谅任何明显的错误!

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

我试图在产品模型上实现搜索,具有部分单词搜索,模糊搜索(拼写错误)的能力。 根据我的理解,我能够为弹性搜索设置我自己的分析器和filter,我已经完成并且目前驻留在产品模型中。 我想将这些移动到一个更合理的位置,一旦我确定了,我确实是这样做的。 我在搜索时得到的结果,但我包括删除索引,在产品模型的最后创建一个映射的新索引,如果我在下面的内容不是“正确的方法”,那么有什么比我需要1,使用rails 2实现弹性搜索,更有效地分离关注点。

谢谢,非常感谢

码:

LIB /任务/ elasticsearch.rake:

require 'elasticsearch/rails/tasks/import' 

视图:

     

我使用的gem:

  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: [''], post_tags: [''], 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

将耐嚼与弹性搜索整合:

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

谢谢

我可以推荐searchkick:

https://github.com/ankane/searchkick

生产中的几个应用程序使用searchkick运行,并且易于使用。

另请查看searchkick的文档,其中详细描述了对产品的搜索,包括方面,建议等。