rroonga 2.0.4 → 2.0.5
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.
- data/Rakefile +21 -2
- data/bin/grndump +3 -2
- data/ext/groonga/extconf.rb +1 -6
- data/ext/groonga/rb-grn-array.c +58 -31
- data/ext/groonga/rb-grn-column.c +220 -148
- data/ext/groonga/rb-grn-context.c +46 -32
- data/ext/groonga/rb-grn-database.c +102 -90
- data/ext/groonga/rb-grn-double-array-trie.c +205 -163
- data/ext/groonga/rb-grn-encoding-support.c +2 -3
- data/ext/groonga/rb-grn-encoding.c +13 -8
- data/ext/groonga/rb-grn-exception.c +1 -1
- data/ext/groonga/rb-grn-expression.c +110 -133
- data/ext/groonga/rb-grn-fix-size-column.c +5 -4
- data/ext/groonga/rb-grn-geo-point.c +55 -0
- data/ext/groonga/rb-grn-hash.c +120 -82
- data/ext/groonga/rb-grn-index-column.c +66 -70
- data/ext/groonga/rb-grn-index-cursor.c +3 -0
- data/ext/groonga/rb-grn-logger.c +33 -51
- data/ext/groonga/rb-grn-object.c +2 -0
- data/ext/groonga/rb-grn-operator.c +1 -1
- data/ext/groonga/rb-grn-patricia-trie.c +287 -232
- data/ext/groonga/rb-grn-plugin.c +11 -14
- data/ext/groonga/rb-grn-snippet.c +37 -54
- data/ext/groonga/rb-grn-table-cursor-key-support.c +3 -3
- data/ext/groonga/rb-grn-table-cursor.c +8 -6
- data/ext/groonga/rb-grn-table-key-support.c +22 -29
- data/ext/groonga/rb-grn-table.c +355 -279
- data/ext/groonga/rb-grn-type.c +18 -25
- data/ext/groonga/rb-grn-utils.c +77 -7
- data/ext/groonga/rb-grn-variable-size-column.c +12 -20
- data/ext/groonga/rb-grn-view.c +56 -51
- data/ext/groonga/rb-grn.h +28 -1
- data/ext/groonga/rb-groonga.c +1 -0
- data/lib/groonga.rb +2 -0
- data/lib/groonga/command.rb +3 -1
- data/lib/groonga/database.rb +27 -0
- data/lib/groonga/dumper.rb +96 -17
- data/lib/groonga/geo-point.rb +216 -0
- data/lib/groonga/schema.rb +29 -46
- data/rroonga-build.rb +1 -1
- data/rroonga.gemspec +90 -0
- data/test/groonga-test-utils.rb +168 -0
- data/test/run-test.rb +60 -0
- data/test/test-accessor.rb +36 -0
- data/test/test-array.rb +146 -0
- data/test/test-column.rb +350 -0
- data/test/test-command-select.rb +181 -0
- data/test/test-context.rb +130 -0
- data/test/test-database-dumper.rb +259 -0
- data/test/test-database.rb +173 -0
- data/test/test-double-array-trie.rb +189 -0
- data/test/test-encoding.rb +33 -0
- data/test/test-exception.rb +230 -0
- data/test/test-expression-builder.rb +602 -0
- data/test/test-expression.rb +134 -0
- data/test/test-fix-size-column.rb +77 -0
- data/test/test-geo-point.rb +190 -0
- data/test/test-gqtp.rb +70 -0
- data/test/test-hash.rb +367 -0
- data/test/test-index-column.rb +180 -0
- data/test/test-index-cursor.rb +123 -0
- data/test/test-logger.rb +37 -0
- data/test/test-pagination.rb +249 -0
- data/test/test-patricia-trie.rb +440 -0
- data/test/test-plugin.rb +35 -0
- data/test/test-procedure.rb +37 -0
- data/test/test-query-log.rb +258 -0
- data/test/test-record.rb +577 -0
- data/test/test-remote.rb +63 -0
- data/test/test-schema-create-table.rb +267 -0
- data/test/test-schema-dumper.rb +235 -0
- data/test/test-schema-type.rb +208 -0
- data/test/test-schema-view.rb +90 -0
- data/test/test-schema.rb +886 -0
- data/test/test-snippet.rb +130 -0
- data/test/test-table-dumper.rb +353 -0
- data/test/test-table-offset-and-limit.rb +100 -0
- data/test/test-table-select-mecab.rb +80 -0
- data/test/test-table-select-normalize.rb +57 -0
- data/test/test-table-select-weight.rb +123 -0
- data/test/test-table-select.rb +191 -0
- data/test/test-table-traverse.rb +304 -0
- data/test/test-table.rb +711 -0
- data/test/test-type.rb +79 -0
- data/test/test-variable-size-column.rb +147 -0
- data/test/test-variable.rb +28 -0
- data/test/test-vector-column.rb +76 -0
- data/test/test-version.rb +61 -0
- data/test/test-view.rb +72 -0
- metadata +330 -202
data/test/test-hash.rb
ADDED
@@ -0,0 +1,367 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2009-2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License version 2.1 as published by the Free Software Foundation.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
17
|
+
|
18
|
+
class HashTest < Test::Unit::TestCase
|
19
|
+
include GroongaTestUtils
|
20
|
+
|
21
|
+
setup :setup_database
|
22
|
+
|
23
|
+
def test_support_key?
|
24
|
+
assert_predicate(Groonga::Hash.create(:name => "Users",
|
25
|
+
:key_type => "ShortText"),
|
26
|
+
:support_key?)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_delete
|
30
|
+
bookmarks_path = @tables_dir + "bookmarks"
|
31
|
+
bookmarks = Groonga::Hash.create(:name => "Bookmarks",
|
32
|
+
:path => bookmarks_path.to_s)
|
33
|
+
|
34
|
+
bookmarks.add("groonga")
|
35
|
+
google = bookmarks.add("Google")
|
36
|
+
cutter = bookmarks.add("Cutter")
|
37
|
+
|
38
|
+
assert_equal(["groonga", "Google", "Cutter"],
|
39
|
+
bookmarks.collect {|bookmark| bookmark.key})
|
40
|
+
|
41
|
+
bookmarks.delete(google.id)
|
42
|
+
assert_equal(["groonga", "Cutter"],
|
43
|
+
bookmarks.collect {|bookmark| bookmark.key})
|
44
|
+
|
45
|
+
bookmarks.delete(cutter.key)
|
46
|
+
assert_equal(["groonga"],
|
47
|
+
bookmarks.collect {|bookmark| bookmark.key})
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_value
|
51
|
+
bookmarks_path = @tables_dir + "bookmarks"
|
52
|
+
bookmarks = Groonga::Hash.create(:name => "Bookmarks",
|
53
|
+
:path => bookmarks_path.to_s,
|
54
|
+
:key_type => "ShortText",
|
55
|
+
:value_type => "UInt32")
|
56
|
+
bookmarks.set_value("http://google.com/", 29)
|
57
|
+
assert_equal(29, bookmarks.value("http://google.com/"))
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_column_value
|
61
|
+
bookmarks_path = @tables_dir + "bookmarks"
|
62
|
+
bookmarks = Groonga::Hash.create(:name => "Bookmarks",
|
63
|
+
:path => bookmarks_path.to_s,
|
64
|
+
:key_type => "ShortText")
|
65
|
+
bookmarks.define_column("uri", "ShortText")
|
66
|
+
bookmarks.set_column_value("google", "uri", "http://google.com/")
|
67
|
+
assert_equal("http://google.com/", bookmarks.column_value("google", "uri"))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_array_reference
|
71
|
+
bookmarks_path = @tables_dir + "bookmarks"
|
72
|
+
bookmarks = Groonga::Hash.create(:name => "bookmarks",
|
73
|
+
:path => bookmarks_path.to_s,
|
74
|
+
:key_type => "ShortText")
|
75
|
+
bookmark = bookmarks.add("http://google.com/")
|
76
|
+
assert_equal(bookmark, bookmarks["http://google.com/"])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_inspect_anonymous
|
80
|
+
path = @tables_dir + "anoymous.groonga"
|
81
|
+
anonymous_table = Groonga::Hash.create(:path => path.to_s)
|
82
|
+
assert_equal("#<Groonga::Hash " +
|
83
|
+
"id: <#{anonymous_table.id}>, " +
|
84
|
+
"name: (anonymous), " +
|
85
|
+
"path: <#{path}>, " +
|
86
|
+
"domain: <ShortText>, " +
|
87
|
+
"range: (nil), " +
|
88
|
+
"flags: <>, " +
|
89
|
+
"encoding: <#{encoding.inspect}>, " +
|
90
|
+
"size: <0>>",
|
91
|
+
anonymous_table.inspect)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_inspect_anonymous_temporary
|
95
|
+
anonymous_table = Groonga::Hash.create
|
96
|
+
assert_equal("#<Groonga::Hash " +
|
97
|
+
"id: <#{anonymous_table.id}>, " +
|
98
|
+
"name: (anonymous), " +
|
99
|
+
"path: (temporary), " +
|
100
|
+
"domain: <ShortText>, " +
|
101
|
+
"range: (nil), " +
|
102
|
+
"flags: <>, " +
|
103
|
+
"encoding: <#{encoding.inspect}>, " +
|
104
|
+
"size: <0>>",
|
105
|
+
anonymous_table.inspect)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_inspect_named
|
109
|
+
path = @tables_dir + "named.groonga"
|
110
|
+
named_table = Groonga::Hash.create(:name => "Users", :path => path.to_s)
|
111
|
+
assert_equal("#<Groonga::Hash " +
|
112
|
+
"id: <#{named_table.id}>, " +
|
113
|
+
"name: <Users>, " +
|
114
|
+
"path: <#{path}>, " +
|
115
|
+
"domain: <ShortText>, " +
|
116
|
+
"range: (nil), " +
|
117
|
+
"flags: <>, " +
|
118
|
+
"encoding: <#{encoding.inspect}>, " +
|
119
|
+
"size: <0>>",
|
120
|
+
named_table.inspect)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_inspect_temporary
|
124
|
+
named_table = Groonga::Hash.create
|
125
|
+
assert_equal("#<Groonga::Hash " +
|
126
|
+
"id: <#{named_table.id}>, " +
|
127
|
+
"name: (anonymous), " +
|
128
|
+
"path: (temporary), " +
|
129
|
+
"domain: <ShortText>, " +
|
130
|
+
"range: (nil), " +
|
131
|
+
"flags: <>, " +
|
132
|
+
"encoding: <#{encoding.inspect}>, " +
|
133
|
+
"size: <0>>",
|
134
|
+
named_table.inspect)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_encoding
|
138
|
+
assert_equal(Groonga::Encoding.default,
|
139
|
+
Groonga::Hash.create.encoding)
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_tokenizer
|
143
|
+
hash = Groonga::Hash.create
|
144
|
+
assert_nil(hash.default_tokenizer)
|
145
|
+
hash.default_tokenizer = "TokenBigram"
|
146
|
+
assert_equal(Groonga::Context.default["TokenBigram"],
|
147
|
+
hash.default_tokenizer)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_search
|
151
|
+
users = Groonga::Array.create(:name => "Users")
|
152
|
+
users.define_column("name", "ShortText")
|
153
|
+
|
154
|
+
bookmarks = Groonga::Hash.create(:name => "Bookmarks",
|
155
|
+
:key_type => "ShortText")
|
156
|
+
bookmarks.define_column("user_id", users)
|
157
|
+
|
158
|
+
daijiro = users.add
|
159
|
+
daijiro["name"] = "daijiro"
|
160
|
+
gunyarakun = users.add
|
161
|
+
gunyarakun["name"] = "gunyarakun"
|
162
|
+
|
163
|
+
groonga = bookmarks.add("http://groonga.org/")
|
164
|
+
groonga["user_id"] = daijiro
|
165
|
+
|
166
|
+
records = bookmarks.search("http://groonga.org/")
|
167
|
+
assert_equal(["daijiro"],
|
168
|
+
records.records.collect {|record| record["user_id.name"]})
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_add
|
172
|
+
users = Groonga::Hash.create(:name => "Users")
|
173
|
+
users.define_column("address", "Text")
|
174
|
+
me = users.add("me", :address => "me@example.com")
|
175
|
+
assert_equal("me@example.com", me[:address])
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_default_tokenizer_on_create
|
179
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
180
|
+
:default_tokenizer => "TokenTrigram")
|
181
|
+
assert_equal(context[Groonga::Type::TRIGRAM],
|
182
|
+
terms.default_tokenizer)
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_duplicated_name
|
186
|
+
Groonga::Hash.create(:name => "Users")
|
187
|
+
assert_raise(Groonga::InvalidArgument) do
|
188
|
+
Groonga::Hash.create(:name => "Users")
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_define_index_column_implicit_with_position
|
193
|
+
bookmarks = Groonga::Hash.create(:name => "Bookmarks")
|
194
|
+
bookmarks.define_column("comment", "Text")
|
195
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
196
|
+
:default_tokenizer => "TokenBigram")
|
197
|
+
index = terms.define_index_column("comment", bookmarks,
|
198
|
+
:source => "Bookmarks.comment")
|
199
|
+
bookmarks.add("groonga", :comment => "search engine by Brazil")
|
200
|
+
bookmarks.add("google", :comment => "search engine by Google")
|
201
|
+
bookmarks.add("ruby", :comment => "programing language")
|
202
|
+
|
203
|
+
assert_equal(["groonga", "google"],
|
204
|
+
index.search("engine").collect {|record| record.key.key})
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_has_key?
|
208
|
+
users = Groonga::Hash.create(:name => "Users")
|
209
|
+
assert_false(users.has_key?("morita"))
|
210
|
+
users.add("morita")
|
211
|
+
assert_true(users.has_key?("morita"))
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_big_key
|
215
|
+
hash = Groonga::Hash.create(:key_type => "UInt64")
|
216
|
+
assert_nothing_raised do
|
217
|
+
hash.add(1 << 63)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_value_by_key
|
222
|
+
users = Groonga::Hash.create(:key_type => "ShortText",
|
223
|
+
:value_type => "Int32")
|
224
|
+
key = "niku"
|
225
|
+
users.add(key)
|
226
|
+
users.set_value(key, 29)
|
227
|
+
assert_equal(29, users.value(key))
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_value_by_id
|
231
|
+
users = Groonga::Hash.create(:key_type => "ShortText",
|
232
|
+
:value_type => "Int32")
|
233
|
+
user_id = users.add("niku").id
|
234
|
+
users.set_value(user_id, 29, :id => true)
|
235
|
+
assert_equal(29, users.value(user_id, :id => true))
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_id
|
239
|
+
users = Groonga::Hash.create(:key_type => "ShortText")
|
240
|
+
|
241
|
+
key = "niku"
|
242
|
+
assert_nil(users.id(key))
|
243
|
+
user_id = users.add(key).id
|
244
|
+
assert_equal(user_id, users.id(key))
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_set_multi_values
|
248
|
+
users = Groonga::Hash.create(:name => "Users",
|
249
|
+
:key_type => "ShortText")
|
250
|
+
users.define_column("self_introduction", "ShortText")
|
251
|
+
users.define_column("age", "UInt32")
|
252
|
+
|
253
|
+
key = "niku"
|
254
|
+
niku = users.add(key)
|
255
|
+
assert_equal({
|
256
|
+
"_id" => niku.id,
|
257
|
+
"_key" => key,
|
258
|
+
"self_introduction" => nil,
|
259
|
+
"age" => 0,
|
260
|
+
},
|
261
|
+
niku.attributes)
|
262
|
+
users[key] = {
|
263
|
+
"self_introduction" => "I'm a meet lover.",
|
264
|
+
"age" => 29
|
265
|
+
}
|
266
|
+
assert_equal({
|
267
|
+
"_id" => niku.id,
|
268
|
+
"_key" => key,
|
269
|
+
"self_introduction" => "I'm a meet lover.",
|
270
|
+
"age" => 29,
|
271
|
+
},
|
272
|
+
niku.attributes)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_set_multi_values_for_nonexistent_record
|
276
|
+
users = Groonga::Hash.create(:name => "Users",
|
277
|
+
:key_type => "ShortText")
|
278
|
+
users.define_column("self_introduction", "ShortText")
|
279
|
+
users.define_column("age", "UInt32")
|
280
|
+
|
281
|
+
key = "niku"
|
282
|
+
users[key] = {
|
283
|
+
"self_introduction" => "I'm a meet lover.",
|
284
|
+
"age" => 29
|
285
|
+
}
|
286
|
+
|
287
|
+
assert_equal({
|
288
|
+
"_id" => users[key].id,
|
289
|
+
"_key" => key,
|
290
|
+
"self_introduction" => "I'm a meet lover.",
|
291
|
+
"age" => 29,
|
292
|
+
},
|
293
|
+
users[key].attributes)
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_set_multi_values_with_nonexistent_column
|
297
|
+
users = Groonga::Hash.create(:name => "Users",
|
298
|
+
:key_type => "ShortText")
|
299
|
+
users.define_column("self_introduction", "ShortText")
|
300
|
+
users.define_column("age", "UInt32")
|
301
|
+
|
302
|
+
key = "niku"
|
303
|
+
inspected_users = users.inspect.sub(/size: <0>/, "size: <1>")
|
304
|
+
message = "no such column: <\"nonexistent\">: <#{inspected_users}>"
|
305
|
+
assert_raise(Groonga::NoSuchColumn.new(message)) do
|
306
|
+
users[key] = {
|
307
|
+
"nonexistent" => "No!",
|
308
|
+
}
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_added?
|
313
|
+
users = Groonga::Hash.create(:name => "Users",
|
314
|
+
:key_type => "ShortText")
|
315
|
+
bob = users.add("bob")
|
316
|
+
assert_predicate(bob, :added?)
|
317
|
+
bob_again = users.add("bob")
|
318
|
+
assert_not_predicate(bob_again, :added?)
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_defrag
|
322
|
+
users = Groonga::Hash.create(:name => "Users",
|
323
|
+
:key_type => "ShortText")
|
324
|
+
users.define_column("name", "ShortText")
|
325
|
+
users.define_column("address", "ShortText")
|
326
|
+
1000.times do |i|
|
327
|
+
users.add("user #{i}",
|
328
|
+
:name => "user #{i}" * 1000,
|
329
|
+
:address => "address #{i}" * 1000)
|
330
|
+
end
|
331
|
+
assert_equal(7, users.defrag)
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_rename
|
335
|
+
users = Groonga::Hash.create(:name => "Users",
|
336
|
+
:key_type => "ShortText")
|
337
|
+
name = users.define_column("name", "ShortText")
|
338
|
+
address = users.define_column("address", "ShortText")
|
339
|
+
|
340
|
+
users.rename("People")
|
341
|
+
assert_equal(["People", "People.name", "People.address"],
|
342
|
+
[users.name, name.name, address.name])
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_each
|
346
|
+
users = Groonga::Hash.create(:name => "Users", :key_type => "ShortText")
|
347
|
+
users.add("Alice")
|
348
|
+
users.add("Bob")
|
349
|
+
users.add("Carl")
|
350
|
+
|
351
|
+
user_names = []
|
352
|
+
users.each do |user|
|
353
|
+
user_names << user.key
|
354
|
+
end
|
355
|
+
assert_equal(["Alice", "Bob", "Carl"], user_names)
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_each_without_block
|
359
|
+
users = Groonga::Hash.create(:name => "Users", :key_type => "ShortText")
|
360
|
+
users.add("Alice")
|
361
|
+
users.add("Bob")
|
362
|
+
users.add("Carl")
|
363
|
+
|
364
|
+
user_names = users.each.collect(&:key)
|
365
|
+
assert_equal(["Alice", "Bob", "Carl"], user_names)
|
366
|
+
end
|
367
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2009-2011 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License version 2.1 as published by the Free Software Foundation.
|
8
|
+
#
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12
|
+
# Lesser General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
15
|
+
# License along with this library; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
17
|
+
|
18
|
+
class IndexColumnTest < Test::Unit::TestCase
|
19
|
+
include GroongaTestUtils
|
20
|
+
|
21
|
+
def setup
|
22
|
+
setup_database
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_index?
|
26
|
+
articles = Groonga::Array.create(:name => "Articles")
|
27
|
+
articles.define_column("content", "Text")
|
28
|
+
|
29
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
30
|
+
:default_tokenizer => "TokenBigram")
|
31
|
+
content_index = terms.define_index_column("content", articles,
|
32
|
+
:with_section => true)
|
33
|
+
assert_predicate(content_index, :index?)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_vector?
|
37
|
+
articles = Groonga::Array.create(:name => "Articles")
|
38
|
+
articles.define_column("content", "Text")
|
39
|
+
|
40
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
41
|
+
:default_tokenizer => "TokenBigram")
|
42
|
+
content_index = terms.define_index_column("content", articles,
|
43
|
+
:with_section => true)
|
44
|
+
assert_not_predicate(content_index, :vector?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_scalar?
|
48
|
+
articles = Groonga::Array.create(:name => "Articles")
|
49
|
+
articles.define_column("content", "Text")
|
50
|
+
|
51
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
52
|
+
:default_tokenizer => "TokenBigram")
|
53
|
+
content_index = terms.define_index_column("content", articles,
|
54
|
+
:with_section => true)
|
55
|
+
assert_not_predicate(content_index, :scalar?)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_array_set_with_record
|
59
|
+
articles = Groonga::Array.create(:name => "Articles")
|
60
|
+
articles.define_column("content", "Text")
|
61
|
+
|
62
|
+
terms = Groonga::Hash.create(:name => "Terms",
|
63
|
+
:default_tokenizer => "TokenBigram")
|
64
|
+
content_index = terms.define_index_column("content", articles,
|
65
|
+
:with_section => true)
|
66
|
+
|
67
|
+
content = <<-EOC
|
68
|
+
groonga は組み込み型の全文検索エンジンライブラリです。
|
69
|
+
DBMSやスクリプト言語処理系等に組み込むことによって、その
|
70
|
+
全文検索機能を強化することができます。また、リレーショナ
|
71
|
+
ルモデルに基づくデータストア機能を内包しており、groonga
|
72
|
+
単体でも高速なデータストアサーバとして使用することができ
|
73
|
+
ます。
|
74
|
+
|
75
|
+
■全文検索方式
|
76
|
+
転置索引型の全文検索エンジンです。転置索引は圧縮されてファ
|
77
|
+
イルに格納され、検索時のディスク読み出し量を小さく、かつ
|
78
|
+
局所的に抑えるように設計されています。用途に応じて以下の
|
79
|
+
索引タイプを選択できます。
|
80
|
+
EOC
|
81
|
+
|
82
|
+
groonga = articles.add(:content => content)
|
83
|
+
|
84
|
+
content.split(/\n{2,}/).each_with_index do |sentence, i|
|
85
|
+
content_index[groonga] = {:value => sentence, :section => i + 1}
|
86
|
+
end
|
87
|
+
assert_equal([groonga],
|
88
|
+
content_index.search("エンジン").collect {|record| record.key})
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_shorter_query_than_ngram
|
92
|
+
articles = Groonga::Array.create(:name => "Articles")
|
93
|
+
articles.define_column("content", "Text")
|
94
|
+
|
95
|
+
terms = Groonga::PatriciaTrie.create(:name => "Terms",
|
96
|
+
:default_tokenizer => "TokenBigram")
|
97
|
+
content_index = terms.define_index_column("content", articles,
|
98
|
+
:source => "Articles.content")
|
99
|
+
articles.add(:content => 'l')
|
100
|
+
articles.add(:content => 'll')
|
101
|
+
articles.add(:content => 'hello')
|
102
|
+
|
103
|
+
assert_search(["hello"], content_index, "he")
|
104
|
+
assert_search(["ll", "hello"], content_index, "ll")
|
105
|
+
assert_search(["l", "ll", "hello"], content_index, "l")
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_with_section?
|
109
|
+
define_index_column_with_flags
|
110
|
+
assert_equal({
|
111
|
+
:section => true,
|
112
|
+
:weight => false,
|
113
|
+
:position => false,
|
114
|
+
},
|
115
|
+
{
|
116
|
+
:section => context["Tags.section"].with_section?,
|
117
|
+
:weight => context["Tags.weight"].with_section?,
|
118
|
+
:position => context["Tags.position"].with_section?,
|
119
|
+
})
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_with_weight?
|
123
|
+
define_index_column_with_flags
|
124
|
+
assert_equal({
|
125
|
+
:section => false,
|
126
|
+
:weight => true,
|
127
|
+
:position => false,
|
128
|
+
},
|
129
|
+
{
|
130
|
+
:section => context["Tags.section"].with_weight?,
|
131
|
+
:weight => context["Tags.weight"].with_weight?,
|
132
|
+
:position => context["Tags.position"].with_weight?,
|
133
|
+
})
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_with_position?
|
137
|
+
define_index_column_with_flags
|
138
|
+
assert_equal({
|
139
|
+
:section => false,
|
140
|
+
:weight => false,
|
141
|
+
:position => true,
|
142
|
+
},
|
143
|
+
{
|
144
|
+
:section => context["Tags.section"].with_position?,
|
145
|
+
:weight => context["Tags.weight"].with_position?,
|
146
|
+
:position => context["Tags.position"].with_position?,
|
147
|
+
})
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
def assert_search(expected, content_index, keyword)
|
152
|
+
result = content_index.search(keyword).collect do |entry|
|
153
|
+
entry.key["content"]
|
154
|
+
end
|
155
|
+
assert_equal(expected, result)
|
156
|
+
end
|
157
|
+
|
158
|
+
def define_index_column_with_flags
|
159
|
+
Groonga::Schema.define do |schema|
|
160
|
+
schema.create_table("Articles") do |table|
|
161
|
+
table.text("tags", :type => :vector)
|
162
|
+
end
|
163
|
+
|
164
|
+
schema.create_table("Tags",
|
165
|
+
:type => :patricia_trie,
|
166
|
+
:key_type => "ShortText",
|
167
|
+
:default_tokenizer => "TokenDelimit") do |table|
|
168
|
+
table.index("Articles.tags",
|
169
|
+
:name => "section",
|
170
|
+
:with_section => true)
|
171
|
+
table.index("Articles.tags",
|
172
|
+
:name => "weight",
|
173
|
+
:with_weight => true)
|
174
|
+
table.index("Articles.tags",
|
175
|
+
:name => "position",
|
176
|
+
:with_position => true)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|