dilute 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,8 +20,10 @@ module Dilute::Modelize
20
20
  end
21
21
 
22
22
  def all(query = {}, page_size = 10)
23
- search_results = type.search(size: page_size, query: {match_all: {}})
24
- search_results.raw["hits"]["hits"].collect {|r| new(r) }
23
+
24
+ # search_results = type.search(size: page_size, query: {match_all: {}})
25
+ # search_results.raw["hits"]["hits"].collect {|r| new(r) }
26
+ Dilute::Query.new(self)
25
27
  end
26
28
 
27
29
  def define_type(options = {}, &blk)
@@ -0,0 +1,45 @@
1
+ class Dilute::Query
2
+ attr_reader :for_class, :options, :chained, :results, :from_elastic_search
3
+ include Enumerable
4
+
5
+ def initialize(for_class, options = {}, chained = true)
6
+ @for_class, @options, @chained = for_class, options, chained
7
+ end
8
+
9
+ def match(q)
10
+ merge_or_chain(:match, q)
11
+ end
12
+
13
+ def to_query
14
+ query = options.reject {|k,v| v.nil? || v.empty? }
15
+ query[:match_all] ||= {} unless query[:match]
16
+ query
17
+ end
18
+
19
+ def execute!
20
+ @results ||= begin
21
+ search_results = for_class.type.search(size: 25, query: to_query)
22
+ @from_elastic_search = search_results
23
+ search_results.raw["hits"]["hits"].collect {|r| for_class.new(r) }
24
+ end
25
+ end
26
+
27
+ def to_a
28
+ execute!
29
+ results
30
+ end
31
+
32
+ def each(*args, &block)
33
+ return enum_for(__callee__) unless block_given?
34
+ to_a.each(*args, &block)
35
+ end
36
+
37
+ private
38
+
39
+ def merge_or_chain(key, to_merge)
40
+ new_options = chained ? options.dup : options
41
+ new_options[key] ||= {}
42
+ new_options[key].merge!(to_merge)
43
+ chained ? self.class.new(for_class, new_options) : new_options[key]
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Dilute
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,15 +1,16 @@
1
1
  require_relative "spec_helper"
2
2
  require "pry"
3
+ require 'pp'
3
4
 
4
5
  describe Dilute::Modelize do
5
6
  let(:dilute_class) do
6
7
  Class.new do
7
8
  include Dilute::Modelize
8
9
 
9
- define_type(index_name: :dilute_test_suite, type_name: :abstract) do
10
+ define_type(type_name: :abstract) do
10
11
  attribute(:data).not_indexed
11
12
  attribute(:header).not_indexed
12
- attribute(:keys).index_analyzer("keyword")
13
+ attribute(:keys).not_indexed# .index_analyzer("keyword")
13
14
  attribute(:created_at, :date)
14
15
  end
15
16
  end
@@ -33,15 +34,38 @@ describe Dilute::Modelize do
33
34
  expect(dilute_class.find(saved.id).data).to eq(test_data)
34
35
  end
35
36
 
36
- it "should retreive many saved documents" do
37
- destroy_test_index!
38
- 10.times { dilute_class.new({data: test_data}).save }
39
- sleep 1
40
- expect(dilute_class.all({}, 5)).to have(5).items
41
- end
42
-
43
37
  it "I have no idea what I'm doing." do
44
38
  true
39
+ # That's why you see the sleep 1s and such.
40
+ end
41
+
42
+ context "searching" do
43
+ before { destroy_test_index! }
44
+ it "should find docs" do
45
+ destroy_test_index!
46
+ saved
47
+ sleep 1
48
+ all = dilute_class.all
49
+ expect(all.to_a).to have(1).item
50
+ expect(all.first.data).to eq(test_data)
51
+ end
52
+
53
+ it "should find docs matching pattern" do
54
+ dilute_class.new({keys: %w(taga tagb tagc)}).save
55
+ dilute_class.new({keys: %w(tagb tagc)}).save
56
+ sleep 1
57
+ search = dilute_class.all.match(keys: "taga")
58
+ expect(search.to_a).to have(1).item
59
+ end
60
+
61
+ it "should find docs matching very short word" do
62
+ dilute_class.new({keys: %w(a)}).save
63
+ dilute_class.new({keys: %w(b)}).save
64
+ sleep 1
65
+ search = dilute_class.all.match(keys: "a")
66
+ expect(search.to_a).to have(1).item
67
+ end
68
+
45
69
  end
46
70
 
47
71
  context "infering the index name" do
@@ -63,8 +87,6 @@ describe Dilute::Modelize do
63
87
  expect(new_diluted.attributes["_index"]).to eq "default_dilute_index_name"
64
88
  expect(new_diluted.attributes["_type"]).to eq "test_name_for_dilutes"
65
89
  end
66
-
67
-
68
90
  end
69
91
 
70
92
  end
@@ -0,0 +1,20 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe Dilute::Query do
4
+
5
+ let(:model_class) { Class.new }
6
+ let(:query) { Dilute::Query.new(model_class) }
7
+
8
+ it "should create new" do
9
+ expect(query).to be_kind_of Dilute::Query
10
+ end
11
+
12
+ it "should chain match queries" do
13
+ expect(query.match({foo: "bar"})).to be_kind_of Dilute::Query
14
+ end
15
+
16
+ it "should have updated options in chained query" do
17
+ expect(query.match({foo: "bar"}).options[:match]).to eq({foo: "bar"})
18
+ end
19
+
20
+ end
data/spec/spec_helper.rb CHANGED
@@ -22,5 +22,9 @@ def destroy_test_index!
22
22
  require "stretcher"
23
23
  server = Stretcher::Server.new("http://localhost:9200", log_level: :info)
24
24
  index = server.index("default_dilute_index_name")
25
- index.delete
25
+ begin
26
+ index.delete
27
+ rescue Stretcher::RequestError::NotFound
28
+ # Index didn't exist to begin with.
29
+ end
26
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dilute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-24 00:00:00.000000000 Z
12
+ date: 2013-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stretcher
@@ -142,9 +142,11 @@ files:
142
142
  - lib/dilute.rb
143
143
  - lib/dilute/attribute.rb
144
144
  - lib/dilute/modelize.rb
145
+ - lib/dilute/query.rb
145
146
  - lib/dilute/type.rb
146
147
  - lib/dilute/version.rb
147
148
  - spec/modelize_integration_spec.rb
149
+ - spec/query_spec.rb
148
150
  - spec/spec_helper.rb
149
151
  homepage: ''
150
152
  licenses:
@@ -159,12 +161,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
161
  - - ! '>='
160
162
  - !ruby/object:Gem::Version
161
163
  version: '0'
164
+ segments:
165
+ - 0
166
+ hash: 3890611216473297716
162
167
  required_rubygems_version: !ruby/object:Gem::Requirement
163
168
  none: false
164
169
  requirements:
165
170
  - - ! '>='
166
171
  - !ruby/object:Gem::Version
167
172
  version: '0'
173
+ segments:
174
+ - 0
175
+ hash: 3890611216473297716
168
176
  requirements: []
169
177
  rubyforge_project:
170
178
  rubygems_version: 1.8.23
@@ -173,5 +181,5 @@ specification_version: 3
173
181
  summary: An ActiveRecord-ish thing for using ElasticSearch through Stretcher.
174
182
  test_files:
175
183
  - spec/modelize_integration_spec.rb
184
+ - spec/query_spec.rb
176
185
  - spec/spec_helper.rb
177
- has_rdoc: