oss_active_record 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/oss_active_record/search_request.rb +13 -13
- data/lib/oss_active_record/searchable.rb +29 -13
- data/lib/oss_active_record/version.rb +1 -1
- data/test/dummy/app/models/article.rb +1 -0
- data/test/dummy/app/models/document.rb +2 -1
- data/test/dummy/config/initializers/oss_active_record.rb +2 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20130901161100_create_articles.rb +1 -0
- data/test/dummy/db/schema.rb +1 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +66 -0
- data/test/dummy/log/test.log +5190 -0
- data/test/dummy/test/fixtures/articles.yml +13 -4
- data/test/dummy/test/fixtures/documents.yml +9 -9
- data/test/dummy/test/models/article_test.rb +43 -13
- data/test/dummy/test/models/document_test.rb +12 -6
- data/test/e +0 -0
- metadata +26 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f8e8e1561d5a73a94cdddbf76c5c759a7a3fa5f
|
4
|
+
data.tar.gz: 310cc970b75e8cc6a11b11324e29ca177ac964dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52842be27da1d798c3a4da10630de1334a969441b8ca65fd891ace53b0a1d9fc9e22327c2fce62007d36053cd666a418ab97ea754ae3776499fcb5d649a50bac
|
7
|
+
data.tar.gz: 3a90938372e32b123bd86481d5dcd60402106a4b6159f4aa8ba7eb9ce72b32bc6971c8d6bba9f56f67c188b24a022b5b993cc249785f312f3b87d820d4bbb35f
|
@@ -1,12 +1,10 @@
|
|
1
1
|
module OssActiveRecord
|
2
2
|
class SearchRequest
|
3
|
-
def initialize(
|
4
|
-
@index = index
|
3
|
+
def initialize(searchable)
|
5
4
|
@params = {'start' => 0, 'rows' => 10}
|
6
|
-
@filters = []
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@order_by = {};
|
5
|
+
@filters = []
|
6
|
+
@searchable = searchable
|
7
|
+
@order_by = {}
|
10
8
|
end
|
11
9
|
|
12
10
|
def fulltext(keywords, &block)
|
@@ -14,15 +12,17 @@ module OssActiveRecord
|
|
14
12
|
end
|
15
13
|
|
16
14
|
def filter(field, value, negative)
|
17
|
-
@filters << {"type"=> "QueryFilter", "negative"=> negative, "query"=> "#{field}
|
15
|
+
@filters << {"type"=> "QueryFilter", "negative"=> negative, "query"=> "#{field}:(#{value})"}
|
18
16
|
end
|
19
17
|
|
20
18
|
def with(field, value)
|
21
|
-
|
19
|
+
index_field = @searchable.find_field_name(field)
|
20
|
+
filter index_field, value, false unless index_field.nil?
|
22
21
|
end
|
23
22
|
|
24
23
|
def without(field, value)
|
25
|
-
|
24
|
+
index_field = @searchable.find_field_name(field)
|
25
|
+
filter index_field, value, true unless index_field.nil?
|
26
26
|
end
|
27
27
|
|
28
28
|
def returns(fields)
|
@@ -37,15 +37,15 @@ module OssActiveRecord
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def order_by(field = nil, direction = :asc)
|
40
|
-
|
41
|
-
@order_by[
|
40
|
+
index_field = field == :score ? 'score' :@searchable.find_sortable_name(field)
|
41
|
+
@order_by[index_field] = direction.to_s.upcase unless index_field.nil?
|
42
42
|
end
|
43
43
|
|
44
44
|
def execute(&block)
|
45
45
|
self.instance_eval(&block) unless block.nil?
|
46
46
|
@params['filters'] = @filters unless @filters.length == 0
|
47
47
|
fields = []
|
48
|
-
@
|
48
|
+
@searchable._text_fields.each do |key, value|
|
49
49
|
fields<<{ "field"=> value,"phrase"=> true,"boost"=> 1.0}
|
50
50
|
end
|
51
51
|
@params['searchFields'] = fields unless fields.length == 0
|
@@ -54,7 +54,7 @@ module OssActiveRecord
|
|
54
54
|
sorts<<{ "field"=> key,"direction"=> value}
|
55
55
|
end
|
56
56
|
@params['sorts'] = sorts unless sorts.length == 0
|
57
|
-
return @
|
57
|
+
return @searchable.oss_index.search_field(@params)
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
@@ -17,12 +17,25 @@ module OssActiveRecord
|
|
17
17
|
@@_field_id = nil
|
18
18
|
@@_text_fields = {}
|
19
19
|
@@_sortable_fields = {}
|
20
|
+
@@_all_fields = {}
|
20
21
|
@@index = nil
|
21
22
|
|
23
|
+
def _text_fields
|
24
|
+
@@_text_fields
|
25
|
+
end
|
26
|
+
|
22
27
|
def _fields
|
23
28
|
@@_fields
|
24
29
|
end
|
25
30
|
|
31
|
+
def find_sortable_name(field_name)
|
32
|
+
field == :score ? 'score' : @@_sortable_fields[field_name] unless field_name.nil?
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_field_name(field_name)
|
36
|
+
@@_all_fields[field_name] unless field_name.nil?
|
37
|
+
end
|
38
|
+
|
26
39
|
def searchable(options = {}, &block)
|
27
40
|
yield
|
28
41
|
unless options[:auto_index] == false
|
@@ -33,7 +46,10 @@ module OssActiveRecord
|
|
33
46
|
def oss_index
|
34
47
|
if @@index.nil?
|
35
48
|
@@index_name ||= self.name.downcase
|
36
|
-
@@index = Oss::Index.new(@@index_name,
|
49
|
+
@@index = Oss::Index.new(@@index_name,
|
50
|
+
Rails.configuration.open_search_server_url,
|
51
|
+
Rails.configuration.open_search_server_login,
|
52
|
+
Rails.configuration.open_search_server_apikey)
|
37
53
|
create_schema!
|
38
54
|
end
|
39
55
|
@@index
|
@@ -63,7 +79,10 @@ module OssActiveRecord
|
|
63
79
|
end
|
64
80
|
|
65
81
|
def create_schema_field!(field)
|
66
|
-
analyzers = {
|
82
|
+
analyzers = {
|
83
|
+
:text => 'StandardAnalyzer',
|
84
|
+
:integer => 'IntegerAnalyzer',
|
85
|
+
:decimal => 'DecimalAnalyzer'}
|
67
86
|
analyzer = analyzers[field[:type]] if field[:name] != :id
|
68
87
|
termVectors = { :text => 'POSITIONS_OFFSETS'}
|
69
88
|
termVector = termVectors[field[:type]] || 'NO'
|
@@ -77,34 +96,31 @@ module OssActiveRecord
|
|
77
96
|
}
|
78
97
|
@@_text_fields[field[:name]] = name if field[:type] == :text
|
79
98
|
@@_sortable_fields[field[:name]] = name unless field[:type] == :text
|
99
|
+
@@_all_fields[field[:name]] = name
|
80
100
|
self.oss_index.set_field(params)
|
81
101
|
self.oss_index.set_field_default_unique(name, name) if field[:name] == :id
|
82
102
|
end
|
83
103
|
|
84
104
|
def method_missing(method, *args, &block)
|
85
|
-
yield unless block.nil?
|
86
105
|
add_field args[0], method, block if @@field_types.include? method
|
87
106
|
end
|
88
107
|
|
89
108
|
def search(*args, &block)
|
90
|
-
searchRequest = SearchRequest.new(self
|
109
|
+
searchRequest = SearchRequest.new(self)
|
91
110
|
searchRequest.returns @@_fields.map {|f|"#{f[:name]}|#{f[:type]}"}
|
92
|
-
|
111
|
+
find_results searchRequest.execute(&block)
|
93
112
|
end
|
94
113
|
|
95
|
-
def
|
96
|
-
ids = []
|
114
|
+
def find_results(search_result)
|
97
115
|
id_field_name = "#{@@_field_id[:name]}|#{@@_field_id[:type]}"
|
116
|
+
results = []
|
98
117
|
search_result['documents'].each do |document|
|
99
118
|
document['fields'].each do |field|
|
100
|
-
|
119
|
+
id = field['values'].map {|f|f.to_i}.uniq if field['fieldName'] == id_field_name
|
120
|
+
results<<find(id)[0] unless id.nil?
|
101
121
|
end
|
102
122
|
end
|
103
|
-
return
|
104
|
-
end
|
105
|
-
|
106
|
-
def active_record_from_result(search_result)
|
107
|
-
find get_ids_from_results(search_result)
|
123
|
+
return results
|
108
124
|
end
|
109
125
|
|
110
126
|
end
|
Binary file
|
data/test/dummy/db/schema.rb
CHANGED
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -150,3 +150,69 @@ Migrating to CreateArticles (20130901161100)
|
|
150
150
|
[1m[35mSQL (0.1ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130901161100"]]
|
151
151
|
[1m[36m (1.1ms)[0m [1mcommit transaction[0m
|
152
152
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
153
|
+
[1m[36m (1.4ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) [0m
|
154
|
+
[1m[35m (1.3ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
155
|
+
[1m[36m (1.3ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
156
|
+
[1m[35m (0.8ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
157
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
158
|
+
[1m[35m (0.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
159
|
+
[1m[36m (0.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|
160
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
161
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
162
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
163
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
164
|
+
[1m[36m (1.3ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) [0m
|
165
|
+
[1m[35m (1.0ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
166
|
+
[1m[36m (1.2ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
167
|
+
[1m[35m (0.8ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
168
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
169
|
+
[1m[35m (0.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
170
|
+
[1m[36m (0.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|
171
|
+
[1m[36m (1.3ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) [0m
|
172
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
173
|
+
[1m[36m (0.9ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
174
|
+
[1m[35m (1.0ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
175
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
176
|
+
[1m[35m (0.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
177
|
+
[1m[36m (0.8ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|
178
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
179
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
180
|
+
[1m[36m (1.2ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) [0m
|
181
|
+
[1m[35m (0.9ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
182
|
+
[1m[36m (1.6ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
183
|
+
[1m[35m (1.0ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
184
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
185
|
+
[1m[35m (0.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
186
|
+
[1m[36m (0.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|
187
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
188
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
189
|
+
[1m[36m (1.1ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) [0m
|
190
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
191
|
+
[1m[36m (1.2ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
192
|
+
[1m[35m (0.9ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
193
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
194
|
+
[1m[35m (0.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
195
|
+
[1m[36m (0.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|
196
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
197
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
198
|
+
[1m[36m (1.5ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
199
|
+
[1m[35m (1.7ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
200
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
201
|
+
Migrating to CreateDocuments (20130711150619)
|
202
|
+
[1m[35m (0.1ms)[0m begin transaction
|
203
|
+
[1m[36m (0.5ms)[0m [1mCREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime) [0m
|
204
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130711150619"]]
|
205
|
+
[1m[36m (0.8ms)[0m [1mcommit transaction[0m
|
206
|
+
Migrating to CreateArticles (20130901161100)
|
207
|
+
[1m[35m (0.1ms)[0m begin transaction
|
208
|
+
[1m[36m (0.5ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) [0m
|
209
|
+
[1m[35mSQL (0.1ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130901161100"]]
|
210
|
+
[1m[36m (1.3ms)[0m [1mcommit transaction[0m
|
211
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
212
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) [0m
|
213
|
+
[1m[35m (1.4ms)[0m CREATE TABLE "documents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "folder_id" integer, "room_id" integer, "name" varchar(255), "updated_at" datetime, "uuid" integer, "user_id" integer, "file_size" integer, "file_content_type" varchar(255), "state" varchar(255), "created_at" datetime)
|
214
|
+
[1m[36m (1.0ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
215
|
+
[1m[35m (0.8ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
216
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
217
|
+
[1m[35m (0.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
|
218
|
+
[1m[36m (0.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130711150619')[0m
|