Neil Zhu衬潦,簡書ID Not_GOD斤蔓,University AI 創(chuàng)始人 & Chief Scientist,致力于推進世界人工智能化進程镀岛。制定并實施 UAI 中長期增長戰(zhàn)略和目標(biāo)弦牡,帶領(lǐng)團隊快速成長為人工智能領(lǐng)域最專業(yè)的力量。
作為行業(yè)領(lǐng)導(dǎo)者漂羊,他和UAI一起在2014年創(chuàng)建了TASA(中國最早的人工智能社團), DL Center(深度學(xué)習(xí)知識中心全球價值網(wǎng)絡(luò))驾锰,AI growth(行業(yè)智庫培訓(xùn))等,為中國的人工智能人才建設(shè)輸送了大量的血液和養(yǎng)分走越。此外椭豫,他還參與或者舉辦過各類國際性的人工智能峰會和活動,產(chǎn)生了巨大的影響力旨指,書寫了60萬字的人工智能精品技術(shù)內(nèi)容赏酥,生產(chǎn)翻譯了全球第一本深度學(xué)習(xí)入門書《神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)》,生產(chǎn)的內(nèi)容被大量的專業(yè)垂直公眾號和媒體轉(zhuǎn)載與連載谆构。曾經(jīng)受邀為國內(nèi)頂尖大學(xué)制定人工智能學(xué)習(xí)規(guī)劃和教授人工智能前沿課程裸扶,均受學(xué)生和老師好評。
elasticsearch-persistence
Rubygem 提供了 Ruby domain 對象的持久層搬素。
它支持兩個設(shè)計模式來集成你的對象:repository 和 active record呵晨。
Repository
Elasticsearch::Persistence::Repository
模塊提供了一個 repository 模式的實現(xiàn),可以進行保存蔗蹋、刪除何荚、尋找和搜索存儲在 Elasticsearch 中的對象,并對索引的映射和設(shè)置進行配置猪杭。
簡介特性
- Elasticsearch 的客戶端訪問
- 設(shè)置索引名稱、文檔類型和反序列化的對象類
- 為索引復(fù)合映射和設(shè)置
- 創(chuàng)建妥衣、刪除和刷新索引
- 尋找或者搜索文檔
- 提供對 domain 對象和搜索返回結(jié)果的訪問
- 提供對搜索結(jié)果(聚合皂吮、總共……)的 Elasticsearch 響應(yīng)的訪問
- 定義了序列化和反序列化的方法
使用
讓我們看看簡單的老的 Ruby 對象(PORO):
class Note
attr_reader :attributes
def initialize(attributes={})
@attributes = attributes
end
def to_hash
@attributes
end
end
現(xiàn)在創(chuàng)建一個默認的,空白的 repository税手,作為第一步:
require 'elasticsearch/persistence'
repository = Elasticsearch::Persistence::Repository.new
我們可以保存 Note
實例到 repository 中蜂筹,然后查找、搜索芦倒、刪除它:
note = Note.new id: 1, text: 'Test'
repository.save(note)
# PUT http://localhost:9200/repository/note/1
# > {"id":1,"text":"Test"}
# < {"_index":"repository","_type":"note","_id":"1","_version":1,"created":true}
n = repository.find(1)
# GET http://localhost:9200/repository/_all/1
# < {"_index":"repository","_type":"note","_id":"1","_version":2,"found":true, "_source" : {"id":1,"text":"Test"}}
=> <Note:0x007fcbfc0c4980 @attributes={"id"=>1, "text"=>"Test"}>
repository.search(query: { match: { text: 'test' } }).first
# GET http://localhost:9200/repository/_search
# > {"query":{"match":{"text":"test"}}}
# < {"took":2, ... "hits":{"total":1, ... "hits":[{ ... "_source" : {"id":1,"text":"Test"}}]}}
=> <Note:0x007fcbfc1c7b70 @attributes={"id"=>1, "text"=>"Test"}>
repository.delete(note)
# DELETE http://localhost:9200/repository/note/1
# < {"found":true,"_index":"repository","_type":"note","_id":"1","_version":3}
=> {"found"=>true, "_index"=>"repository", "_type"=>"note", "_id"=>"1", "_version"=>2}
repository 模塊提供了一些特性和便利方式來配置和定制行為艺挪,以及對擴展自身和定制的 repository 類的支持。
參考documentation查看更多信息兵扬。
同樣麻裳,檢查example application展示了使用 repository 觀點來進行持久化的方法口蝠。
Active Record
Elasticsearch::Persistence::Model
模塊提供了一個 Active record 的設(shè)計模式,并帶有一個常見的接口來使用 Elasticsearch 作為 Rails 應(yīng)用的持久層津坑。模型和 Rails 的規(guī)范和幫助都是合拍的妙蔗,例如 url_for
。
所有的方法都有易懂的例子疆瑰,online可以查看眉反。
特性
- 來自 ActiveRecords 的持久化方法的熟悉的接口
- 比如 validation 和 callbacks 這樣的共同的模型特性
- 定義模型屬性的方法,包含 Elasticsearch 映射
- 支持 bulk 獲取數(shù)據(jù)(
find_in_batches
,find_each
) - 輔助的搜索結(jié)果易用性穆役,模型實例和元數(shù)據(jù)(如高亮或者聚合)
- 易用的頂層的 gateway 和 client
使用
將 persistence 使用 require 語句加入到 Gemfile:
gem "elasticsearch-persistence", require: 'elasticsearch/persistence/model'
可以包含模塊到一個正常的 Ruby 類中寸五,設(shè)置屬性、映射等等:
class Article
include Elasticsearch::Persistence::Model
# Define a plain `title` attribute
#
attribute :title, String
# Define an `author` attribute, with multiple analyzers for this field
#
attribute :author, String, mapping: { fields: {
author: { type: 'string'},
raw: { type: 'string', analyzer: 'keyword' }
} }
# Define a `views` attribute, with default value
#
attribute :views, Integer, default: 0, mapping: { type: 'integer' }
# Validate the presence of the `title` attribute
#
validates :title, presence: true
# Execute code after saving the model.
#
after_save { puts "Successfully saved: #{self}" }
end
模型的屬性定義支持使用 Virtus RubyGem 實現(xiàn)耿币,naming播歼、validation 等等特性使用 ActiveModel RubyGem 實現(xiàn)。
屬性 validation