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,39 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class QueryStringIntegrationTest < Test::Unit::TestCase
6
+ include Test::Integration
7
+
8
+ context "Text query" do
9
+ setup do
10
+ Tire.index('articles-test') do
11
+ store :type => 'article', :title => '+1 !!!'
12
+ store :type => 'article', :title => 'Furry Kitten'
13
+ refresh
14
+ end
15
+ end
16
+
17
+ should "find article by title" do
18
+ results = Tire.search('articles-test') do
19
+ query { text :title, '+1' }
20
+ end.results
21
+
22
+ assert_equal 1, results.count
23
+ assert_equal "+1 !!!", results.first[:title]
24
+ end
25
+
26
+ should "allow to pass options (fuzziness)" do
27
+ results = Tire.search('articles-test') do
28
+ query { text :title, 'fuzzy mitten', :fuzziness => 0.5, :operator => 'and' }
29
+ end.results
30
+
31
+ assert_equal 1, results.count
32
+ assert_equal "Furry Kitten", results.first[:title]
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,31 @@
1
+ # Example ActiveModel class
2
+
3
+ require 'rubygems'
4
+ require 'active_model'
5
+
6
+ class ActiveModelArticle
7
+
8
+ extend ActiveModel::Naming
9
+ include ActiveModel::AttributeMethods
10
+ include ActiveModel::Serialization
11
+ include ActiveModel::Serializers::JSON
12
+
13
+ include Tire::Model::Search
14
+
15
+ attr_reader :attributes
16
+
17
+ def initialize(attributes = {})
18
+ @attributes = attributes
19
+ end
20
+
21
+ def id; attributes['id'] || attributes['_id']; end
22
+ def id=(value); attributes['id'] = value; end
23
+
24
+ def method_missing(name, *args, &block)
25
+ attributes[name.to_sym] || attributes[name.to_s] || super
26
+ end
27
+
28
+ def persisted?; true; end
29
+ def save; true; end
30
+
31
+ end
@@ -0,0 +1,49 @@
1
+ # Example ActiveModel class with callbacks
2
+
3
+ require 'rubygems'
4
+ require 'active_model'
5
+
6
+ class ActiveModelArticleWithCallbacks
7
+
8
+ include ActiveModel::AttributeMethods
9
+ include ActiveModel::Validations
10
+ include ActiveModel::Serialization
11
+ include ActiveModel::Serializers::JSON
12
+ include ActiveModel::Naming
13
+
14
+ extend ActiveModel::Callbacks
15
+ define_model_callbacks :save, :destroy
16
+
17
+ include Tire::Model::Search
18
+ include Tire::Model::Callbacks
19
+
20
+ attr_reader :attributes
21
+
22
+ def initialize(attributes = {})
23
+ @attributes = attributes
24
+ end
25
+
26
+ def method_missing(id, *args, &block)
27
+ attributes[id.to_sym] || attributes[id.to_s] || super
28
+ end
29
+
30
+ def persisted?
31
+ true
32
+ end
33
+
34
+ def save
35
+ _run_save_callbacks do
36
+ STDERR.puts "[Saving ...]"
37
+ end
38
+ end
39
+
40
+ def destroy
41
+ _run_destroy_callbacks do
42
+ STDERR.puts "[Destroying ...]"
43
+ @destroyed = true
44
+ end
45
+ end
46
+
47
+ def destroyed?; !!@destroyed; end
48
+
49
+ end
@@ -0,0 +1,7 @@
1
+ # Example ActiveModel class with custom document type
2
+
3
+ require File.expand_path('../active_model_article', __FILE__)
4
+
5
+ class ActiveModelArticleWithCustomDocumentType < ActiveModelArticle
6
+ document_type 'my_custom_type'
7
+ end
@@ -0,0 +1,7 @@
1
+ # Example ActiveModel class with custom index name
2
+
3
+ require File.expand_path('../active_model_article', __FILE__)
4
+
5
+ class ActiveModelArticleWithCustomIndexName < ActiveModelArticle
6
+ index_name 'custom-index-name'
7
+ end
@@ -0,0 +1,131 @@
1
+ require 'active_record'
2
+
3
+ class ActiveRecordArticle < ActiveRecord::Base
4
+ has_many :comments, :class_name => "ActiveRecordComment", :foreign_key => "article_id"
5
+ has_many :stats, :class_name => "ActiveRecordStat", :foreign_key => "article_id"
6
+
7
+ include Tire::Model::Search
8
+ include Tire::Model::Callbacks
9
+
10
+ tire do
11
+ mapping do
12
+ indexes :title, :type => 'string', :boost => 10, :analyzer => 'snowball'
13
+ indexes :created_at, :type => 'date'
14
+
15
+ indexes :comments do
16
+ indexes :author
17
+ indexes :body
18
+ end
19
+ end
20
+ end
21
+
22
+ def to_indexed_json
23
+ {
24
+ :title => title,
25
+ :length => length,
26
+
27
+ :comments => comments.map { |c| { :_type => 'active_record_comment',
28
+ :_id => c.id,
29
+ :author => c.author,
30
+ :body => c.body } },
31
+ :stats => stats.map { |s| { :pageviews => s.pageviews } }
32
+ }.to_json
33
+ end
34
+
35
+ def length
36
+ title.length
37
+ end
38
+
39
+ def comment_authors
40
+ comments.map(&:author).to_sentence
41
+ end
42
+ end
43
+
44
+ class ActiveRecordComment < ActiveRecord::Base
45
+ belongs_to :article, :class_name => "ActiveRecordArticle", :foreign_key => "article_id"
46
+ end
47
+
48
+ class ActiveRecordStat < ActiveRecord::Base
49
+ belongs_to :article, :class_name => "ActiveRecordArticle", :foreign_key => "article_id"
50
+ end
51
+
52
+ class ActiveRecordClassWithTireMethods < ActiveRecord::Base
53
+
54
+ def self.mapping
55
+ "THIS IS MY MAPPING!"
56
+ end
57
+
58
+ def index
59
+ "THIS IS MY INDEX!"
60
+ end
61
+
62
+ include Tire::Model::Search
63
+ include Tire::Model::Callbacks
64
+
65
+ tire do
66
+ mapping do
67
+ indexes :title, :type => 'string', :analyzer => 'snowball'
68
+ end
69
+ end
70
+ end
71
+
72
+ class ActiveRecordClassWithDynamicIndexName < ActiveRecord::Base
73
+ include Tire::Model::Search
74
+ include Tire::Model::Callbacks
75
+
76
+ index_name do
77
+ "dynamic" + '_' + "index"
78
+ end
79
+ end
80
+
81
+ # Used in test for multiple class instances in one index,
82
+ # and single table inheritance (STI) support.
83
+
84
+ class ActiveRecordModelOne < ActiveRecord::Base
85
+ include Tire::Model::Search
86
+ include Tire::Model::Callbacks
87
+ self.table_name = 'active_record_model_one'
88
+ index_name 'active_record_model_one'
89
+ end
90
+
91
+ class ActiveRecordModelTwo < ActiveRecord::Base
92
+ include Tire::Model::Search
93
+ include Tire::Model::Callbacks
94
+ self.table_name = 'active_record_model_two'
95
+ index_name 'active_record_model_two'
96
+ end
97
+
98
+ class ActiveRecordAsset < ActiveRecord::Base
99
+ include Tire::Model::Search
100
+ include Tire::Model::Callbacks
101
+ end
102
+
103
+ class ActiveRecordVideo < ActiveRecordAsset
104
+ index_name 'active_record_assets'
105
+ end
106
+
107
+ class ActiveRecordPhoto < ActiveRecordAsset
108
+ index_name 'active_record_assets'
109
+ end
110
+
111
+ # Namespaced ActiveRecord models
112
+
113
+ module ActiveRecordNamespace
114
+ def self.table_name_prefix
115
+ 'active_record_namespace_'
116
+ end
117
+ end
118
+
119
+ class ActiveRecordNamespace::MyModel < ActiveRecord::Base
120
+ include Tire::Model::Search
121
+ include Tire::Model::Callbacks
122
+ end
123
+
124
+ # Model with percolation
125
+
126
+ class ActiveRecordModelWithPercolation < ActiveRecord::Base
127
+ include Tire::Model::Search
128
+ include Tire::Model::Callbacks
129
+
130
+ percolate!
131
+ end
@@ -0,0 +1,15 @@
1
+ # Example non-ActiveModel custom wrapper for result item
2
+
3
+ class Article
4
+ attr_reader :id, :title, :body
5
+
6
+ def initialize(attributes={})
7
+ attributes.each { |k,v| instance_variable_set(:"@#{k}", v) }
8
+ end
9
+
10
+ def to_json(options={})
11
+ { :id => @id, :title => @title, :body => @body }.to_json
12
+ end
13
+
14
+ alias :to_indexed_json :to_json
15
+ end
@@ -0,0 +1,85 @@
1
+ require 'mongoid'
2
+
3
+ class MongoidArticle
4
+
5
+ include Mongoid::Document
6
+
7
+
8
+ has_many :comments, :class_name => "MongoidComment", :foreign_key => "article_id"
9
+ has_many :stats, :class_name => "MongoidStat", :foreign_key => "article_id"
10
+
11
+ include Tire::Model::Search
12
+ include Tire::Model::Callbacks
13
+
14
+ tire do
15
+ mapping do
16
+ indexes :title, :type => 'string', :boost => 10, :analyzer => 'snowball'
17
+ indexes :created_at, :type => 'date'
18
+
19
+ indexes :comments do
20
+ indexes :author
21
+ indexes :body
22
+ end
23
+ end
24
+ end
25
+
26
+ def to_indexed_json
27
+ {
28
+ :title => title,
29
+ :length => length,
30
+
31
+ :comments => comments.map { |c| { :_type => 'mongoid_comment',
32
+ :_id => c.id,
33
+ :author => c.author,
34
+ :body => c.body } },
35
+ :stats => stats.map { |s| { :pageviews => s.pageviews } }
36
+ }.to_json
37
+ end
38
+
39
+ def length
40
+ title.length
41
+ end
42
+
43
+ def comment_authors
44
+ comments.map(&:author).to_sentence
45
+ end
46
+ end
47
+
48
+ class MongoidComment
49
+
50
+ include Mongoid::Document
51
+
52
+
53
+ belongs_to :article, :class_name => "MongoidArticle", :foreign_key => "article_id"
54
+ end
55
+
56
+ class MongoidStat
57
+
58
+ include Mongoid::Document
59
+
60
+
61
+ belongs_to :article, :class_name => "MongoidArticle", :foreign_key => "article_id"
62
+ end
63
+
64
+ class MongoidClassWithTireMethods
65
+
66
+ include Mongoid::Document
67
+
68
+
69
+ def self.mapping
70
+ "THIS IS MY MAPPING!"
71
+ end
72
+
73
+ def index
74
+ "THIS IS MY INDEX!"
75
+ end
76
+
77
+ include Tire::Model::Search
78
+ include Tire::Model::Callbacks
79
+
80
+ tire do
81
+ mapping do
82
+ indexes :title, :type => 'string', :analyzer => 'snowball'
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,11 @@
1
+ # Example class with Elasticsearch persistence
2
+
3
+ class PersistentArticle
4
+
5
+ include Tire::Model::Persistence
6
+
7
+ property :title
8
+ property :published_on
9
+ property :tags
10
+
11
+ end
@@ -0,0 +1,16 @@
1
+ # Example class with Elasticsearch persistence in index `persistent_articles`
2
+ #
3
+ # The `index` is `persistent_articles`
4
+ #
5
+
6
+ class PersistentArticleInIndex
7
+
8
+ include Tire::Model::Persistence
9
+
10
+ property :title
11
+ property :published_on
12
+ property :tags
13
+
14
+ index_name "persistent_articles"
15
+
16
+ end
@@ -0,0 +1,12 @@
1
+ # Example namespaced class with Elasticsearch persistence
2
+ #
3
+ # The `document_type` is `my_namespace/persistent_article_in_namespace`
4
+ #
5
+
6
+ module MyNamespace
7
+ class PersistentArticleInNamespace
8
+ include Tire::Model::Persistence
9
+
10
+ property :title
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ class Author
2
+ attr_accessor :first_name, :last_name
3
+ def initialize(attributes)
4
+ @first_name = HashWithIndifferentAccess.new(attributes)[:first_name]
5
+ @last_name = HashWithIndifferentAccess.new(attributes)[:last_name]
6
+ end
7
+ end
8
+
9
+ class Comment
10
+ def initialize(params); @attributes = HashWithIndifferentAccess.new(params); end
11
+ def method_missing(method_name, *arguments); @attributes[method_name]; end
12
+ def as_json(*); @attributes; end
13
+ end
14
+
15
+ class PersistentArticleWithCastedItem
16
+ include Tire::Model::Persistence
17
+
18
+ property :title
19
+ property :author, :class => Author
20
+ property :stats
21
+ end
22
+
23
+ class PersistentArticleWithCastedCollection
24
+ include Tire::Model::Persistence
25
+
26
+ property :title
27
+ property :comments, :class => [Comment]
28
+ end