Sha256: ddec8adf06241f49626b0465f8010dfd62e946afcb94223692ea031d51e50370

Contents?: true

Size: 1.88 KB

Versions: 24

Compression:

Stored size: 1.88 KB

Contents

# Ohm for Redis and Elasticsearch
# ===============================
#
# https://github.com/soveran/ohm#example

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'pry'
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)

require 'logger'
require 'ansi/core'
require 'active_model'
require 'ohm'

require 'elasticsearch/model'

class Article < Ohm::Model
  # Include JSON serialization from ActiveModel
  include ActiveModel::Serializers::JSON

  attribute :title
  attribute :published_at
end

# Extend the model with Elasticsearch support
#
Article.__send__ :include, Elasticsearch::Model

# Register a custom adapter
#
module Elasticsearch
  module Model
    module Adapter
      module Ohm
        Adapter.register self,
                         lambda { |klass| defined?(::Ohm::Model) and klass.ancestors.include?(::Ohm::Model) }
        module Records
          def records
            klass.fetch(@ids)
          end
        end
      end
    end
  end
end

# Configure the Elasticsearch client to log operations
#
Elasticsearch::Model.client = Elasticsearch::Client.new log: true

puts '', '-'*Pry::Terminal.width!

Article.all.map { |a| a.delete }
Article.create id: '1', title: 'Foo'
Article.create id: '2', title: 'Bar'
Article.create id: '3', title: 'Foo Foo'

Article.__elasticsearch__.client.indices.delete index: 'articles' rescue nil
Article.__elasticsearch__.client.bulk index: 'articles',
                                      type:  'article',
                                      body:  Article.all.map { |a| { index: { _id: a.id, data: a.attributes } } },
                                      refresh: true


response = Article.search 'foo', index: 'articles', type: 'article';

Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
                   input: StringIO.new('response.records.to_a'),
                   quiet: true)

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
elasticsearch-model-queryable-0.1.9 examples/ohm_article.rb
elasticsearch-model-6.1.2 examples/ohm_article.rb
elasticsearch-model-queryable-0.1.5 examples/ohm_article.rb
elasticsearch-model-6.1.1 examples/ohm_article.rb
elasticsearch-model-6.1.0 examples/ohm_article.rb
elasticsearch-model-6.0.0 examples/ohm_article.rb
elasticsearch-model-6.0.0.pre examples/ohm_article.rb
elasticsearch-model-5.1.0 examples/ohm_article.rb
elasticsearch-model-5.0.2 examples/ohm_article.rb
elasticsearch-model-2.0.1 examples/ohm_article.rb
elasticsearch-model-5.0.1 examples/ohm_article.rb
elasticsearch-model-2.0.0 examples/ohm_article.rb
elasticsearch-model-5.0.0 examples/ohm_article.rb
elasticsearch-model-0.1.9 examples/ohm_article.rb
elasticsearch-model-0.1.8 examples/ohm_article.rb
elasticsearch-model-0.1.7 examples/ohm_article.rb
elasticsearch-model-0.1.6 examples/ohm_article.rb
elasticsearch-model-0.1.5 examples/ohm_article.rb
elasticsearch-model-0.1.4 examples/ohm_article.rb
elasticsearch-model-0.1.3 examples/ohm_article.rb