导轨4,elasticsearch-rails [英] Rails 4, elasticsearch-rails

查看:71
本文介绍了导轨4,elasticsearch-rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for some advice on the best way forward with my app which i have began to integrate elasticsearch for the first time. Im a bit of a beginner in rails but keen to dive in so forgive any glaring errors!

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

I followed a tutorial http://www.sitepoint.com/full-text-search-rails-elasticsearch/ and have also implemented some additional elasticsearch dsl features from reading documentation etc.. im just not convinced i'm there yet. (I certainly need to move out of the model, as currently most sits in the Product active record model.)

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

I am trying to implement a search on the Product model with ability to partial word search, fuzzy search (misspellings). From what I understand, I am able to set my own analyzers and filters for the elasticsearch, which I have done and currently reside in the Product model. I would like to move these to a more sensible location too, once I have established if indeed I am actually doing this correctly. I do get results when i search but im including things like deleting the index, creating a new index with mapping all in the end of the product model, if what i have below is not "the correct way", what better way is there than what i have to 1, implement elastic search using rails 2, seperate concerns more efficiently.

感谢和赞赏

代码:

lib/tasks/elasticsearch.rake:

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

推荐答案

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

If you are using elasticsearch for searching,then i will recommend gem 'chewy' with elasticsearch server.

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

For more information just go to the links provided below.

耐嚼:

https://github.com/toptal/chewy

将耐嚼性与Elasticsearch集成:

Integrate chewy with elasticsearch:

http://www.toptal.com/ruby-on-rails/elasticsearch-for-ruby-on-rails-介绍耐嚼性

谢谢

这篇关于导轨4,elasticsearch-rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆