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,68 @@
1
+ module Tire
2
+ module Results
3
+
4
+ # Adds support for WillPaginate and Kaminari
5
+ #
6
+ module Pagination
7
+
8
+ def default_per_page
9
+ 10
10
+ end
11
+ module_function :default_per_page
12
+
13
+ def total_entries
14
+ @total
15
+ end
16
+
17
+ def per_page
18
+ (@options[:per_page] || @options[:size] || default_per_page ).to_i
19
+ end
20
+
21
+ def total_pages
22
+ ( @total.to_f / per_page ).ceil
23
+ end
24
+
25
+ def current_page
26
+ if @options[:page]
27
+ @options[:page].to_i
28
+ else
29
+ (per_page + @options[:from].to_i) / per_page
30
+ end
31
+ end
32
+
33
+ def previous_page
34
+ current_page > 1 ? (current_page - 1) : nil
35
+ end
36
+
37
+ def next_page
38
+ current_page < total_pages ? (current_page + 1) : nil
39
+ end
40
+
41
+ def offset
42
+ per_page * (current_page - 1)
43
+ end
44
+
45
+ def out_of_bounds?
46
+ current_page > total_pages
47
+ end
48
+
49
+ # Kaminari support
50
+ #
51
+ alias :limit_value :per_page
52
+ alias :total_count :total_entries
53
+ alias :num_pages :total_pages
54
+ alias :offset_value :offset
55
+
56
+ def first_page?
57
+ current_page == 1
58
+ end
59
+
60
+ def last_page?
61
+ current_page == total_pages
62
+ end
63
+
64
+
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+
3
+ def to_json(options=nil)
4
+ MultiJson.encode(self)
5
+ end unless respond_to?(:to_json)
6
+
7
+ alias_method :to_indexed_json, :to_json
8
+ end
@@ -0,0 +1 @@
1
+ require 'tire/rubyext/uri_escape'
@@ -0,0 +1,11 @@
1
+ # ActiveModel::Serialization Ruby < 1.9.x compatibility
2
+
3
+ class Symbol
4
+ def <=> other
5
+ self.to_s <=> other.to_s
6
+ end unless method_defined?(:'<=>')
7
+
8
+ def capitalize
9
+ to_s.capitalize
10
+ end unless method_defined?(:capitalize)
11
+ end
@@ -0,0 +1,74 @@
1
+ # Steal the URI escape/unescape compatibility layer from Rack
2
+ #
3
+ # See <http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html#method-c-encode_www_form_component>
4
+
5
+ # :stopdoc:
6
+
7
+ # Stolen from ruby core's uri/common.rb, with modifications to support 1.8.x
8
+ #
9
+ # https://github.com/ruby/ruby/blob/trunk/lib/uri/common.rb
10
+ #
11
+ #
12
+
13
+ module URI
14
+ TBLENCWWWCOMP_ = {} # :nodoc:
15
+ TBLDECWWWCOMP_ = {} # :nodoc:
16
+
17
+ # Encode given +s+ to URL-encoded form data.
18
+ #
19
+ # This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP
20
+ # (ASCII space) to + and converts others to %XX.
21
+ #
22
+ # This is an implementation of
23
+ # http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
24
+ #
25
+ # See URI.decode_www_form_component, URI.encode_www_form
26
+ def self.encode_www_form_component(s)
27
+ str = s.to_s
28
+ if RUBY_VERSION < "1.9" && $KCODE =~ /u/i
29
+ str.gsub(/([^ a-zA-Z0-9_.-]+)/) do
30
+ '%' + $1.unpack('H2' * Rack::Utils.bytesize($1)).join('%').upcase
31
+ end.tr(' ', '+')
32
+ else
33
+ if TBLENCWWWCOMP_.empty?
34
+ tbl = {}
35
+ 256.times do |i|
36
+ tbl[i.chr] = '%%%02X' % i
37
+ end
38
+ tbl[' '] = '+'
39
+ begin
40
+ TBLENCWWWCOMP_.replace(tbl)
41
+ TBLENCWWWCOMP_.freeze
42
+ rescue
43
+ end
44
+ end
45
+ str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
46
+ end
47
+ end
48
+
49
+ # Decode given +str+ of URL-encoded form data.
50
+ #
51
+ # This decods + to SP.
52
+ #
53
+ # See URI.encode_www_form_component, URI.decode_www_form
54
+ def self.decode_www_form_component(str, enc=nil)
55
+ if TBLDECWWWCOMP_.empty?
56
+ tbl = {}
57
+ 256.times do |i|
58
+ h, l = i>>4, i&15
59
+ tbl['%%%X%X' % [h, l]] = i.chr
60
+ tbl['%%%x%X' % [h, l]] = i.chr
61
+ tbl['%%%X%x' % [h, l]] = i.chr
62
+ tbl['%%%x%x' % [h, l]] = i.chr
63
+ end
64
+ tbl['+'] = ' '
65
+ begin
66
+ TBLDECWWWCOMP_.replace(tbl)
67
+ TBLDECWWWCOMP_.freeze
68
+ rescue
69
+ end
70
+ end
71
+ raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ =~ str
72
+ str.gsub(/\+|%[0-9a-fA-F]{2}/) {|m| TBLDECWWWCOMP_[m]}
73
+ end
74
+ end
@@ -0,0 +1,211 @@
1
+ module Tire
2
+ module Search
3
+ class SearchRequestFailed < StandardError; end
4
+
5
+ class Search
6
+
7
+ attr_reader :indices, :types, :query, :facets, :filters, :options, :explain, :script_fields
8
+
9
+ def initialize(indices=nil, options={}, &block)
10
+ if indices.is_a?(Hash)
11
+ set_indices_options(indices)
12
+ @indices = indices.keys
13
+ else
14
+ @indices = Array(indices)
15
+ end
16
+ @types = Array(options.delete(:type)).map { |type| Utils.escape(type) }
17
+ @options = options
18
+
19
+ @path = ['/', @indices.join(','), @types.join(','), '_search'].compact.join('/').squeeze('/')
20
+
21
+ block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
22
+ end
23
+
24
+
25
+ def set_indices_options(indices)
26
+ indices.each do |index, index_options|
27
+ if index_options[:boost]
28
+ @indices_boost ||= {}
29
+ @indices_boost[index] = index_options[:boost]
30
+ end
31
+ end
32
+ end
33
+
34
+ def results
35
+ @results || (perform; @results)
36
+ end
37
+
38
+ def response
39
+ @response || (perform; @response)
40
+ end
41
+
42
+ def json
43
+ @json || (perform; @json)
44
+ end
45
+
46
+ def url
47
+ Configuration.url + @path
48
+ end
49
+
50
+ def params
51
+ options = @options.except(:wrapper)
52
+ options.empty? ? '' : '?' + options.to_param
53
+ end
54
+
55
+ def query(&block)
56
+ @query = Query.new
57
+ block.arity < 1 ? @query.instance_eval(&block) : block.call(@query)
58
+ self
59
+ end
60
+
61
+ def sort(&block)
62
+ @sort = Sort.new(&block).to_ary
63
+ self
64
+ end
65
+
66
+ def facet(name, options={}, &block)
67
+ @facets ||= {}
68
+ @facets.update Facet.new(name, options, &block).to_hash
69
+ self
70
+ end
71
+
72
+ def filter(type, *options)
73
+ @filters ||= []
74
+ @filters << Filter.new(type, *options).to_hash
75
+ self
76
+ end
77
+
78
+ def script_field(name, options={})
79
+ @script_fields ||= {}
80
+ @script_fields.merge! ScriptField.new(name, options).to_hash
81
+ self
82
+ end
83
+
84
+ def highlight(*args)
85
+ unless args.empty?
86
+ @highlight = Highlight.new(*args)
87
+ self
88
+ else
89
+ @highlight
90
+ end
91
+ end
92
+
93
+ def from(value)
94
+ @from = value
95
+ @options[:from] = value
96
+ self
97
+ end
98
+
99
+ def size(value)
100
+ @size = value
101
+ @options[:size] = value
102
+ self
103
+ end
104
+
105
+ def fields(*fields)
106
+ @fields = Array(fields.flatten)
107
+ self
108
+ end
109
+
110
+ def partial_field(name, options)
111
+ @partial_fields ||= {}
112
+ @partial_fields[name] = options
113
+ end
114
+
115
+ def explain(value)
116
+ @explain = value
117
+ self
118
+ end
119
+
120
+ def version(value)
121
+ @version = value
122
+ self
123
+ end
124
+
125
+ def min_score(value)
126
+ @min_score = value
127
+ self
128
+ end
129
+
130
+ def track_scores(value)
131
+ @track_scores = value
132
+ self
133
+ end
134
+
135
+ def perform
136
+ @response = Configuration.client.get(self.url + self.params, self.to_json)
137
+ if @response.failure?
138
+ STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
139
+ raise SearchRequestFailed, @response.to_s
140
+ end
141
+ @json = MultiJson.decode(@response.body)
142
+ @results = Results::Collection.new(@json, @options)
143
+ return self
144
+ ensure
145
+ logged
146
+ end
147
+
148
+ def to_curl
149
+ to_json_escaped = to_json.gsub("'",'\u0027')
150
+ %Q|curl -X GET '#{url}#{params.empty? ? '?' : params.to_s + '&'}pretty' -d '#{to_json_escaped}'|
151
+ end
152
+
153
+ def to_hash
154
+ @options[:payload] || begin
155
+ request = {}
156
+ request.update( { :indices_boost => @indices_boost } ) if @indices_boost
157
+ request.update( { :query => @query.to_hash } ) if @query
158
+ request.update( { :sort => @sort.to_ary } ) if @sort
159
+ request.update( { :facets => @facets.to_hash } ) if @facets
160
+ request.update( { :filter => @filters.first.to_hash } ) if @filters && @filters.size == 1
161
+ request.update( { :filter => { :and => @filters.map {|filter| filter.to_hash} } } ) if @filters && @filters.size > 1
162
+ request.update( { :highlight => @highlight.to_hash } ) if @highlight
163
+ request.update( { :size => @size } ) if @size
164
+ request.update( { :from => @from } ) if @from
165
+ request.update( { :fields => @fields } ) if @fields
166
+ request.update( { :partial_fields => @partial_fields } ) if @partial_fields
167
+ request.update( { :script_fields => @script_fields } ) if @script_fields
168
+ request.update( { :version => @version } ) if @version
169
+ request.update( { :explain => @explain } ) if @explain
170
+ request.update( { :min_score => @min_score } ) if @min_score
171
+ request.update( { :track_scores => @track_scores } ) if @track_scores
172
+ request
173
+ end
174
+ end
175
+
176
+ def to_json(options={})
177
+ payload = to_hash
178
+ # TODO: Remove when deprecated interface is removed
179
+ if payload.is_a?(String)
180
+ payload
181
+ else
182
+ MultiJson.encode(payload, :pretty => Configuration.pretty)
183
+ end
184
+ end
185
+
186
+ def logged(endpoint='_search')
187
+ if Configuration.logger
188
+
189
+ Configuration.logger.log_request endpoint, indices, to_curl
190
+
191
+ took = @json['took'] rescue nil
192
+ code = @response.code rescue nil
193
+
194
+ if Configuration.logger.level.to_s == 'debug'
195
+ body = if @json
196
+ MultiJson.encode( @json, :pretty => Configuration.pretty)
197
+ else
198
+ MultiJson.encode( MultiJson.load(@response.body), :pretty => Configuration.pretty) rescue ''
199
+ end
200
+ else
201
+ body = ''
202
+ end
203
+
204
+ Configuration.logger.log_response code || 'N/A', took || 'N/A', body || 'N/A'
205
+ end
206
+ end
207
+
208
+ end
209
+
210
+ end
211
+ end
@@ -0,0 +1,81 @@
1
+ module Tire
2
+ module Search
3
+
4
+ #--
5
+ # TODO: Implement all elastic search facets (geo, histogram, range, etc)
6
+ # http://elasticsearch.org/guide/reference/api/search/facets/
7
+ #++
8
+
9
+ class Facet
10
+
11
+ def initialize(name, options={}, &block)
12
+ @name = name
13
+ @options = options
14
+ @value = {}
15
+ block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
16
+ end
17
+
18
+ def terms(field, options={})
19
+ size = options.delete(:size) || 10
20
+ all_terms = options.delete(:all_terms) || false
21
+ @value[:terms] = if field.is_a?(Enumerable) and not field.is_a?(String)
22
+ { :fields => field }.update({ :size => size, :all_terms => all_terms }).update(options)
23
+ else
24
+ { :field => field }.update({ :size => size, :all_terms => all_terms }).update(options)
25
+ end
26
+ self
27
+ end
28
+
29
+ def date(field, options={})
30
+ interval = { :interval => options.delete(:interval) || 'day' }
31
+ fields = options[:value_field] || options[:value_script] ? { :key_field => field } : { :field => field }
32
+ @value[:date_histogram] = {}.update(fields).update(interval).update(options)
33
+ self
34
+ end
35
+
36
+ def range(field, ranges=[], options={})
37
+ @value[:range] = { :field => field, :ranges => ranges }.update(options)
38
+ self
39
+ end
40
+
41
+ def histogram(field, options={})
42
+ @value[:histogram] = (options.delete(:histogram) || {:field => field}.update(options))
43
+ self
44
+ end
45
+
46
+ def statistical(field, options={})
47
+ @value[:statistical] = (options.delete(:statistical) || {:field => field}.update(options))
48
+ self
49
+ end
50
+
51
+ def terms_stats(key_field, value_field, options={})
52
+ @value[:terms_stats] = {:key_field => key_field, :value_field => value_field}.update(options)
53
+ self
54
+ end
55
+
56
+ def query(&block)
57
+ @value[:query] = Query.new(&block).to_hash
58
+ end
59
+
60
+ def filter(type, options={})
61
+ @value[:filter] = Filter.new(type, options)
62
+ self
63
+ end
64
+
65
+ def facet_filter(type, *options)
66
+ @value[:facet_filter] = Filter.new(type, *options).to_hash
67
+ self
68
+ end
69
+
70
+ def to_json(options={})
71
+ to_hash.to_json
72
+ end
73
+
74
+ def to_hash
75
+ @value.update @options
76
+ { @name => @value }
77
+ end
78
+ end
79
+
80
+ end
81
+ end