Skip to content

Commit d6fe949

Browse files
committedJan 18, 2014
[MODEL] Added examples for ActiveRecord, CouchBase, DataMapper, Mongoid, Ohm and Riak models
1 parent b363c58 commit d6fe949

File tree

6 files changed

+397
-0
lines changed

6 files changed

+397
-0
lines changed
 

Diff for: ‎elasticsearch-model/examples/activerecord_article.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ActiveRecord and Elasticsearch
2+
# ==============================
3+
#
4+
# https://github.com/rails/rails/tree/master/activerecord
5+
6+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7+
8+
require 'pry'
9+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
10+
11+
require 'logger'
12+
require 'ansi/core'
13+
require 'active_record'
14+
15+
require 'elasticsearch/model'
16+
17+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
18+
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
19+
20+
ActiveRecord::Schema.define(version: 1) do
21+
create_table :articles do |t|
22+
t.string :title
23+
t.date :published_at
24+
t.timestamps
25+
end
26+
end
27+
28+
class Article < ActiveRecord::Base
29+
end
30+
31+
# Store data
32+
#
33+
Article.delete_all
34+
Article.create title: 'Foo'
35+
Article.create title: 'Bar'
36+
Article.create title: 'Foo Foo'
37+
38+
# Index data
39+
#
40+
client = Elasticsearch::Client.new log:true
41+
42+
# client.indices.delete index: 'articles' rescue nil
43+
# client.indices.create index: 'articles', body: { mappings: { article: { dynamic: 'strict' }, properties: {} } }
44+
45+
client.indices.delete index: 'articles' rescue nil
46+
client.bulk index: 'articles',
47+
type: 'article',
48+
body: Article.all.as_json.map { |a| { index: { _id: a.delete('id'), data: a } } },
49+
refresh: true
50+
51+
# Extend the model with Elasticsearch support
52+
#
53+
Article.__send__ :include, Elasticsearch::Model
54+
# Article.__send__ :include, Elasticsearch::Model::Callbacks
55+
56+
# ActiveRecord::Base.logger.silence do
57+
# 10_000.times do |i|
58+
# Article.create title: "Foo #{i}"
59+
# end
60+
# end
61+
62+
puts '', '-'*Pry::Terminal.width!
63+
64+
Elasticsearch::Model.client Elasticsearch::Client.new log: true
65+
66+
response = Article.search 'foo';
67+
68+
p response.size
69+
p response.results.size
70+
p response.records.size
71+
72+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
73+
input: StringIO.new('response.records.to_a'),
74+
quiet: true)

Diff for: ‎elasticsearch-model/examples/couchbase_article.rb

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Couchbase and Elasticsearch
2+
# ===========================
3+
#
4+
# https://github.com/couchbase/couchbase-ruby-model
5+
6+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7+
8+
require 'pry'
9+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
10+
11+
require 'logger'
12+
require 'couchbase/model'
13+
14+
require 'elasticsearch/model'
15+
16+
# Documents are stored as JSON objects in Riak but have rich
17+
# semantics, including validations and associations.
18+
class Article < Couchbase::Model
19+
attribute :title
20+
attribute :published_at
21+
22+
# view :all, :limit => 10, :descending => true
23+
# TODO: Implement view a la
24+
# bucket.save_design_doc <<-JSON
25+
# {
26+
# "_id": "_design/article",
27+
# "language": "javascript",
28+
# "views": {
29+
# "all": {
30+
# "map": "function(doc, meta) { emit(doc.id, doc.title); }"
31+
# }
32+
# }
33+
# }
34+
# JSON
35+
36+
end
37+
38+
# Extend the model with Elasticsearch support
39+
#
40+
Article.__send__ :extend, Elasticsearch::Model::Client::ClassMethods
41+
Article.__send__ :extend, Elasticsearch::Model::Searching::ClassMethods
42+
Article.__send__ :extend, Elasticsearch::Model::Naming::ClassMethods
43+
44+
# Create documents in Riak
45+
#
46+
Article.create id: '1', title: 'Foo' rescue nil
47+
Article.create id: '2', title: 'Bar' rescue nil
48+
Article.create id: '3', title: 'Foo Foo' rescue nil
49+
50+
# Index data into Elasticsearch
51+
#
52+
client = Elasticsearch::Client.new log:true
53+
54+
client.indices.delete index: 'articles' rescue nil
55+
client.bulk index: 'articles',
56+
type: 'article',
57+
body: Article.find(['1', '2', '3']).map { |a|
58+
{ index: { _id: a.id, data: a.attributes } }
59+
},
60+
refresh: true
61+
62+
response = Article.search 'foo', index: 'articles', type: 'article';
63+
64+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
65+
input: StringIO.new('response.records.to_a'),
66+
quiet: true)

Diff for: ‎elasticsearch-model/examples/datamapper_article.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# DataMapper and Elasticsearch
2+
# ============================
3+
#
4+
# https://github.com/datamapper/dm-core
5+
# https://github.com/datamapper/dm-active_model
6+
7+
8+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
9+
10+
require 'pry'
11+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
12+
13+
require 'logger'
14+
require 'ansi/core'
15+
16+
require 'data_mapper'
17+
require 'dm-active_model'
18+
19+
require 'active_support/all'
20+
21+
require 'elasticsearch/model'
22+
23+
DataMapper::Logger.new(STDOUT, :debug)
24+
DataMapper.setup(:default, 'sqlite::memory:')
25+
26+
class Article
27+
include DataMapper::Resource
28+
29+
property :id, Serial
30+
property :title, String
31+
property :published_at, DateTime
32+
end
33+
34+
DataMapper.auto_migrate!
35+
DataMapper.finalize
36+
37+
Article.create title: 'Foo'
38+
Article.create title: 'Bar'
39+
Article.create title: 'Foo Foo'
40+
41+
# Extend the model with Elasticsearch support
42+
#
43+
Article.__send__ :include, Elasticsearch::Model
44+
45+
# The DataMapper adapter
46+
#
47+
module DataMapperAdapter
48+
49+
# Implement the interface for fetching records
50+
#
51+
module Records
52+
def records
53+
klass.all(id: @ids)
54+
end
55+
56+
# ...
57+
end
58+
end
59+
60+
# Register the adapter
61+
#
62+
Elasticsearch::Model::Adapter.register(
63+
DataMapperAdapter,
64+
lambda { |klass| defined?(::DataMapper::Resource) and klass.ancestors.include?(::DataMapper::Resource) }
65+
)
66+
67+
response = Article.search 'foo';
68+
69+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
70+
input: StringIO.new('response.records.to_a'),
71+
quiet: true)

Diff for: ‎elasticsearch-model/examples/mongoid_article.rb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Mongoid and Elasticsearch
2+
# =========================
3+
#
4+
# http://mongoid.org/en/mongoid/index.html
5+
6+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7+
8+
require 'pry'
9+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
10+
11+
require 'benchmark'
12+
require 'logger'
13+
require 'ansi/core'
14+
require 'mongoid'
15+
16+
require 'elasticsearch/model'
17+
require 'elasticsearch/model/callbacks'
18+
19+
Mongoid.logger.level = Logger::DEBUG
20+
Moped.logger.level = Logger::DEBUG
21+
22+
Mongoid.connect_to 'articles'
23+
24+
Elasticsearch::Model.client Elasticsearch::Client.new log: true
25+
26+
class Article
27+
include Mongoid::Document
28+
field :id, type: String
29+
field :title, type: String
30+
field :published_at, type: DateTime
31+
attr_accessible :id, :title, :published_at
32+
end
33+
34+
# Extend the model with Elasticsearch support
35+
#
36+
Article.__send__ :include, Elasticsearch::Model
37+
# Article.__send__ :include, Elasticsearch::Model::Callbacks
38+
39+
# Store data
40+
#
41+
Article.delete_all
42+
Article.create id: '1', title: 'Foo'
43+
Article.create id: '2', title: 'Bar'
44+
Article.create id: '3', title: 'Foo Foo'
45+
46+
# Index data
47+
#
48+
client = Elasticsearch::Client.new log:true
49+
50+
client.indices.delete index: 'articles' rescue nil
51+
client.bulk index: 'articles',
52+
type: 'article',
53+
body: Article.all.map { |a| { index: { _id: a.id, data: a.attributes } } },
54+
refresh: true
55+
56+
# puts Benchmark.realtime { 9_875.times { |i| Article.create title: "Foo #{i}" } }
57+
58+
puts '', '-'*Pry::Terminal.width!
59+
60+
response = Article.search 'foo';
61+
62+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
63+
input: StringIO.new('response.records.to_a'),
64+
quiet: true)

Diff for: ‎elasticsearch-model/examples/ohm_article.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Ohm for Redis and Elasticsearch
2+
# ===============================
3+
#
4+
# https://github.com/soveran/ohm#example
5+
6+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7+
8+
require 'pry'
9+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
10+
11+
require 'logger'
12+
require 'ansi/core'
13+
require 'active_model'
14+
require 'ohm'
15+
16+
require 'elasticsearch/model'
17+
18+
class Article < Ohm::Model
19+
# Include JSON serialization from ActiveModel
20+
include ActiveModel::Serializers::JSON
21+
22+
attribute :title
23+
attribute :published_at
24+
end
25+
26+
# Extend the model with Elasticsearch support
27+
#
28+
Article.__send__ :include, Elasticsearch::Model
29+
30+
# Register a custom adapter
31+
#
32+
module Elasticsearch
33+
module Model
34+
module Adapter
35+
module Ohm
36+
Adapter.register self,
37+
lambda { |klass| defined?(::Ohm::Model) and klass.ancestors.include?(::Ohm::Model) }
38+
module Records
39+
def records
40+
klass.fetch(@ids)
41+
end
42+
end
43+
end
44+
end
45+
end
46+
end
47+
48+
# Configure the Elasticsearch client to log operations
49+
#
50+
Elasticsearch::Model.client Elasticsearch::Client.new log: true
51+
52+
puts '', '-'*Pry::Terminal.width!
53+
54+
Article.all.map { |a| a.delete }
55+
Article.create id: '1', title: 'Foo'
56+
Article.create id: '2', title: 'Bar'
57+
Article.create id: '3', title: 'Foo Foo'
58+
59+
Article.__elasticsearch__.client.indices.delete index: 'articles' rescue nil
60+
Article.__elasticsearch__.client.bulk index: 'articles',
61+
type: 'article',
62+
body: Article.all.map { |a| { index: { _id: a.id, data: a.attributes } } },
63+
refresh: true
64+
65+
66+
response = Article.search 'foo', index: 'articles', type: 'article';
67+
68+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
69+
input: StringIO.new('response.records.to_a'),
70+
quiet: true)

Diff for: ‎elasticsearch-model/examples/riak_article.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Riak and Elasticsearch
2+
# ======================
3+
#
4+
# https://github.com/basho-labs/ripple
5+
6+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7+
8+
require 'pry'
9+
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
10+
11+
require 'logger'
12+
require 'ripple'
13+
14+
require 'elasticsearch/model'
15+
16+
# Documents are stored as JSON objects in Riak but have rich
17+
# semantics, including validations and associations.
18+
class Article
19+
include Ripple::Document
20+
21+
property :title, String
22+
property :published_at, Time, :default => proc { Time.now }
23+
end
24+
25+
# Extend the model with Elasticsearch support
26+
#
27+
Article.__send__ :include, Elasticsearch::Model
28+
29+
# Create documents in Riak
30+
#
31+
Article.destroy_all
32+
Article.create id: '1', title: 'Foo'
33+
Article.create id: '2', title: 'Bar'
34+
Article.create id: '3', title: 'Foo Foo'
35+
36+
# Index data into Elasticsearch
37+
#
38+
client = Elasticsearch::Client.new log:true
39+
40+
client.indices.delete index: 'articles' rescue nil
41+
client.bulk index: 'articles',
42+
type: 'article',
43+
body: Article.all.map { |a|
44+
{ index: { _id: a.key, data: JSON.parse(a.robject.raw_data) } }
45+
}.as_json,
46+
refresh: true
47+
48+
response = Article.search 'foo';
49+
50+
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
51+
input: StringIO.new('response.records.to_a'),
52+
quiet: true)

0 commit comments

Comments
 (0)
Please sign in to comment.