Skip to content

Commit ff9e664

Browse files
committed
[STORE] Added MyModel.all finder method
Person.all.to_a # 2014-05-05 15:02:24 +0200: GET http://localhost:9250/people/person/_search [status:200, request:0.047s, query:0.024s] # 2014-05-05 15:02:24 +0200: > {"query":{"match_all":{}},"size":10000} # 2014-05-05 15:02:24 +0200: < # {"took":24,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":100,"max_score":1.0,"hits":[ .... ]}} # => [#<Person:0x007ff1d8fb04b0 ... ]
1 parent ed0a552 commit ff9e664

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'elasticsearch/persistence'
77
require 'elasticsearch/persistence/model/errors'
88
require 'elasticsearch/persistence/model/store'
9+
require 'elasticsearch/persistence/model/find'
910

1011
module Elasticsearch
1112
module Persistence
@@ -45,6 +46,8 @@ def self.included(base)
4546
extend Elasticsearch::Persistence::Model::Store::ClassMethods
4647
include Elasticsearch::Persistence::Model::Store::InstanceMethods
4748

49+
extend Elasticsearch::Persistence::Model::Find::ClassMethods
50+
4851
class << self
4952
def attribute(name, type=nil, options={}, &block)
5053
mapping = options.delete(:mapping) || {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Elasticsearch
2+
module Persistence
3+
module Model
4+
5+
module Find
6+
module ClassMethods
7+
8+
# Return all documents (up to 10,000) for a model
9+
#
10+
# @example Retrieve all people
11+
#
12+
# Person.all
13+
# # => [#<Person:0x007ff1d8fb04b0 ... ]
14+
#
15+
# @example Retrieve all people matching a query
16+
#
17+
# Person.all query: { match: { last_name: 'Smith' } }
18+
# # => [#<Person:0x007ff1d8fb04b0 ... ]
19+
#
20+
def all(options={})
21+
gateway.search( { query: { match_all: {} }, size: 10_000 }.merge(options) )
22+
end
23+
end
24+
end
25+
26+
end
27+
end
28+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'test_helper'
2+
3+
require 'active_model'
4+
require 'virtus'
5+
6+
require 'elasticsearch/persistence/model/errors'
7+
require 'elasticsearch/persistence/model/find'
8+
9+
class Elasticsearch::Persistence::ModelFindTest < Test::Unit::TestCase
10+
context "The model find module," do
11+
12+
class DummyFindModel
13+
include ActiveModel::Naming
14+
include ActiveModel::Conversion
15+
include ActiveModel::Serialization
16+
include ActiveModel::Serializers::JSON
17+
include ActiveModel::Validations
18+
19+
include Virtus.model
20+
21+
extend Elasticsearch::Persistence::Model::Find::ClassMethods
22+
23+
extend ActiveModel::Callbacks
24+
define_model_callbacks :create, :save, :update, :destroy
25+
define_model_callbacks :find, :touch, only: :after
26+
27+
attribute :id, String, writer: :private
28+
attribute :title, String
29+
attribute :count, Integer, default: 0
30+
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
31+
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }
32+
33+
def set_id(id); self.id = id; end
34+
end
35+
36+
setup do
37+
@gateway = stub
38+
DummyFindModel.stubs(:gateway).returns(@gateway)
39+
40+
@response = MultiJson.load <<-JSON
41+
{
42+
"took": 14,
43+
"timed_out": false,
44+
"_shards": {
45+
"total": 5,
46+
"successful": 5,
47+
"failed": 0
48+
},
49+
"hits": {
50+
"total": 1,
51+
"max_score": 1.0,
52+
"hits": [
53+
{
54+
"_index": "dummy",
55+
"_type": "dummy",
56+
"_id": "abc123",
57+
"_score": 1.0,
58+
"_source": {
59+
"name": "TEST"
60+
}
61+
}
62+
]
63+
}
64+
}
65+
JSON
66+
end
67+
68+
should "find all records" do
69+
@gateway
70+
.expects(:search)
71+
.with({ query: { match_all: {} }, size: 10_000 })
72+
.returns(@response)
73+
74+
DummyFindModel.all
75+
end
76+
77+
should "pass options when finding all records" do
78+
@gateway
79+
.expects(:search)
80+
.with({ query: { match: { title: 'test' } }, size: 10_000, routing: 'abc123' })
81+
.returns(@response)
82+
83+
DummyFindModel.all( { query: { match: { title: 'test' } }, routing: 'abc123' } )
84+
end
85+
86+
end
87+
end

0 commit comments

Comments
 (0)