tire-erez 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +14 -0
  2. data/.travis.yml +32 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +10 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.markdown +775 -0
  7. data/Rakefile +51 -0
  8. data/examples/rails-application-template.rb +263 -0
  9. data/examples/tire-dsl.rb +932 -0
  10. data/lib/tire.rb +59 -0
  11. data/lib/tire/alias.rb +296 -0
  12. data/lib/tire/configuration.rb +38 -0
  13. data/lib/tire/count.rb +85 -0
  14. data/lib/tire/dsl.rb +114 -0
  15. data/lib/tire/http/client.rb +62 -0
  16. data/lib/tire/http/clients/curb.rb +61 -0
  17. data/lib/tire/http/clients/faraday.rb +71 -0
  18. data/lib/tire/http/response.rb +27 -0
  19. data/lib/tire/index.rb +443 -0
  20. data/lib/tire/logger.rb +60 -0
  21. data/lib/tire/model/callbacks.rb +40 -0
  22. data/lib/tire/model/import.rb +27 -0
  23. data/lib/tire/model/indexing.rb +134 -0
  24. data/lib/tire/model/naming.rb +100 -0
  25. data/lib/tire/model/percolate.rb +99 -0
  26. data/lib/tire/model/persistence.rb +72 -0
  27. data/lib/tire/model/persistence/attributes.rb +148 -0
  28. data/lib/tire/model/persistence/finders.rb +54 -0
  29. data/lib/tire/model/persistence/storage.rb +77 -0
  30. data/lib/tire/model/search.rb +322 -0
  31. data/lib/tire/multi_search.rb +263 -0
  32. data/lib/tire/results/collection.rb +156 -0
  33. data/lib/tire/results/item.rb +94 -0
  34. data/lib/tire/results/pagination.rb +68 -0
  35. data/lib/tire/rubyext/hash.rb +8 -0
  36. data/lib/tire/rubyext/ruby_1_8.rb +1 -0
  37. data/lib/tire/rubyext/symbol.rb +11 -0
  38. data/lib/tire/rubyext/uri_escape.rb +74 -0
  39. data/lib/tire/search.rb +211 -0
  40. data/lib/tire/search/facet.rb +81 -0
  41. data/lib/tire/search/filter.rb +28 -0
  42. data/lib/tire/search/highlight.rb +37 -0
  43. data/lib/tire/search/queries/match.rb +40 -0
  44. data/lib/tire/search/query.rb +250 -0
  45. data/lib/tire/search/scan.rb +114 -0
  46. data/lib/tire/search/script_field.rb +23 -0
  47. data/lib/tire/search/sort.rb +25 -0
  48. data/lib/tire/tasks.rb +138 -0
  49. data/lib/tire/utils.rb +17 -0
  50. data/lib/tire/version.rb +18 -0
  51. data/test/fixtures/articles/1.json +1 -0
  52. data/test/fixtures/articles/2.json +1 -0
  53. data/test/fixtures/articles/3.json +1 -0
  54. data/test/fixtures/articles/4.json +1 -0
  55. data/test/fixtures/articles/5.json +1 -0
  56. data/test/integration/active_model_indexing_test.rb +51 -0
  57. data/test/integration/active_model_searchable_test.rb +114 -0
  58. data/test/integration/active_record_searchable_test.rb +620 -0
  59. data/test/integration/boolean_queries_test.rb +43 -0
  60. data/test/integration/boosting_queries_test.rb +32 -0
  61. data/test/integration/bulk_test.rb +86 -0
  62. data/test/integration/count_test.rb +64 -0
  63. data/test/integration/custom_score_queries_test.rb +89 -0
  64. data/test/integration/dis_max_queries_test.rb +68 -0
  65. data/test/integration/dsl_search_test.rb +30 -0
  66. data/test/integration/explanation_test.rb +44 -0
  67. data/test/integration/facets_test.rb +311 -0
  68. data/test/integration/filtered_queries_test.rb +66 -0
  69. data/test/integration/filters_test.rb +75 -0
  70. data/test/integration/fuzzy_queries_test.rb +20 -0
  71. data/test/integration/highlight_test.rb +64 -0
  72. data/test/integration/index_aliases_test.rb +122 -0
  73. data/test/integration/index_mapping_test.rb +43 -0
  74. data/test/integration/index_store_test.rb +112 -0
  75. data/test/integration/index_update_document_test.rb +121 -0
  76. data/test/integration/match_query_test.rb +79 -0
  77. data/test/integration/mongoid_searchable_test.rb +309 -0
  78. data/test/integration/multi_search_test.rb +114 -0
  79. data/test/integration/nested_query_test.rb +135 -0
  80. data/test/integration/percolator_test.rb +111 -0
  81. data/test/integration/persistent_model_test.rb +205 -0
  82. data/test/integration/prefix_query_test.rb +43 -0
  83. data/test/integration/query_return_version_test.rb +70 -0
  84. data/test/integration/query_string_test.rb +52 -0
  85. data/test/integration/range_queries_test.rb +36 -0
  86. data/test/integration/reindex_test.rb +56 -0
  87. data/test/integration/results_test.rb +58 -0
  88. data/test/integration/scan_test.rb +56 -0
  89. data/test/integration/script_fields_test.rb +38 -0
  90. data/test/integration/sort_test.rb +52 -0
  91. data/test/integration/text_query_test.rb +39 -0
  92. data/test/models/active_model_article.rb +31 -0
  93. data/test/models/active_model_article_with_callbacks.rb +49 -0
  94. data/test/models/active_model_article_with_custom_document_type.rb +7 -0
  95. data/test/models/active_model_article_with_custom_index_name.rb +7 -0
  96. data/test/models/active_record_models.rb +131 -0
  97. data/test/models/article.rb +15 -0
  98. data/test/models/mongoid_models.rb +85 -0
  99. data/test/models/persistent_article.rb +11 -0
  100. data/test/models/persistent_article_in_index.rb +16 -0
  101. data/test/models/persistent_article_in_namespace.rb +12 -0
  102. data/test/models/persistent_article_with_casting.rb +28 -0
  103. data/test/models/persistent_article_with_defaults.rb +12 -0
  104. data/test/models/persistent_article_with_percolation.rb +5 -0
  105. data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
  106. data/test/models/supermodel_article.rb +17 -0
  107. data/test/models/validated_model.rb +11 -0
  108. data/test/test_helper.rb +118 -0
  109. data/test/unit/active_model_lint_test.rb +17 -0
  110. data/test/unit/configuration_test.rb +84 -0
  111. data/test/unit/count_test.rb +67 -0
  112. data/test/unit/http_client_test.rb +79 -0
  113. data/test/unit/http_response_test.rb +49 -0
  114. data/test/unit/index_alias_test.rb +335 -0
  115. data/test/unit/index_test.rb +1098 -0
  116. data/test/unit/logger_test.rb +125 -0
  117. data/test/unit/model_callbacks_test.rb +116 -0
  118. data/test/unit/model_import_test.rb +75 -0
  119. data/test/unit/model_initialization_test.rb +31 -0
  120. data/test/unit/model_persistence_test.rb +548 -0
  121. data/test/unit/model_search_test.rb +964 -0
  122. data/test/unit/multi_search_test.rb +304 -0
  123. data/test/unit/results_collection_test.rb +372 -0
  124. data/test/unit/results_item_test.rb +173 -0
  125. data/test/unit/rubyext_test.rb +66 -0
  126. data/test/unit/search_facet_test.rb +186 -0
  127. data/test/unit/search_filter_test.rb +42 -0
  128. data/test/unit/search_highlight_test.rb +46 -0
  129. data/test/unit/search_query_test.rb +419 -0
  130. data/test/unit/search_scan_test.rb +113 -0
  131. data/test/unit/search_script_field_test.rb +26 -0
  132. data/test/unit/search_sort_test.rb +50 -0
  133. data/test/unit/search_test.rb +556 -0
  134. data/test/unit/tire_test.rb +144 -0
  135. data/tire.gemspec +83 -0
  136. metadata +586 -0
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class BooleanQueriesIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Boolean queries" do
9
+
10
+ should "allow to set multiple queries per condition" do
11
+ s = Tire.search('articles-test') do
12
+ query do
13
+ boolean do
14
+ must { term :tags, 'ruby' }
15
+ must { term :tags, 'python' }
16
+ end
17
+ end
18
+ end
19
+
20
+ assert_equal 1, s.results.size
21
+ assert_equal 'Two', s.results.first.title
22
+ end
23
+
24
+ should "allow to set multiple queries for multiple conditions" do
25
+ s = Tire.search('articles-test') do
26
+ query do
27
+ boolean do
28
+ must { term :tags, 'ruby' }
29
+ should { term :tags, 'python' }
30
+ end
31
+ end
32
+ end
33
+
34
+ assert_equal 2, s.results.size
35
+ assert_equal 'Two', s.results[0].title
36
+ assert_equal 'One', s.results[1].title
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class BoostingQueriesIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Boosting queries" do
9
+
10
+ should "allow to set multiple queries per condition" do
11
+ s = Tire.search('articles-test') do
12
+ query do
13
+ boosting(:negative_boost => 0.2) do
14
+ positive do
15
+ string "Two One"
16
+ end
17
+ negative do
18
+ term :tags, 'python'
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ assert_equal 'One', s.results[0].title
25
+ assert_equal 'Two', s.results[1].title
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class BulkIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Bulk" do
9
+ setup do
10
+ @index = Tire.index('bulk-test') { delete; create }
11
+ @articles = [
12
+ { id: '1', type: 'article', title: 'one', tags: ['ruby'] },
13
+ { id: '2', type: 'article', title: 'two', tags: ['ruby', 'python'] },
14
+ { id: '3', type: 'article', title: 'three', tags: ['java'] }
15
+ ]
16
+ end
17
+
18
+ teardown do
19
+ @index.delete
20
+ Tire.index('bulk-test-fresh').delete
21
+ Tire.index('bulk-test-consistency').delete
22
+ Tire.index('bulk-test-routing').delete
23
+ end
24
+
25
+ should "store a collection of documents and refresh the index" do
26
+ @index.bulk_store @articles, refresh: true
27
+ assert_equal 3, Tire.search('bulk-test/article').query { all }.results.size
28
+ end
29
+
30
+ should "extract the routing value from documents" do
31
+ index = Tire.index('bulk-test-routing')
32
+ index.delete
33
+ index.create index: { number_of_shards: 2, number_of_replicas: 0 }
34
+ index.bulk_store [ { id: '1', title: 'A', _routing: 'a'}, { id: '2', title: 'B', _routing: 'b'} ]
35
+ index.refresh
36
+
37
+ assert_equal 2, Tire.search('bulk-test-routing') { query {all} }.results.size
38
+ assert_equal 1, Tire.search('bulk-test-routing', routing: 'a') { query {all} }.results.size
39
+ assert_equal 1, Tire.search('bulk-test-routing', routing: 'b') { query {all} }.results.size
40
+ end
41
+
42
+ should "delete documents in bulk" do
43
+ (1..10).to_a.each { |i| @index.store id: i }
44
+ @index.refresh
45
+ assert_equal 10, Tire.search('bulk-test') { query {all} }.results.size
46
+
47
+ documents = (1..10).to_a.map { |i| { id: i } }
48
+ @index.bulk_delete documents, refresh: true
49
+ assert_equal 0, Tire.search('bulk-test') { query {all} }.results.size
50
+ end
51
+
52
+ should "allow to feed search results to bulk API" do
53
+ (1..10).to_a.each { |i| @index.store id: i }
54
+ @index.refresh
55
+ assert_equal 10, Tire.search('bulk-test') { query {all} }.results.size
56
+
57
+ documents = Tire.search('bulk-test') { query {all} }.results.to_a
58
+
59
+ @index.bulk_delete documents, refresh: true
60
+ assert_equal 0, Tire.search('bulk-test') { query {all} }.results.size
61
+
62
+ Tire.index('bulk-test-fresh').bulk_create documents, refresh: true
63
+ assert_equal 10, Tire.search('bulk-test-fresh') { query {all} }.results.size
64
+ end
65
+
66
+ should "timeout when consistency factor is not met" do
67
+ # Tire.configure { logger STDERR, level: 'debug' }
68
+
69
+ Tire.index 'bulk-test-consistency' do
70
+ delete
71
+ create index: { number_of_shards: 1, number_of_replicas: 15 }
72
+ end
73
+
74
+ assert_raise Timeout::Error do
75
+ Timeout::timeout(3) do
76
+ Tire.index('bulk-test-consistency').bulk_store [ {id: '1', title: 'One' } ],
77
+ consistency: 'all',
78
+ raise: true
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class CountIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Count with search type" do
9
+
10
+ should "return total number of hits for the query, but no hits" do
11
+ s = Tire.search 'articles-test', :search_type => 'count' do
12
+ query { term :tags, 'ruby' }
13
+ end
14
+
15
+ assert_equal 2, s.results.total
16
+ assert_equal 0, s.results.count
17
+ assert s.results.empty?
18
+ end
19
+
20
+ should "return facets in results" do
21
+ s = Tire.search 'articles-test', :search_type => 'count' do
22
+ query { term :tags, 'ruby' }
23
+ facet('tags') { terms :tags }
24
+ end
25
+
26
+ assert ! s.results.facets['tags'].empty?
27
+ assert_equal 2, s.results.facets['tags']['terms'].select { |t| t['term'] == 'ruby' }. first['count']
28
+ assert_equal 1, s.results.facets['tags']['terms'].select { |t| t['term'] == 'python' }.first['count']
29
+ end
30
+
31
+ end
32
+
33
+ context "Count with the count method" do
34
+ setup { Tire.index('articles-test-count') { delete; create and store(title: 'Test') and refresh } }
35
+ teardown { Tire.index('articles-test-count') { delete } }
36
+
37
+ should "return number of documents in the index" do
38
+ assert_equal 5, Tire.count('articles-test')
39
+ end
40
+
41
+ should "return number of documents in the index for specific query" do
42
+ # Tire.configure { logger STDERR, level: 'debug' }
43
+ count = Tire.count('articles-test') do
44
+ term :tags, 'ruby'
45
+ end
46
+ assert_equal 2, count
47
+ end
48
+
49
+ should "return number of documents in multiple indices" do
50
+ assert_equal 6, Tire.count(['articles-test', 'articles-test-count'])
51
+ end
52
+
53
+ should "allow access to the JSON and response" do
54
+ c = Tire::Search::Count.new('articles-test')
55
+ c.perform
56
+ assert_equal 5, c.value
57
+ assert_equal 0, c.json['_shards']['failed']
58
+ assert c.response.success?, "Response should be successful: #{c.response.inspect}"
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class CustomScoreQueriesIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Custom score queries" do
9
+
10
+ should "allow to define custom score queries (base score on field value)" do
11
+ s = Tire.search('articles-test') do
12
+ query do
13
+ # Give longer documents higher score
14
+ #
15
+ custom_score :script => "1.0 / doc['words'].value" do
16
+ string "title:T*"
17
+ end
18
+ end
19
+ end
20
+
21
+ assert_equal 2, s.results.size
22
+ assert_equal ['Two', 'Three'], s.results.map(&:title)
23
+
24
+ assert s.results[0]._score > 0
25
+ assert s.results[1]._score > 0
26
+ assert s.results[0]._score > s.results[1]._score
27
+ end
28
+
29
+ should "allow to manipulate the default score (boost recent)" do
30
+ s = Tire.search('articles-test') do
31
+ query do
32
+ # Boost recent documents
33
+ #
34
+ custom_score :script => "_score + ( doc['published_on'].date.getMillis() / time() )" do
35
+ string 'title:F*'
36
+ end
37
+ end
38
+ end
39
+
40
+ assert_equal 2, s.results.size
41
+ assert_equal ['Five', 'Four'], s.results.map(&:title)
42
+
43
+ assert s.results[0]._score > 1
44
+ assert s.results[1]._score > 1
45
+ end
46
+
47
+ should "allow to define arbitrary custom scoring" do
48
+ s = Tire.search('articles-test') do
49
+ query do
50
+ # Replace documents score with the count of characters in their title
51
+ #
52
+ custom_score :script => "doc['title'].value.length()" do
53
+ string "title:T*"
54
+ end
55
+ end
56
+ end
57
+
58
+ assert_equal 2, s.results.size
59
+ assert_equal ['Three', 'Two'], s.results.map(&:title)
60
+
61
+ assert_equal 5.0, s.results.max_score
62
+ assert_equal 5.0, s.results[0]._score
63
+ assert_equal 3.0, s.results[1]._score
64
+ end
65
+
66
+ should "allow to pass parameters to the script" do
67
+ s = Tire.search('articles-test') do
68
+ query do
69
+ # Replace documents score with parameterized computation
70
+ #
71
+ custom_score :script => "doc['words'].doubleValue / max(a, b)",
72
+ :params => { :a => 1, :b => 2 } do
73
+ string "title:T*"
74
+ end
75
+ end
76
+ end
77
+
78
+ assert_equal 2, s.results.size
79
+ assert_equal ['Three', 'Two'], s.results.map(&:title)
80
+
81
+ assert_equal 187.5, s.results[0]._score
82
+ assert_equal 125.0, s.results[1]._score
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class DisMaxQueriesIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Dis Max queries" do
9
+ setup do
10
+ Tire.index 'dis_max_test' do
11
+ delete
12
+ create
13
+
14
+ store title: "It's an Albino, Albino, Albino thing!", text: "Albino, albino, albino! Wanna know about albino? ..."
15
+ store title: "Albino Vampire Monkey Attacks!", text: "The night was just setting in when ..."
16
+ store title: "Pinky Elephant", text: "An albino walks into a ZOO and ..."
17
+ refresh
18
+ end
19
+ end
20
+
21
+ teardown do
22
+ Tire.index('dis_max_test').delete
23
+ end
24
+
25
+ should_eventually "boost matches in both fields" do
26
+ dis_max = Tire.search 'dis_max_test' do
27
+ query do
28
+ dis_max do
29
+ query { string "albino elephant", fields: ['title', 'text'] }
30
+ end
31
+ end
32
+ end
33
+ # p "DisMax:", dis_max.results.map(&:title)
34
+
35
+ assert_equal 'Pinky Elephant', dis_max.results.first.title
36
+
37
+ # NOTE: This gives exactly the same result as a boolean query:
38
+ # boolean = Tire.search 'dis_max_test' do
39
+ # query do
40
+ # boolean do
41
+ # should { string "albino", fields: ['title', 'text'] }
42
+ # should { string "elephant", fields: ['title', 'text'] }
43
+ # end
44
+ # end
45
+ # end
46
+ # p "Boolean:", boolean.results.map(&:title)
47
+ end
48
+
49
+ should "allow to set multiple queries" do
50
+ s = Tire.search('articles-test') do
51
+ query do
52
+ dis_max do
53
+ query { term :tags, 'ruby' }
54
+ query { term :tags, 'python' }
55
+ end
56
+ end
57
+ end
58
+
59
+ assert_equal 2, s.results.size
60
+ assert_equal 'Two', s.results[0].title
61
+ assert_equal 'One', s.results[1].title
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class DSLSearchIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "DSL" do
9
+
10
+ should "allow passing search payload as a Hash" do
11
+ s = Tire.search 'articles-test', :query => { :query_string => { :query => 'ruby' } },
12
+ :facets => { 'tags' => { :filter => { :term => {:tags => 'ruby' } } } }
13
+
14
+ assert_equal 2, s.results.count
15
+ assert_equal 2, s.results.facets['tags']['count']
16
+ end
17
+
18
+ should "allow building search query iteratively" do
19
+ s = Tire.search 'articles-test'
20
+ s.query { string 'T*' }
21
+ s.filter :terms, :tags => ['java']
22
+
23
+ assert_equal 1, s.results.count
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class ExplanationIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Explanation" do
9
+ teardown { Tire.index('explanation-test').delete }
10
+
11
+ setup do
12
+ content = "A Fox one day fell into a deep well and could find no means of escape."
13
+
14
+ Tire.index 'explanation-test' do
15
+ delete
16
+ create
17
+ store :id => 1, :content => content
18
+ refresh
19
+ end
20
+ end
21
+
22
+ should "add '_explanation' field to the result item" do
23
+ # Tire::Configuration.logger STDERR, :level => 'debug'
24
+ s = Tire.search 'explanation-test', :explain => true do
25
+ query do
26
+ boolean do
27
+ should { string 'content:Fox' }
28
+ end
29
+ end
30
+ end
31
+
32
+ doc = s.results.first
33
+
34
+ explanation = doc._explanation
35
+
36
+ assert explanation.description.include?("product of:")
37
+ assert explanation.value < 0.6
38
+ assert_not_nil explanation.details
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end