【问题标题】:Rails 4, elasticsearch-railsRails 4,弹性搜索导轨
【发布时间】:2014-10-13 01:52:18
【问题描述】:

我正在寻找一些关于我的应用程序的最佳前进方式的建议,我已经开始第一次集成弹性搜索。我在 Rails 方面有点初学者,但热衷于潜入,所以请原谅任何明显的错误!

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

我正在尝试在具有部分单词搜索、模糊搜索(拼写错误)功能的 Product 模型上实现搜索。据我了解,我可以为 elasticsearch 设置我自己的分析器和过滤器,我已经完成并且目前驻留在 Product 模型中。一旦我确定我是否确实正确地执行了此操作,我也想将这些移动到更明智的位置。我在搜索时确实得到了结果,但我包括删除索引、创建一个新索引并在产品模型的末尾映射所有内容,如果我下面的内容不是“正确的方式”,还有什么比这更好的方式我必须做的 1,使用 rails 2 实现弹性搜索,更有效地分离关注点。

谢谢,非常感谢

代码:

lib/tasks/elasticsearch.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 %>

我使用的宝石:

 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

【问题讨论】:

    标签: ruby-on-rails elasticsearch


    【解决方案1】:

    如果您使用 elasticsearch 进行搜索,那么我会推荐 gem 'chewy' 与 elasticsearch 服务器。

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

    耐嚼:

    https://github.com/toptal/chewy

    将耐嚼与弹性搜索集成:

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

    谢谢

    【讨论】:

      【解决方案2】:

      我可以推荐 searchkick:

      https://github.com/ankane/searchkick

      使用 searchkick 运行的多个生产应用程序很容易使用。

      还可以查看 searchkick 的文档,其中详细描述了产品搜索以及方面、建议等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-22
        • 2015-10-07
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2018-01-12
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多