elastic_record 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '0.6.0'
5
+ s.version = '0.6.1'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -39,11 +39,26 @@ module ElasticRecord
39
39
  connection.json_get url, elastic_query
40
40
  end
41
41
 
42
+ def explain(id, elastic_query)
43
+ connection.json_get "/#{alias_name}/#{type}/#{id}/_explain", elastic_query
44
+ end
45
+
42
46
  def scroll(scroll_id, scroll_keep_alive)
43
47
  options = {scroll_id: scroll_id, scroll: scroll_keep_alive}
44
48
  connection.json_get("/_search/scroll?#{options.to_query}")
45
49
  end
46
50
 
51
+ def bulk
52
+ @batch = []
53
+ yield
54
+ if @batch.any?
55
+ body = @batch.map { |action| "#{ActiveSupport::JSON.encode(action)}\n" }.join
56
+ connection.json_post "/_bulk", body
57
+ end
58
+ ensure
59
+ @batch = nil
60
+ end
61
+
47
62
  def bulk_add(batch, index_name = nil)
48
63
  return if disabled
49
64
 
@@ -55,17 +70,6 @@ module ElasticRecord
55
70
  end
56
71
  end
57
72
  end
58
-
59
- def bulk
60
- @batch = []
61
- yield
62
- if @batch.any?
63
- body = @batch.map { |action| "#{ActiveSupport::JSON.encode(action)}\n" }.join
64
- connection.json_post "/_bulk", body
65
- end
66
- ensure
67
- @batch = nil
68
- end
69
73
  end
70
74
  end
71
75
  end
@@ -2,6 +2,7 @@ module ElasticRecord
2
2
  class Index
3
3
  module Percolator
4
4
  def create_percolator(name, elastic_query)
5
+ p "creating #{elastic_query.inspect}"
5
6
  unless exists? percolator_index_name
6
7
  create percolator_index_name
7
8
  else
@@ -20,9 +21,14 @@ module ElasticRecord
20
21
  end
21
22
 
22
23
  def percolate(document)
24
+ # p "document = #{document}"
23
25
  connection.json_get("/#{percolator_index_name}/#{type}/_percolate", 'doc' => document)['matches']
24
26
  end
25
27
 
28
+ def reset_percolator
29
+ delete(percolator_index_name) if exists?(percolator_index_name)
30
+ end
31
+
26
32
  def percolator_index_name
27
33
  @percolator_index_name ||= "percolate_#{alias_name}"
28
34
  end
@@ -29,8 +29,8 @@ module ElasticRecord
29
29
  klass.elastic_index.create_percolator(name, as_elastic)
30
30
  end
31
31
 
32
- def reset
33
- @search_results = @records = nil
32
+ def explain(id)
33
+ klass.elastic_index.explain(id, as_elastic)
34
34
  end
35
35
 
36
36
  def initialize_copy(other)
@@ -70,6 +70,10 @@ module ElasticRecord
70
70
  end
71
71
 
72
72
  private
73
+ def reset
74
+ @search_results = @records = nil
75
+ end
76
+
73
77
  def search_hits
74
78
  search_results['hits']['hits']
75
79
  end
@@ -6,20 +6,20 @@ class ElasticRecord::RelationTest < MiniTest::Spec
6
6
  end
7
7
 
8
8
  def test_count
9
- create_widgets
9
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
10
10
 
11
11
  assert_equal 2, Widget.elastic_relation.count
12
12
  end
13
13
 
14
14
  def test_facets
15
- create_widgets
15
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
16
16
 
17
17
  facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets
18
18
 
19
19
  assert_equal 2, facets['popular_colors']['total']
20
20
  end
21
21
 
22
- def test_percolate
22
+ def test_create_percolator
23
23
  Widget.elastic_relation.filter(color: 'green').create_percolator('green')
24
24
  Widget.elastic_relation.filter(color: 'blue').create_percolator('blue')
25
25
  widget = Widget.new(color: 'green')
@@ -27,17 +27,26 @@ class ElasticRecord::RelationTest < MiniTest::Spec
27
27
  assert_equal ['green'], Widget.elastic_index.percolate(widget.as_search)
28
28
  end
29
29
 
30
+ def test_explain
31
+ create_widgets [Widget.new(id: 10, color: 'blue')]
32
+
33
+ # explain = Widget.elastic_relation.filter(color: 'blue').explain('10')
34
+ # p "explain = #{explain}"
35
+ end
36
+
30
37
  def test_to_hits
31
38
  # assert Widget.elastic_relation.search_results.is_a?(ElasticSearch::Api::Hits)
32
39
  end
33
40
 
34
41
  def test_to_ids
35
- create_widgets
42
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
43
+
36
44
  assert_equal ['5', '10'], Widget.elastic_relation.to_ids
37
45
  end
38
46
 
39
47
  def test_to_a
40
- create_widgets
48
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
49
+
41
50
  array = Widget.elastic_relation.to_a
42
51
 
43
52
  assert_equal 2, array.size
@@ -45,7 +54,7 @@ class ElasticRecord::RelationTest < MiniTest::Spec
45
54
  end
46
55
 
47
56
  def test_equal
48
- create_widgets
57
+ create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]
49
58
 
50
59
  assert(Widget.elastic_relation.filter(color: 'green') == Widget.elastic_relation.filter(color: 'green'))
51
60
  assert(Widget.elastic_relation.filter(color: 'green') != Widget.elastic_relation.filter(color: 'blue'))
@@ -58,12 +67,8 @@ class ElasticRecord::RelationTest < MiniTest::Spec
58
67
  end
59
68
 
60
69
  private
61
- def create_widgets
62
- Widget.elastic_index.bulk_add [
63
- Widget.new(id: 5, color: 'red'),
64
- Widget.new(id: 10, color: 'blue')
65
- ]
66
-
70
+ def create_widgets(widgets)
71
+ Widget.elastic_index.bulk_add(widgets)
67
72
  Widget.elastic_index.refresh
68
73
  end
69
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
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: 2012-09-10 00:00:00.000000000 Z
12
+ date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic