oss_active_record 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8d98241b53d0f1f9dc6ee4dcb51ea5fa60f4aca
4
- data.tar.gz: 8284c8abaa036e7ee4c80b5ca3b808b540f15320
3
+ metadata.gz: 2f8e8e1561d5a73a94cdddbf76c5c759a7a3fa5f
4
+ data.tar.gz: 310cc970b75e8cc6a11b11324e29ca177ac964dc
5
5
  SHA512:
6
- metadata.gz: 451d6b0de1db3f12699cba08c5fa1c4e5cdf16bc456c5cf35abb64172dfa327ac5e97ae22e4d4b339a095db22e6a9fd14bb5fd9dbd9ec0f7cdc2865ee6b96bf0
7
- data.tar.gz: 2cdb7d185b3fdea92cb2b56fcda65241f5e092505a79fae9e8d9f0e5112f443d92ce260ce68dfc4331a7e2c49d9f379c8e3432c1ac11b99f34f1fcfcdf32b640
6
+ metadata.gz: 52842be27da1d798c3a4da10630de1334a969441b8ca65fd891ace53b0a1d9fc9e22327c2fce62007d36053cd666a418ab97ea754ae3776499fcb5d649a50bac
7
+ data.tar.gz: 3a90938372e32b123bd86481d5dcd60402106a4b6159f4aa8ba7eb9ce72b32bc6971c8d6bba9f56f67c188b24a022b5b993cc249785f312f3b87d820d4bbb35f
@@ -1,12 +1,10 @@
1
1
  module OssActiveRecord
2
2
  class SearchRequest
3
- def initialize(index, text_fields, sortable_fields)
4
- @index = index
3
+ def initialize(searchable)
5
4
  @params = {'start' => 0, 'rows' => 10}
6
- @filters = [];
7
- @text_fields = text_fields;
8
- @sortable_fields = sortable_fields;
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}:#{value}"}
15
+ @filters << {"type"=> "QueryFilter", "negative"=> negative, "query"=> "#{field}:(#{value})"}
18
16
  end
19
17
 
20
18
  def with(field, value)
21
- filter field, value, false
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
- filter field, value, true
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
- field = field == :score ? 'score' :@sortable_fields[field]
41
- @order_by[field.to_s] = direction.to_s.upcase unless field.nil?
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
- @text_fields.each do |key, value|
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 @index.search_field(@params)
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, Rails.configuration.open_search_server_url)
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 = { :text => 'StandardAnalyzer', :integer => 'DecimalAnalyzer'}
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.oss_index, @@_text_fields, @@_sortable_fields)
109
+ searchRequest = SearchRequest.new(self)
91
110
  searchRequest.returns @@_fields.map {|f|"#{f[:name]}|#{f[:type]}"}
92
- active_record_from_result searchRequest.execute(&block)
111
+ find_results searchRequest.execute(&block)
93
112
  end
94
113
 
95
- def get_ids_from_results(search_result)
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
- ids << field['values'].map {|f|f.to_i}.uniq if field['fieldName'] == id_field_name
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 ids
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
@@ -1,3 +1,3 @@
1
1
  module OssActiveRecord
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -5,6 +5,7 @@ class Article < ActiveRecord::Base
5
5
  text :title # fulltext
6
6
  string :title # order_by
7
7
  text :content #fulltext
8
+ integer :category_id
8
9
  time :updated_at
9
10
  end
10
11
 
@@ -27,7 +27,8 @@ class Folder
27
27
  end
28
28
 
29
29
  class Document < ActiveRecord::Base
30
- # belongs_to :current_revision
30
+
31
+ belongs_to :current_revision
31
32
  def self.folder
32
33
  Folder.new
33
34
  end
@@ -1 +1,3 @@
1
1
  Rails.configuration.open_search_server_url = "http://localhost:8080"
2
+ Rails.configuration.open_search_server_login = nil
3
+ Rails.configuration.open_search_server_apikey = nil
Binary file
@@ -3,6 +3,7 @@ class CreateArticles < ActiveRecord::Migration
3
3
  create_table :articles do |t|
4
4
  t.string :title
5
5
  t.string :content
6
+ t.integer :category_id
6
7
  t.time :updated_at
7
8
  t.timestamps
8
9
  end
@@ -16,6 +16,7 @@ ActiveRecord::Schema.define(version: 20130901161100) do
16
16
  create_table "articles", force: true do |t|
17
17
  t.string "title"
18
18
  t.string "content"
19
+ t.integer "category_id"
19
20
  t.datetime "updated_at"
20
21
  t.datetime "created_at"
21
22
  end
Binary file
@@ -150,3 +150,69 @@ Migrating to CreateArticles (20130901161100)
150
150
  SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130901161100"]]
151
151
   (1.1ms) commit transaction
152
152
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
153
+  (1.4ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) 
154
+  (1.3ms) 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
+  (1.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
156
+  (0.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
157
+  (0.1ms) SELECT version FROM "schema_migrations"
158
+  (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
159
+  (0.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')
160
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
161
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
162
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
163
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
164
+  (1.3ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) 
165
+  (1.0ms) 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
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
167
+  (0.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
168
+  (0.1ms) SELECT version FROM "schema_migrations"
169
+  (0.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
170
+  (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')
171
+  (1.3ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) 
172
+  (1.2ms) 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
+  (0.9ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
174
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
175
+  (0.1ms) SELECT version FROM "schema_migrations"
176
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
177
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')
178
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
179
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
180
+  (1.2ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) 
181
+  (0.9ms) 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
+  (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
183
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
184
+  (0.1ms) SELECT version FROM "schema_migrations"
185
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
186
+  (0.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')
187
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
188
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
189
+  (1.1ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "updated_at" datetime, "created_at" datetime) 
190
+  (1.2ms) 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
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
192
+  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
193
+  (0.1ms) SELECT version FROM "schema_migrations"
194
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
195
+  (0.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')
196
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
197
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
198
+  (1.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
199
+  (1.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
200
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
201
+ Migrating to CreateDocuments (20130711150619)
202
+  (0.1ms) begin transaction
203
+  (0.5ms) 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) 
204
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130711150619"]]
205
+  (0.8ms) commit transaction
206
+ Migrating to CreateArticles (20130901161100)
207
+  (0.1ms) begin transaction
208
+  (0.5ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) 
209
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20130901161100"]]
210
+  (1.3ms) commit transaction
211
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
212
+  (1.7ms) CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "content" varchar(255), "category_id" integer, "updated_at" datetime, "created_at" datetime) 
213
+  (1.4ms) 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
+  (1.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
215
+  (0.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
216
+  (0.1ms) SELECT version FROM "schema_migrations"
217
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130901161100')
218
+  (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20130711150619')