[英]Indexing elasticsearch without active record
沒有活動記錄時,如何在彈性搜索上使用索引? 但是model.rb這樣指定
class Article
MIN_TERM_CHARS = 2
MAX_NGRAM_CHARS = 20
include SyncAttr
include Tire::Model::Persistence
index_name { Thread.current[:index_name] || "test" }
document_type "document"
# TODO: set index_options to doc for some fields for optimization
# TODO: maybe use simple analyzer for 'id' fieldsart
property :artikelnummer, type: 'string', index: 'not_analyzed'
property :eannummer, type: 'string', index: 'not_analyzed'
property :bezeichnung, type: 'multi_field', fields: {
bezeichnung: {type: 'string'},
ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'},
suggest: {:type => 'string', :analyzer => 'suggest_analyzer'}
}
我想索引我的數據。 我該如何實現? 我正在為此使用輪胎/ karmi
karmi/tire
寶石(已)疲勞 :
自從當時發布了新庫
https://github.com/elasticsearch/elasticsearch-ruby
以來,它已於2013年9月https://github.com/elasticsearch/elasticsearch-ruby
。新
elasticsearch
紅寶石創業板Elasticsearch不支持輪胎的所有高果糖高水平的DSL,或者與Rails的助手,如搜索結果的自動兼容性url_for
或您的機型豐富的集成。
查看elasticsearch-ruby
,以獲取更多最新的針對彈性搜索API的ruby包裝器。
編輯
只是要澄清- tire
仍然可以使用,但它的作者建議您使用新的寶石:
如果您在項目中使用Tyre,或者打算開始使用Tyre,那么以下幾點很重要:
圖書館將繼續像現在一樣工作 。 實際上,它肯定會更好, 因為將修復錯誤並添加重要功能。
所有的URL將繼續起作用 ,並且可以通過瀏覽器Git或Bundler正確地重定向。 除非您願意,否則無需更改它們。
該項目已被重命名,以增加此遷移的可見性,而不僅僅是為了好玩。
新gem的基本API用法(來自文檔 ):
與elasticsearch gem配合使用
當您從
elasticsearch-ruby
包中使用客戶端時,庫模塊已經包含在內,因此您只需調用API方法即可:require 'elasticsearch' client = Elasticsearch::Client.new log: true client.index index: 'myindex', type: 'mytype', id: 1, body: { title: 'Test' } # => {"_index"=>"myindex", ... "created"=>true} client.search index: 'myindex', body: { query: { match: { title: 'test' } } } # => {"took"=>2, ..., "hits"=>{"total":5, ...}}
要為非模型支持的對象建立索引,可以使用Uri引用的elasticsearch-ruby API(帶有client.create語法)。 Elasticsearch-ruby只是Elasticsearch的Ruby連接器,因此您可以對常規curl請求進行的任何操作,都可以對API進行的操作,包括設置自定義映射,添加文檔和執行搜索。 我不確定是否可以使用Tyre索引非模型支持的對象,但是我知道Tire可以搜索任何索引,包括不是Tyre設置的索引(例如您使用elasticsearch設置的自定義索引) -紅寶石)。
您還可以使用Tyre來索引模型支持的對象,同時通過包括Tire::Persistence
模塊來完全繞過ActiveRecord (而不是使用ActiveRecord回調)。 要使其正常工作:
在模型類中包括Tire::Persistence
模塊(已完成)。
在config/application.rb
禁用ActiveRecord,將require 'rails/all'
的行替換為:
require "rails" [ # 'active_record', 'action_controller', 'action_mailer', 'active_resource', 'rails/test_unit', ].each do |framework| begin require "#{framework}/railtie" rescue LoadError end end
刪除config/database.yml
從Gemfile中刪除'gem sqlite3'或您使用的任何數據庫gem。
運行bundle install
有關更多詳細信息,請參閱此博客文章 ,以及有關stackoverflow問題以及使用Elasticsearch而不是ActiveRecord索引對象的工作模型的完整示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.