Elasticsearch 持久化 Persistence

Neil Zhu,ID Not_GOD,University AI 创始人 & Chief Scientist,致力于推进世界人工智能化进程。制定并实施 UAI 中长期增长战略和目标,带领团队快速成长为人工智能领域最专业的力量。
作为行业领导者,他和UAI一起在2014年创建了TASA(中国最早的人工智能社团), DL Center(深度学习知识中心全球价值网络),AI growth(行业智库培训)等,为中国的人工智能人才建设输送了大量的血液和养分。此外,他还参与或者举办过各类国际性的人工智能峰会和活动,产生了巨大的影响力,书写了60万字的人工智能精品技术内容,生产翻译了全球第一本深度学习入门书《神经网络与深度学习》,生产的内容被大量的专业垂直公众号和媒体转载与连载。曾经受邀为国内顶尖大学制定人工智能学习规划和教授人工智能前沿课程,均受学生和老师好评。

elasticsearch-persistence Rubygem 提供了 Ruby domain 对象的持久层。
它支持两个设计模式来集成你的对象:repositoryactive record

Repository

Elasticsearch::Persistence::Repository 模块提供了一个 repository 模式的实现,可以进行保存、删除、寻找和搜索存储在 Elasticsearch 中的对象,并对索引的映射和设置进行配置。

简介特性

  • Elasticsearch 的客户端访问
  • 设置索引名称、文档类型和反序列化的对象类
  • 为索引复合映射和设置
  • 创建、删除和刷新索引
  • 寻找或者搜索文档
  • 提供对 domain 对象和搜索返回结果的访问
  • 提供对搜索结果(聚合、总共……)的 Elasticsearch 响应的访问
  • 定义了序列化和反序列化的方法

使用

让我们看看简单的老的 Ruby 对象(PORO):

class Note
  attr_reader :attributes

  def initialize(attributes={})
    @attributes = attributes
  end

  def to_hash
    @attributes
  end
end

现在创建一个默认的,空白的 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"}}
=> 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"}}]}}
=> 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 的设计模式,并带有一个常见的接口来使用 Elasticsearch 作为 Rails 应用的持久层。模型和 Rails 的规范和帮助都是合拍的,例如 url_for

所有的方法都有易懂的例子,online可以查看。

特性

  • 来自 ActiveRecords 的持久化方法的熟悉的接口
  • 比如 validation 和 callbacks 这样的共同的模型特性
  • 定义模型属性的方法,包含 Elasticsearch 映射
  • 支持 bulk 获取数据(find_in_batches, find_each
  • 辅助的搜索结果易用性,模型实例和元数据(如高亮或者聚合)
  • 易用的顶层的 gateway 和 client

使用

将 persistence 使用 require 语句加入到 Gemfile:

gem "elasticsearch-persistence", require: 'elasticsearch/persistence/model'

可以包含模块到一个正常的 Ruby 类中,设置属性、映射等等:

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 实现,naming、validation 等等特性使用 ActiveModel RubyGem 实现。

属性 validation

你可能感兴趣的:(Elasticsearch 持久化 Persistence)