tire-erez 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.travis.yml +32 -0
- data/.yardopts +1 -0
- data/Gemfile +10 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +775 -0
- data/Rakefile +51 -0
- data/examples/rails-application-template.rb +263 -0
- data/examples/tire-dsl.rb +932 -0
- data/lib/tire.rb +59 -0
- data/lib/tire/alias.rb +296 -0
- data/lib/tire/configuration.rb +38 -0
- data/lib/tire/count.rb +85 -0
- data/lib/tire/dsl.rb +114 -0
- data/lib/tire/http/client.rb +62 -0
- data/lib/tire/http/clients/curb.rb +61 -0
- data/lib/tire/http/clients/faraday.rb +71 -0
- data/lib/tire/http/response.rb +27 -0
- data/lib/tire/index.rb +443 -0
- data/lib/tire/logger.rb +60 -0
- data/lib/tire/model/callbacks.rb +40 -0
- data/lib/tire/model/import.rb +27 -0
- data/lib/tire/model/indexing.rb +134 -0
- data/lib/tire/model/naming.rb +100 -0
- data/lib/tire/model/percolate.rb +99 -0
- data/lib/tire/model/persistence.rb +72 -0
- data/lib/tire/model/persistence/attributes.rb +148 -0
- data/lib/tire/model/persistence/finders.rb +54 -0
- data/lib/tire/model/persistence/storage.rb +77 -0
- data/lib/tire/model/search.rb +322 -0
- data/lib/tire/multi_search.rb +263 -0
- data/lib/tire/results/collection.rb +156 -0
- data/lib/tire/results/item.rb +94 -0
- data/lib/tire/results/pagination.rb +68 -0
- data/lib/tire/rubyext/hash.rb +8 -0
- data/lib/tire/rubyext/ruby_1_8.rb +1 -0
- data/lib/tire/rubyext/symbol.rb +11 -0
- data/lib/tire/rubyext/uri_escape.rb +74 -0
- data/lib/tire/search.rb +211 -0
- data/lib/tire/search/facet.rb +81 -0
- data/lib/tire/search/filter.rb +28 -0
- data/lib/tire/search/highlight.rb +37 -0
- data/lib/tire/search/queries/match.rb +40 -0
- data/lib/tire/search/query.rb +250 -0
- data/lib/tire/search/scan.rb +114 -0
- data/lib/tire/search/script_field.rb +23 -0
- data/lib/tire/search/sort.rb +25 -0
- data/lib/tire/tasks.rb +138 -0
- data/lib/tire/utils.rb +17 -0
- data/lib/tire/version.rb +18 -0
- data/test/fixtures/articles/1.json +1 -0
- data/test/fixtures/articles/2.json +1 -0
- data/test/fixtures/articles/3.json +1 -0
- data/test/fixtures/articles/4.json +1 -0
- data/test/fixtures/articles/5.json +1 -0
- data/test/integration/active_model_indexing_test.rb +51 -0
- data/test/integration/active_model_searchable_test.rb +114 -0
- data/test/integration/active_record_searchable_test.rb +620 -0
- data/test/integration/boolean_queries_test.rb +43 -0
- data/test/integration/boosting_queries_test.rb +32 -0
- data/test/integration/bulk_test.rb +86 -0
- data/test/integration/count_test.rb +64 -0
- data/test/integration/custom_score_queries_test.rb +89 -0
- data/test/integration/dis_max_queries_test.rb +68 -0
- data/test/integration/dsl_search_test.rb +30 -0
- data/test/integration/explanation_test.rb +44 -0
- data/test/integration/facets_test.rb +311 -0
- data/test/integration/filtered_queries_test.rb +66 -0
- data/test/integration/filters_test.rb +75 -0
- data/test/integration/fuzzy_queries_test.rb +20 -0
- data/test/integration/highlight_test.rb +64 -0
- data/test/integration/index_aliases_test.rb +122 -0
- data/test/integration/index_mapping_test.rb +43 -0
- data/test/integration/index_store_test.rb +112 -0
- data/test/integration/index_update_document_test.rb +121 -0
- data/test/integration/match_query_test.rb +79 -0
- data/test/integration/mongoid_searchable_test.rb +309 -0
- data/test/integration/multi_search_test.rb +114 -0
- data/test/integration/nested_query_test.rb +135 -0
- data/test/integration/percolator_test.rb +111 -0
- data/test/integration/persistent_model_test.rb +205 -0
- data/test/integration/prefix_query_test.rb +43 -0
- data/test/integration/query_return_version_test.rb +70 -0
- data/test/integration/query_string_test.rb +52 -0
- data/test/integration/range_queries_test.rb +36 -0
- data/test/integration/reindex_test.rb +56 -0
- data/test/integration/results_test.rb +58 -0
- data/test/integration/scan_test.rb +56 -0
- data/test/integration/script_fields_test.rb +38 -0
- data/test/integration/sort_test.rb +52 -0
- data/test/integration/text_query_test.rb +39 -0
- data/test/models/active_model_article.rb +31 -0
- data/test/models/active_model_article_with_callbacks.rb +49 -0
- data/test/models/active_model_article_with_custom_document_type.rb +7 -0
- data/test/models/active_model_article_with_custom_index_name.rb +7 -0
- data/test/models/active_record_models.rb +131 -0
- data/test/models/article.rb +15 -0
- data/test/models/mongoid_models.rb +85 -0
- data/test/models/persistent_article.rb +11 -0
- data/test/models/persistent_article_in_index.rb +16 -0
- data/test/models/persistent_article_in_namespace.rb +12 -0
- data/test/models/persistent_article_with_casting.rb +28 -0
- data/test/models/persistent_article_with_defaults.rb +12 -0
- data/test/models/persistent_article_with_percolation.rb +5 -0
- data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
- data/test/models/supermodel_article.rb +17 -0
- data/test/models/validated_model.rb +11 -0
- data/test/test_helper.rb +118 -0
- data/test/unit/active_model_lint_test.rb +17 -0
- data/test/unit/configuration_test.rb +84 -0
- data/test/unit/count_test.rb +67 -0
- data/test/unit/http_client_test.rb +79 -0
- data/test/unit/http_response_test.rb +49 -0
- data/test/unit/index_alias_test.rb +335 -0
- data/test/unit/index_test.rb +1098 -0
- data/test/unit/logger_test.rb +125 -0
- data/test/unit/model_callbacks_test.rb +116 -0
- data/test/unit/model_import_test.rb +75 -0
- data/test/unit/model_initialization_test.rb +31 -0
- data/test/unit/model_persistence_test.rb +548 -0
- data/test/unit/model_search_test.rb +964 -0
- data/test/unit/multi_search_test.rb +304 -0
- data/test/unit/results_collection_test.rb +372 -0
- data/test/unit/results_item_test.rb +173 -0
- data/test/unit/rubyext_test.rb +66 -0
- data/test/unit/search_facet_test.rb +186 -0
- data/test/unit/search_filter_test.rb +42 -0
- data/test/unit/search_highlight_test.rb +46 -0
- data/test/unit/search_query_test.rb +419 -0
- data/test/unit/search_scan_test.rb +113 -0
- data/test/unit/search_script_field_test.rb +26 -0
- data/test/unit/search_sort_test.rb +50 -0
- data/test/unit/search_test.rb +556 -0
- data/test/unit/tire_test.rb +144 -0
- data/tire.gemspec +83 -0
- metadata +586 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire
|
4
|
+
|
5
|
+
class FuzzyQueryIntegrationTest < Test::Unit::TestCase
|
6
|
+
include Test::Integration
|
7
|
+
|
8
|
+
context "Fuzzy query" do
|
9
|
+
should "fuzzily find article by tag" do
|
10
|
+
results = Tire.search('articles-test') { query { fuzzy :tags, 'irlang' } }.results
|
11
|
+
|
12
|
+
assert_equal 1, results.count
|
13
|
+
assert_equal ["erlang"], results.first[:tags]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire
|
4
|
+
|
5
|
+
class HighlightIntegrationTest < Test::Unit::TestCase
|
6
|
+
include Test::Integration
|
7
|
+
|
8
|
+
context "Highlight" do
|
9
|
+
teardown { Tire.index('highlight-test').delete }
|
10
|
+
|
11
|
+
should "add 'highlight' field to the result item" do
|
12
|
+
# Tire::Configuration.logger STDERR, :level => 'debug'
|
13
|
+
s = Tire.search('articles-test') do
|
14
|
+
query { string 'Two' }
|
15
|
+
highlight :title
|
16
|
+
end
|
17
|
+
|
18
|
+
doc = s.results.first
|
19
|
+
|
20
|
+
assert_equal 1, doc.highlight.title.size
|
21
|
+
assert doc.highlight.title.to_s.include?('<em>'), "Highlight does not include default highlight tag"
|
22
|
+
end
|
23
|
+
|
24
|
+
should "highlight multiple fields with custom highlight tag" do
|
25
|
+
s = Tire.search('articles-test') do
|
26
|
+
query { string 'Two OR ruby' }
|
27
|
+
highlight :tags, :title, :options => { :tag => '<strong>' }
|
28
|
+
end
|
29
|
+
|
30
|
+
doc = s.results.first
|
31
|
+
|
32
|
+
assert_equal 1, doc.highlight.title.size
|
33
|
+
assert_equal "<strong>Two</strong>", doc.highlight.title.first, "Highlight does not include highlight tag"
|
34
|
+
assert_equal "<strong>ruby</strong>", doc.highlight.tags.first, "Highlight does not include highlight tag"
|
35
|
+
end
|
36
|
+
|
37
|
+
should "return entire content with highlighted fragments" do
|
38
|
+
# Tire::Configuration.logger STDERR, :level => 'debug'
|
39
|
+
|
40
|
+
content = "A Fox one day fell into a deep well and could find no means of escape. A Goat, overcome with thirst, came to the same well, and seeing the Fox, inquired if the water was good. Concealing his sad plight under a merry guise, the Fox indulged in a lavish praise of the water, saying it was excellent beyond measure, and encouraging him to descend. The Goat, mindful only of his thirst, thoughtlessly jumped down, but just as he drank, the Fox informed him of the difficulty they were both in and suggested a scheme for their common escape. \"If,\" said he, \"you will place your forefeet upon the wall and bend your head, I will run up your back and escape, and will help you out afterwards.\" The Goat readily assented and the Fox leaped upon his back. Steadying himself with the Goat horns, he safely reached the mouth of the well and made off as fast as he could. When the Goat upbraided him for breaking his promise, he turned around and cried out, \"You foolish old fellow! If you had as many brains in your head as you have hairs in your beard, you would never have gone down before you had inspected the way up, nor have exposed yourself to dangers from which you had no means of escape.\" Look before you leap."
|
41
|
+
|
42
|
+
Tire.index 'highlight-test' do
|
43
|
+
delete
|
44
|
+
create
|
45
|
+
store :id => 1, :content => content
|
46
|
+
refresh
|
47
|
+
end
|
48
|
+
|
49
|
+
s = Tire.search('highlight-test') do
|
50
|
+
query { string 'fox' }
|
51
|
+
highlight :content => { :number_of_fragments => 0 }
|
52
|
+
end
|
53
|
+
|
54
|
+
doc = s.results.first
|
55
|
+
assert_not_nil doc.highlight.content
|
56
|
+
|
57
|
+
highlight = doc.highlight.content
|
58
|
+
assert highlight.to_s.include?('<em>'), "Highlight does not include default highlight tag"
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'active_support/core_ext/numeric'
|
4
|
+
require 'active_support/core_ext/date/calculations'
|
5
|
+
|
6
|
+
module Tire
|
7
|
+
|
8
|
+
class IndexAliasesIntegrationTest < Test::Unit::TestCase
|
9
|
+
include Test::Integration
|
10
|
+
|
11
|
+
context "With a filtered alias" do
|
12
|
+
setup do
|
13
|
+
|
14
|
+
@index = Tire.index 'index-original' do
|
15
|
+
delete
|
16
|
+
create
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
teardown { Tire.index('index-original').delete }
|
22
|
+
|
23
|
+
should "create the alias" do
|
24
|
+
@index.add_alias 'index-aliased'
|
25
|
+
assert_equal 1, @index.aliases.size
|
26
|
+
end
|
27
|
+
|
28
|
+
should "find only portion of documents in the filtered alias" do
|
29
|
+
Tire.index 'index-original' do
|
30
|
+
add_alias 'index-aliased', :filter => { :term => { :user => 'anne' } }
|
31
|
+
store :title => 'Document 1', :user => 'anne'
|
32
|
+
store :title => 'Document 2', :user => 'mary'
|
33
|
+
|
34
|
+
refresh
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal 2, Tire.search('index-original') { query { all } }.results.size
|
38
|
+
assert_equal 1, Tire.search('index-aliased') { query { all } }.results.size
|
39
|
+
end
|
40
|
+
|
41
|
+
should "remove the alias" do
|
42
|
+
@index.add_alias 'index-aliased'
|
43
|
+
assert_equal 1, @index.aliases.size
|
44
|
+
|
45
|
+
@index.remove_alias 'index-aliased'
|
46
|
+
assert_equal 0, @index.aliases.size
|
47
|
+
|
48
|
+
assert_raise Tire::Search::SearchRequestFailed do
|
49
|
+
Tire.search('index-aliased') { query { all } }.results
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
should "retrieve a list of aliases for an index" do
|
54
|
+
@index.add_alias 'index-aliased'
|
55
|
+
|
56
|
+
assert_equal ['index-aliased'], @index.aliases.map(&:name)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "retrieve the properties of an alias" do
|
60
|
+
@index.add_alias 'index-aliased', :routing => '1'
|
61
|
+
|
62
|
+
assert_equal '1', @index.aliases('index-aliased').search_routing
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "In the 'sliding window' scenario" do
|
67
|
+
|
68
|
+
setup do
|
69
|
+
WINDOW_SIZE_IN_WEEKS = 4
|
70
|
+
|
71
|
+
@indices = WINDOW_SIZE_IN_WEEKS.times.map { |number| "articles_#{number.weeks.ago.strftime('%Y-%m-%d')}" }
|
72
|
+
|
73
|
+
@indices.each_with_index do |name,i|
|
74
|
+
Tire.index(name) do
|
75
|
+
delete
|
76
|
+
create
|
77
|
+
store :title => "Document #{i}"
|
78
|
+
refresh
|
79
|
+
end
|
80
|
+
Alias.new(:name => "articles_current") { |a| a.indices(name) and a.save }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
teardown do
|
85
|
+
@indices.each { |index| Tire.index(index).delete }
|
86
|
+
end
|
87
|
+
|
88
|
+
should "add a new index to alias" do
|
89
|
+
@indices << "articles_#{(WINDOW_SIZE_IN_WEEKS+1).weeks.ago.strftime('%Y-%m-%d')}"
|
90
|
+
Tire.index(@indices.last).create
|
91
|
+
Alias.new(:name => "articles_current") { |a| a.index @indices.last and a.save }
|
92
|
+
|
93
|
+
a = Alias.find("articles_current")
|
94
|
+
assert_equal 5, a.indices.size
|
95
|
+
end
|
96
|
+
|
97
|
+
should "remove the stale index from the alias" do
|
98
|
+
Alias.find("articles_current") do |a|
|
99
|
+
# Remove all indices older then 2 weeks from the alias
|
100
|
+
a.indices.delete_if do |i|
|
101
|
+
Time.parse( i.gsub(/articles_/, '') ) < 2.weeks.ago rescue false
|
102
|
+
end
|
103
|
+
a.save
|
104
|
+
end
|
105
|
+
|
106
|
+
assert_equal 2, Alias.find("articles_current").indices.size
|
107
|
+
end
|
108
|
+
|
109
|
+
should "search within the alias" do
|
110
|
+
Alias.find("articles_current") do |a|
|
111
|
+
a.indices.clear and a.indices @indices[0..1] and a.save
|
112
|
+
end
|
113
|
+
|
114
|
+
assert_equal 4, Tire.search(@indices) { query {all} }.results.size
|
115
|
+
assert_equal 2, Tire.search("articles_current") { query {all} }.results.size
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire
|
4
|
+
|
5
|
+
class IndexMappingIntegrationTest < Test::Unit::TestCase
|
6
|
+
include Test::Integration
|
7
|
+
|
8
|
+
context "Default mapping" do
|
9
|
+
teardown { Tire.index('mapped-index').delete; sleep 0.1 }
|
10
|
+
|
11
|
+
should "create and return the default mapping" do
|
12
|
+
|
13
|
+
index = Tire.index 'mapped-index' do
|
14
|
+
create
|
15
|
+
store :type => :article, :title => 'One'
|
16
|
+
refresh
|
17
|
+
sleep 1
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_equal 'string', index.mapping['article']['properties']['title']['type'], index.mapping.inspect
|
21
|
+
assert_nil index.mapping['article']['properties']['title']['boost'], index.mapping.inspect
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Creating index with mapping" do
|
26
|
+
teardown { Tire.index('mapped-index').delete; sleep 0.1 }
|
27
|
+
|
28
|
+
should "create the specified mapping" do
|
29
|
+
|
30
|
+
index = Tire.index 'mapped-index' do
|
31
|
+
create :mappings => { :article => { :properties => { :title => { :type => 'string', :boost => 2.0, :store => 'yes' } } } }
|
32
|
+
end
|
33
|
+
|
34
|
+
# p index.mapping
|
35
|
+
assert_equal 2.0, index.mapping['article']['properties']['title']['boost'], index.mapping.inspect
|
36
|
+
assert_equal 'yes', index.mapping['article']['properties']['title']['store'], index.mapping.inspect
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire
|
4
|
+
|
5
|
+
class IndexStoreIntegrationTest < Test::Unit::TestCase
|
6
|
+
include Test::Integration
|
7
|
+
|
8
|
+
context "Storing the documents in index" do
|
9
|
+
|
10
|
+
setup do
|
11
|
+
Tire.index 'articles-test-ids' do
|
12
|
+
delete
|
13
|
+
create
|
14
|
+
|
15
|
+
store :id => 1, :title => 'One'
|
16
|
+
store :id => 2, :title => 'Two'
|
17
|
+
store :id => 3, :title => 'Three'
|
18
|
+
store :id => 4, :title => 'Four'
|
19
|
+
store :id => 4, :title => 'Four'
|
20
|
+
|
21
|
+
refresh
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
teardown do
|
26
|
+
Tire.index('articles-test-ids').delete
|
27
|
+
Tire.index('articles-test-types').delete
|
28
|
+
end
|
29
|
+
|
30
|
+
should "happen in existing index" do
|
31
|
+
assert Tire.index("articles-test-ids").exists?
|
32
|
+
assert ! Tire.index("four-oh-four-index").exists?
|
33
|
+
end
|
34
|
+
|
35
|
+
should "store hashes under their IDs" do
|
36
|
+
s = Tire.search('articles-test-ids') { query { string '*' } }
|
37
|
+
|
38
|
+
assert_equal 4, s.results.count
|
39
|
+
|
40
|
+
document = Tire.index('articles-test-ids').retrieve :document, 4
|
41
|
+
assert_equal 'Four', document.title
|
42
|
+
assert_equal 2, document._version.to_i
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
should "store documents as proper types" do
|
47
|
+
Tire.index 'articles-test-types' do
|
48
|
+
delete
|
49
|
+
create
|
50
|
+
store :type => 'my_type', :title => 'One'
|
51
|
+
refresh
|
52
|
+
end
|
53
|
+
|
54
|
+
s = Tire.search('articles-test-types/my_type') { query { all } }
|
55
|
+
assert_equal 1, s.results.count
|
56
|
+
assert_equal 'my_type', s.results.first.type
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
context "Removing documents from the index" do
|
62
|
+
|
63
|
+
teardown { Tire.index('articles-test-remove').delete }
|
64
|
+
|
65
|
+
setup do
|
66
|
+
Tire.index 'articles-test-remove' do
|
67
|
+
delete
|
68
|
+
create
|
69
|
+
store :id => 1, :title => 'One'
|
70
|
+
store :id => 2, :title => 'Two'
|
71
|
+
refresh
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
should "remove document from the index" do
|
76
|
+
assert_equal 2, Tire.search('articles-test-remove') { query { string '*' } }.results.count
|
77
|
+
|
78
|
+
assert_nothing_raised do
|
79
|
+
assert Tire.index('articles-test-remove').remove 1
|
80
|
+
assert ! Tire.index('articles-test-remove').remove(1)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
context "Retrieving documents from the index" do
|
87
|
+
|
88
|
+
teardown { Tire.index('articles-test-retrieve').delete }
|
89
|
+
|
90
|
+
setup do
|
91
|
+
Tire.index 'articles-test-retrieve' do
|
92
|
+
delete
|
93
|
+
create
|
94
|
+
store :id => 1, :title => 'One'
|
95
|
+
store :id => 2, :title => 'Two'
|
96
|
+
refresh
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
should "retrieve document from the index" do
|
101
|
+
assert_instance_of Tire::Results::Item, Tire.index('articles-test-retrieve').retrieve(:document, 1)
|
102
|
+
end
|
103
|
+
|
104
|
+
should "return nil when retrieving missing document" do
|
105
|
+
assert_nil Tire.index('articles-test-retrieve').retrieve :document, 4
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire
|
4
|
+
|
5
|
+
class IndexUpdateDocumentIntegrationTest < Test::Unit::TestCase
|
6
|
+
include Test::Integration
|
7
|
+
|
8
|
+
context "Updating a document" do
|
9
|
+
|
10
|
+
setup do
|
11
|
+
Tire.index 'articles-with-tags' do
|
12
|
+
delete
|
13
|
+
create
|
14
|
+
|
15
|
+
store :type => 'article', :id => 1, :title => 'One', :tags => ['foo'], :views => 0
|
16
|
+
store :type => 'article', :id => 2, :title => 'Two', :tags => ['foo', 'bar'], :views => 10
|
17
|
+
store :type => 'article', :id => 3, :title => 'Three', :tags => ['foobar']
|
18
|
+
|
19
|
+
refresh
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
teardown { Tire.index('articles-with-tags').delete }
|
24
|
+
|
25
|
+
should "increment a counter" do
|
26
|
+
Tire.index('articles-with-tags') { update( 'article', '1', :script => "ctx._source.views += 1" ) and refresh }
|
27
|
+
|
28
|
+
document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
|
29
|
+
assert_equal 1, document.views, document.inspect
|
30
|
+
|
31
|
+
Tire.index('articles-with-tags') { update( 'article', '2', :script => "ctx._source.views += 1" ) and refresh }
|
32
|
+
|
33
|
+
document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
|
34
|
+
assert_equal 11, document.views, document.inspect
|
35
|
+
end
|
36
|
+
|
37
|
+
should "add a tag to document" do
|
38
|
+
Tire.index('articles-with-tags') do
|
39
|
+
update 'article', '1', :script => "ctx._source.tags += tag",
|
40
|
+
:params => { :tag => 'new' }
|
41
|
+
refresh
|
42
|
+
end
|
43
|
+
|
44
|
+
document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
|
45
|
+
assert_equal ['foo', 'new'], document.tags, document.inspect
|
46
|
+
end
|
47
|
+
|
48
|
+
should "remove a tag from document" do
|
49
|
+
Tire.index('articles-with-tags') do
|
50
|
+
update 'article', '1', :script => "ctx._source.tags = tags",
|
51
|
+
:params => { :tags => [] }
|
52
|
+
refresh
|
53
|
+
end
|
54
|
+
|
55
|
+
document = Tire.index('articles-with-tags').retrieve 'article', '1'
|
56
|
+
assert_equal [], document.tags, document.inspect
|
57
|
+
end
|
58
|
+
|
59
|
+
should "remove the entire document if specific condition is met" do
|
60
|
+
Tire.index('articles-with-tags') do
|
61
|
+
# Remove document when it contains tag 'foobar'
|
62
|
+
update 'article', '3', :script => "ctx._source.tags.contains(tag) ? ctx.op = 'delete' : 'none'",
|
63
|
+
:params => { :tag => 'foobar' }
|
64
|
+
refresh
|
65
|
+
end
|
66
|
+
|
67
|
+
assert_nil Tire.index('articles-with-tags').retrieve 'article', '3'
|
68
|
+
end
|
69
|
+
|
70
|
+
should "pass the operation parameters to the API" do
|
71
|
+
Tire.index('articles-with-tags').update 'article', '2', { :script => "ctx._source.tags += tag",
|
72
|
+
:params => { :tag => 'new' }
|
73
|
+
},
|
74
|
+
{
|
75
|
+
:refresh => true
|
76
|
+
}
|
77
|
+
|
78
|
+
document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
|
79
|
+
assert_equal 3, document.tags.size, document.inspect
|
80
|
+
end
|
81
|
+
|
82
|
+
should "update the document with a partial one" do
|
83
|
+
Tire.index('articles-with-tags') do
|
84
|
+
update( 'article', '1', :doc => { :title => 'One UPDATED' } )
|
85
|
+
refresh
|
86
|
+
end
|
87
|
+
|
88
|
+
document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
|
89
|
+
assert_equal 'One UPDATED', document.title, document.inspect
|
90
|
+
end
|
91
|
+
|
92
|
+
should "access variables from the outer scope" do
|
93
|
+
$t = self
|
94
|
+
|
95
|
+
class Updater
|
96
|
+
@tags = ['foo', 'bar', 'baz']
|
97
|
+
|
98
|
+
def self.perform_update!
|
99
|
+
$t.assert_not_nil @tags
|
100
|
+
|
101
|
+
Tire.index('articles-with-tags') do |index|
|
102
|
+
$t.assert_not_nil @tags
|
103
|
+
|
104
|
+
index.update 'article', '3', :script => "ctx._source.tags = tags",
|
105
|
+
:params => { :tags => @tags }
|
106
|
+
index.refresh
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
Updater.perform_update!
|
112
|
+
|
113
|
+
document = Tire.search('articles-with-tags') { query { string 'title:three' } }.results.first
|
114
|
+
assert_equal 3, document.tags.size, document.inspect
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|