This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
/
index_update_document_test.rb
111 lines (82 loc) · 3.92 KB
/
index_update_document_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'test_helper'
module Tire
class IndexUpdateDocumentIntegrationTest < Test::Unit::TestCase
include Test::Integration
context "Updating a document" do
setup do
Tire.index 'articles-with-tags' do
delete
create
store :type => 'article', :id => 1, :title => 'One', :tags => ['foo'], :views => 0
store :type => 'article', :id => 2, :title => 'Two', :tags => ['foo', 'bar'], :views => 10
store :type => 'article', :id => 3, :title => 'Three', :tags => ['foobar']
refresh
end
end
teardown { Tire.index('articles-with-tags').delete }
should "increment a counter" do
Tire.index('articles-with-tags') { update( 'article', '1', :script => "ctx._source.views += 1" ) and refresh }
document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
assert_equal 1, document.views, document.inspect
Tire.index('articles-with-tags') { update( 'article', '2', :script => "ctx._source.views += 1" ) and refresh }
document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
assert_equal 11, document.views, document.inspect
end
should "add a tag to document" do
Tire.index('articles-with-tags') do
update 'article', '1', :script => "ctx._source.tags += tag",
:params => { :tag => 'new' }
refresh
end
document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
assert_equal ['foo', 'new'], document.tags, document.inspect
end
should "remove a tag from document" do
Tire.index('articles-with-tags') do
update 'article', '1', :script => "ctx._source.tags = tags",
:params => { :tags => [] }
refresh
end
document = Tire.index('articles-with-tags').retrieve 'article', '1'
assert_equal [], document.tags, document.inspect
end
should "remove the entire document if specific condition is met" do
Tire.index('articles-with-tags') do
# Remove document when it contains tag 'foobar'
update 'article', '3', :script => "ctx._source.tags.contains(tag) ? ctx.op = 'delete' : 'none'",
:params => { :tag => 'foobar' }
refresh
end
assert_nil Tire.index('articles-with-tags').retrieve 'article', '3'
end
should "pass the operation parameters to the API" do
Tire.index('articles-with-tags').update 'article', '2', { :script => "ctx._source.tags += tag",
:params => { :tag => 'new' }
},
{
:refresh => true
}
document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
assert_equal 3, document.tags.size, document.inspect
end
should "access variables from the outer scope" do
$t = self
class Updater
@tags = ['foo', 'bar', 'baz']
def self.perform_update!
$t.assert_not_nil @tags
Tire.index('articles-with-tags') do |index|
$t.assert_not_nil @tags
index.update 'article', '3', :script => "ctx._source.tags = tags",
:params => { :tags => @tags }
index.refresh
end
end
end
Updater.perform_update!
document = Tire.search('articles-with-tags') { query { string 'title:three' } }.results.first
assert_equal 3, document.tags.size, document.inspect
end
end
end
end