簡體   English   中英

導軌4,elasticsearch-rails

[英]Rails 4, elasticsearch-rails

我正在尋找有關通過我的應用程序前進的最佳方式的建議,這是我第一次開始集成Elasticsearch。 我是一名初學者,但是熱衷於深入研究,以便原諒任何明顯的錯誤!

我遵循了教程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

如果您正在使用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

謝謝

我可以推薦searchkick:

https://github.com/ankane/searchkick

生產中的多個應用程序通過searchkick運行,並且易於使用。

另外,請查看searchkick的文檔,其中詳細介紹了產品搜索,包括方面,建議等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM