ssickles-tire 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +760 -0
- data/Rakefile +78 -0
- data/examples/rails-application-template.rb +249 -0
- data/examples/tire-dsl.rb +876 -0
- data/lib/tire.rb +52 -0
- data/lib/tire/alias.rb +296 -0
- data/lib/tire/configuration.rb +30 -0
- data/lib/tire/dsl.rb +43 -0
- data/lib/tire/http/client.rb +62 -0
- data/lib/tire/http/clients/curb.rb +61 -0
- data/lib/tire/http/response.rb +27 -0
- data/lib/tire/index.rb +345 -0
- data/lib/tire/logger.rb +60 -0
- data/lib/tire/model/callbacks.rb +40 -0
- data/lib/tire/model/import.rb +26 -0
- data/lib/tire/model/indexing.rb +128 -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 +143 -0
- data/lib/tire/model/persistence/finders.rb +66 -0
- data/lib/tire/model/persistence/storage.rb +71 -0
- data/lib/tire/model/search.rb +305 -0
- data/lib/tire/results/collection.rb +114 -0
- data/lib/tire/results/item.rb +83 -0
- data/lib/tire/results/pagination.rb +54 -0
- data/lib/tire/rubyext/hash.rb +8 -0
- data/lib/tire/rubyext/ruby_1_8.rb +54 -0
- data/lib/tire/rubyext/symbol.rb +11 -0
- data/lib/tire/search.rb +160 -0
- data/lib/tire/search/facet.rb +70 -0
- data/lib/tire/search/filter.rb +28 -0
- data/lib/tire/search/highlight.rb +37 -0
- data/lib/tire/search/query.rb +151 -0
- data/lib/tire/search/scan.rb +114 -0
- data/lib/tire/search/sort.rb +25 -0
- data/lib/tire/tasks.rb +135 -0
- data/lib/tire/utils.rb +17 -0
- data/lib/tire/version.rb +22 -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 +446 -0
- data/test/integration/boolean_queries_test.rb +43 -0
- data/test/integration/count_test.rb +34 -0
- data/test/integration/custom_score_queries_test.rb +88 -0
- data/test/integration/dsl_search_test.rb +22 -0
- data/test/integration/explanation_test.rb +44 -0
- data/test/integration/facets_test.rb +232 -0
- data/test/integration/filtered_queries_test.rb +66 -0
- data/test/integration/filters_test.rb +63 -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 +96 -0
- data/test/integration/mongoid_searchable_test.rb +309 -0
- data/test/integration/percolator_test.rb +111 -0
- data/test/integration/persistent_model_test.rb +117 -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 +46 -0
- data/test/integration/results_test.rb +39 -0
- data/test/integration/scan_test.rb +56 -0
- data/test/integration/sort_test.rb +36 -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 +122 -0
- data/test/models/article.rb +15 -0
- data/test/models/mongoid_models.rb +97 -0
- data/test/models/persistent_article.rb +11 -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 +11 -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 +88 -0
- data/test/unit/active_model_lint_test.rb +17 -0
- data/test/unit/configuration_test.rb +74 -0
- data/test/unit/http_client_test.rb +76 -0
- data/test/unit/http_response_test.rb +49 -0
- data/test/unit/index_alias_test.rb +275 -0
- data/test/unit/index_test.rb +841 -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 +71 -0
- data/test/unit/model_persistence_test.rb +516 -0
- data/test/unit/model_search_test.rb +899 -0
- data/test/unit/results_collection_test.rb +281 -0
- data/test/unit/results_item_test.rb +155 -0
- data/test/unit/rubyext_test.rb +60 -0
- data/test/unit/search_facet_test.rb +153 -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 +242 -0
- data/test/unit/search_scan_test.rb +113 -0
- data/test/unit/search_sort_test.rb +50 -0
- data/test/unit/search_test.rb +455 -0
- data/test/unit/tire_test.rb +126 -0
- data/tire.gemspec +85 -0
- metadata +506 -0
@@ -0,0 +1,122 @@
|
|
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
|
@@ -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
|
11
|
+
{ :id => @id, :title => @title, :body => @body }.to_json
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :to_indexed_json :to_json
|
15
|
+
end
|
@@ -0,0 +1,97 @@
|
|
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
|
+
# def index
|
12
|
+
# "KEEP OFF MY INDEX!!!"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# def self.settings
|
16
|
+
# "KEEP OFF MY SETTINGS!!!"
|
17
|
+
# end
|
18
|
+
|
19
|
+
include Tire::Model::Search
|
20
|
+
include Tire::Model::Callbacks
|
21
|
+
|
22
|
+
tire do
|
23
|
+
mapping do
|
24
|
+
indexes :title, :type => 'string', :boost => 10, :analyzer => 'snowball'
|
25
|
+
indexes :created_at, :type => 'date'
|
26
|
+
|
27
|
+
indexes :comments do
|
28
|
+
indexes :author
|
29
|
+
indexes :body
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# tire.mapping do
|
35
|
+
# indexes :title, :type => 'string', :boost => 10, :analyzer => 'snowball'
|
36
|
+
# end
|
37
|
+
|
38
|
+
def to_indexed_json
|
39
|
+
{
|
40
|
+
:title => title,
|
41
|
+
:length => length,
|
42
|
+
|
43
|
+
:comments => comments.map { |c| { :_type => 'mongoid_comment',
|
44
|
+
:_id => c.id,
|
45
|
+
:author => c.author,
|
46
|
+
:body => c.body } },
|
47
|
+
:stats => stats.map { |s| { :pageviews => s.pageviews } }
|
48
|
+
}.to_json
|
49
|
+
end
|
50
|
+
|
51
|
+
def length
|
52
|
+
title.length
|
53
|
+
end
|
54
|
+
|
55
|
+
def comment_authors
|
56
|
+
comments.map(&:author).to_sentence
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class MongoidComment
|
61
|
+
|
62
|
+
include Mongoid::Document
|
63
|
+
|
64
|
+
|
65
|
+
belongs_to :article, :class_name => "MongoidArticle", :foreign_key => "article_id"
|
66
|
+
end
|
67
|
+
|
68
|
+
class MongoidStat
|
69
|
+
|
70
|
+
include Mongoid::Document
|
71
|
+
|
72
|
+
|
73
|
+
belongs_to :article, :class_name => "MongoidArticle", :foreign_key => "article_id"
|
74
|
+
end
|
75
|
+
|
76
|
+
class MongoidClassWithTireMethods
|
77
|
+
|
78
|
+
include Mongoid::Document
|
79
|
+
|
80
|
+
|
81
|
+
def self.mapping
|
82
|
+
"THIS IS MY MAPPING!"
|
83
|
+
end
|
84
|
+
|
85
|
+
def index
|
86
|
+
"THIS IS MY INDEX!"
|
87
|
+
end
|
88
|
+
|
89
|
+
include Tire::Model::Search
|
90
|
+
include Tire::Model::Callbacks
|
91
|
+
|
92
|
+
tire do
|
93
|
+
mapping do
|
94
|
+
indexes :title, :type => 'string', :analyzer => 'snowball'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
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
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Example ActiveModel class for testing :searchable mode
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redis/persistence'
|
5
|
+
|
6
|
+
class SupermodelArticle
|
7
|
+
include Redis::Persistence
|
8
|
+
|
9
|
+
include Tire::Model::Search
|
10
|
+
include Tire::Model::Callbacks
|
11
|
+
|
12
|
+
property :title
|
13
|
+
|
14
|
+
mapping do
|
15
|
+
indexes :title, :type => 'string', :boost => 15, :analyzer => 'czech'
|
16
|
+
end
|
17
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
require 'yajl/json_gem'
|
8
|
+
require 'sqlite3'
|
9
|
+
|
10
|
+
require 'shoulda'
|
11
|
+
require 'turn/autorun' unless ENV["TM_FILEPATH"] || ENV["CI"] || defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
|
12
|
+
require 'mocha'
|
13
|
+
|
14
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
15
|
+
|
16
|
+
require 'tire'
|
17
|
+
|
18
|
+
# Require basic model files
|
19
|
+
#
|
20
|
+
require File.dirname(__FILE__) + '/models/active_model_article'
|
21
|
+
require File.dirname(__FILE__) + '/models/active_model_article_with_callbacks'
|
22
|
+
require File.dirname(__FILE__) + '/models/active_model_article_with_custom_document_type'
|
23
|
+
require File.dirname(__FILE__) + '/models/active_model_article_with_custom_index_name'
|
24
|
+
require File.dirname(__FILE__) + '/models/active_record_models'
|
25
|
+
require File.dirname(__FILE__) + '/models/article'
|
26
|
+
require File.dirname(__FILE__) + '/models/persistent_article'
|
27
|
+
require File.dirname(__FILE__) + '/models/persistent_article_in_namespace'
|
28
|
+
require File.dirname(__FILE__) + '/models/persistent_article_with_casting'
|
29
|
+
require File.dirname(__FILE__) + '/models/persistent_article_with_defaults'
|
30
|
+
require File.dirname(__FILE__) + '/models/persistent_articles_with_custom_index_name'
|
31
|
+
require File.dirname(__FILE__) + '/models/validated_model'
|
32
|
+
|
33
|
+
class Test::Unit::TestCase
|
34
|
+
|
35
|
+
def mock_response(body, code=200, headers={})
|
36
|
+
Tire::HTTP::Response.new(body, code, headers)
|
37
|
+
end
|
38
|
+
|
39
|
+
def fixtures_path
|
40
|
+
Pathname( File.expand_path( 'fixtures', File.dirname(__FILE__) ) )
|
41
|
+
end
|
42
|
+
|
43
|
+
def fixture_file(path)
|
44
|
+
File.read File.expand_path( path, fixtures_path )
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
module Test::Integration
|
50
|
+
URL = "http://localhost:9200"
|
51
|
+
|
52
|
+
def setup
|
53
|
+
begin; Object.send(:remove_const, :Rails); rescue; end
|
54
|
+
|
55
|
+
begin
|
56
|
+
::RestClient.get URL
|
57
|
+
rescue Errno::ECONNREFUSED
|
58
|
+
abort "\n\n#{'-'*87}\n[ABORTED] You have to run ElasticSearch on #{URL} for integration tests\n#{'-'*87}\n\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
::RestClient.delete "#{URL}/articles-test" rescue nil
|
62
|
+
::RestClient.post "#{URL}/articles-test", ''
|
63
|
+
fixtures_path.join('articles').entries.each do |f|
|
64
|
+
filename = f.to_s
|
65
|
+
next if filename =~ /^\./
|
66
|
+
::RestClient.put "#{URL}/articles-test/article/#{File.basename(filename, '.*')}",
|
67
|
+
fixtures_path.join('articles').join(f).read
|
68
|
+
end
|
69
|
+
::RestClient.post "#{URL}/articles-test/_refresh", ''
|
70
|
+
|
71
|
+
Dir[File.dirname(__FILE__) + '/models/**/*.rb'].each { |m| load m }
|
72
|
+
end
|
73
|
+
|
74
|
+
def teardown
|
75
|
+
%w[
|
76
|
+
articles-test
|
77
|
+
active_record_articles
|
78
|
+
active_model_article_with_custom_as_serializations
|
79
|
+
active_record_class_with_tire_methods
|
80
|
+
mongoid_articles
|
81
|
+
mongoid_class_with_tire_methods
|
82
|
+
supermodel_articles
|
83
|
+
dynamic_index
|
84
|
+
model_with_nested_documents ].each do |index|
|
85
|
+
::RestClient.delete "#{URL}/#{index}" rescue nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|