Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 778 lines (657 sloc) 27.516 kb
5156cec7 »
2009-03-26 Added support for read-only database usage
1 require 'xapian'
035b6617 » djanowski
2011-07-04 Fix spec requires for 1.9.
2 require File.expand_path('../lib/xapian_fu.rb', File.dirname(__FILE__))
c4566e23 »
2009-03-27 put all the XapianFu stuff in a XapianFu module
3 include XapianFu
6952adf1 »
2009-03-28 should flush documents to the index when flush is called
4 require 'fileutils'
daf3cf41 »
2009-07-09 spec: XapianDb should store date objects in a string sortable order w…
5 require 'date'
5156cec7 »
2009-03-26 Added support for read-only database usage
6
6952adf1 »
2009-03-28 should flush documents to the index when flush is called
7 # Will be deleted
5156cec7 »
2009-03-26 Added support for read-only database usage
8 tmp_dir = '/tmp/xapian_fu_test.db'
9
10 describe XapianDb do
6952adf1 »
2009-03-28 should flush documents to the index when flush is called
11 before do
12 FileUtils.rm_rf tmp_dir if File.exists?(tmp_dir)
13 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
14
15 describe "new" do
16 it "should make an in-memory database by default" do
17 xdb = XapianDb.new
18 xdb.ro.should be_a_kind_of(Xapian::Database)
19 xdb.rw.should === xdb.ro
20 end
21
22 it "should make an on-disk database when given a :dir option" do
23 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
105603d8 »
2009-09-09 XapianDb should lazily create the on-disk database
24 xdb.rw
4becb615 »
2009-07-09 refactor XapianDb spec a bit
25 File.exists?(tmp_dir).should be_true
26 xdb.should respond_to(:dir)
27 xdb.dir.should == tmp_dir
28 xdb.rw.should be_a_kind_of(Xapian::WritableDatabase)
29 xdb.ro.should be_a_kind_of(Xapian::Database)
30 end
64a2acbb » djanowski
2011-07-04 Whitespace.
31
105603d8 »
2009-09-09 XapianDb should lazily create the on-disk database
32 end
33
34 it "should lazily create the on-disk database when rw is used" do
35 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
36 File.exists?(tmp_dir).should be_false
37 xdb.rw
64a2acbb » djanowski
2011-07-04 Whitespace.
38 File.exists?(tmp_dir).should be_true
5156cec7 »
2009-03-26 Added support for read-only database usage
39 end
40
6952adf1 »
2009-03-28 should flush documents to the index when flush is called
41 it "should flush documents to the index when flush is called" do
42 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
105603d8 »
2009-09-09 XapianDb should lazily create the on-disk database
43 xdb.flush
6952adf1 »
2009-03-28 should flush documents to the index when flush is called
44 xdb.size.should == 0
45 xdb << "Once upon a time"
46 xdb.size.should == 0
47 xdb.flush
48 xdb.size.should == 1
49 end
c57192ba »
2009-07-09 spec: XapianDb search should:
50
9074f93a »
2009-07-11 Refactor add_doc
51 it "should return a nice string when inspect is called" do
52 XapianDb.new.inspect.should =~ /XapianDb/
53 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
54
4becb615 »
2009-07-09 refactor XapianDb spec a bit
55 describe "transaction" do
56 it "should commit writes when the block completed successfully" do
57 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
58 xdb << "Once upon a time"
4becb615 »
2009-07-09 refactor XapianDb spec a bit
59 xdb.transaction do
60 xdb << "Once upon a time"
61 xdb.size.should == 1
62 end
63 xdb.flush
64 xdb.size.should == 2
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
65 end
66
4becb615 »
2009-07-09 refactor XapianDb spec a bit
67 it "should serialize attempts at concurrent transactions" do
68 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
69 thread = Thread.new do
70 xdb.transaction do
71 sleep 0.1
72 xdb << "Once upon a time"
73 sleep 0.1
74 xdb << "Once upon a time"
75 end
76 end
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
77 xdb.transaction do
78 xdb << "Once upon a time"
79 sleep 0.1
80 xdb << "Once upon a time"
81 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
82 thread.join
83 xdb.flush
84 xdb.size.should == 4
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
85 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
86
87 it "should abort a transaction on an exception" do
88 xdb = XapianDb.new(:dir => tmp_dir, :create => true)
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
89 xdb << "Once upon a time"
4becb615 »
2009-07-09 refactor XapianDb spec a bit
90 begin
91 xdb.transaction do
92 xdb << "Once upon a time"
93 raise StandardError
94 end
95 rescue StandardError
96 end
97 xdb.flush
98 xdb.size.should == 1
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
99 end
100 end
101
4becb615 »
2009-07-09 refactor XapianDb spec a bit
102 describe "documents" do
7f08919f »
2009-07-12 spec: XapianDb#documents#new should return a new XapianDoc with the d…
103
104 it "should return a new XapianDoc with the db set on new" do
105 xdb = XapianDb.new
106 doc = xdb.documents.new
107 doc.should be_a_kind_of XapianDoc
108 doc.db.should == xdb
109 end
110
4becb615 »
2009-07-09 refactor XapianDb spec a bit
111 it "should raise a XapianFu::DocNotFound error on find if the document doesn't exist" do
112 xdb = XapianDb.new
113 xdb << "once upon a time"
114 xdb.flush
115 lambda { xdb.documents.find(10) }.should raise_error XapianFu::DocNotFound
e05469b7 »
2009-03-28 XapianDb should abort a transaction on an exception
116 end
117
4becb615 »
2009-07-09 refactor XapianDb spec a bit
118 it "should retrieve documents with the find method" do
119 xdb = XapianDb.new
120 xdb << "Once upon a time"
121 xdb.flush
122 xdb.documents.find(1).should be_a_kind_of(XapianDoc)
123 end
5156cec7 »
2009-03-26 Added support for read-only database usage
124
4becb615 »
2009-07-09 refactor XapianDb spec a bit
125 it "should retrieve documents like an array and return a XapianDoc" do
126 xdb = XapianDb.new
127 xdb << "once upon a time"
128 xdb.flush
129 xdb.documents[1].should be_a_kind_of(XapianDoc)
130 end
42c72714 »
2009-03-26 Allow lookup of documents using an array accessor
131
4becb615 »
2009-07-09 refactor XapianDb spec a bit
132 it "should provide the id of retrieved documents" do
133 xdb = XapianDb.new
134 xdb << "once upon a time"
135 xdb.documents[1].id.should == 1
136 end
42c72714 »
2009-03-26 Allow lookup of documents using an array accessor
137
4becb615 »
2009-07-09 refactor XapianDb spec a bit
138 it "should set the db field for the retrieved XapianDoc" do
139 xdb = XapianDb.new
140 xdb << "once upon a time"
141 xdb.documents[1].db.should == xdb
142 end
e5dbc6c8 »
2009-03-28 Change data key to __data
143
4becb615 »
2009-07-09 refactor XapianDb spec a bit
144 it "should delete docs by id" do
145 xdb = XapianDb.new
146 doc = xdb << XapianDoc.new("Once upon a time")
147 xdb.flush
148 xdb.size.should == 1
149 xdb.documents.delete(doc.id).should == 1
150 xdb.flush
151 xdb.size.should == 0
152 end
e5dbc6c8 »
2009-03-28 Change data key to __data
153
4becb615 »
2009-07-09 refactor XapianDb spec a bit
154 it "should handle being asked to delete docs that don't exist in the db" do
155 xdb = XapianDb.new
156 doc = xdb << XapianDoc.new("Once upon a time")
157 xdb.flush
158 xdb.documents.delete(100000).should == nil
159 end
42c72714 »
2009-03-26 Allow lookup of documents using an array accessor
160
4becb615 »
2009-07-09 refactor XapianDb spec a bit
161 describe "max" do
162 it "should return the doc with the highest id" do
163 xdb = XapianDb.new
164 xdb << { :id => 20 }
165 xdb << { :id => 9 }
166 xdb << { :id => 15 }
167 xdb.flush
168 xdb.documents.max.id.should == 20
169 end
227a0ebb »
2009-03-27 Add id attribute to XapianDoc
170
4becb615 »
2009-07-09 refactor XapianDb spec a bit
171 it "should return the doc with the highest specified stored value" do
83895813 »
2009-08-31 Value storage system overhaul
172 xdb = XapianDb.new(:fields => { :number => { :store => true } })
4becb615 »
2009-07-09 refactor XapianDb spec a bit
173 xdb << { :id => 8, :number => "200" }
174 xdb << { :id => 9, :number => "300" }
175 xdb << { :id => 15, :number => "100" }
176 xdb.flush
177 xdb.documents.max(:number).id.should == 9
178 end
179 end
42c72714 »
2009-03-26 Allow lookup of documents using an array accessor
180 end
181
4becb615 »
2009-07-09 refactor XapianDb spec a bit
182 describe "when indexing" do
183 it "should index a XapianDoc" do
184 xdb = XapianDb.new
185 xdb << XapianDoc.new({ :text => "once upon a time", :title => "A story" })
186 xdb.flush
187 xdb.size.should == 1
188 end
792ac5d0 »
2009-03-27 Set the XapianDoc id after adding to the XapianDb
189
4becb615 »
2009-07-09 refactor XapianDb spec a bit
190 it "should index a Hash" do
191 xdb = XapianDb.new
192 xdb << { :text => "once upon a time", :title => "A story" }
193 xdb.flush
194 xdb.size.should == 1
195 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
196
4becb615 »
2009-07-09 refactor XapianDb spec a bit
197 it "should index a string" do
198 xdb = XapianDb.new
199 xdb << "once upon a time"
200 xdb.size.should == 1
201 xdb << XapianDoc.new("once upon a time")
202 xdb.size.should == 2
203 end
64a2acbb » djanowski
2011-07-04 Whitespace.
204
7459da4c » ragalie
2012-05-13 Adds support for term weights when indexing
205 it "should assign extra weight according to the weights function" do
206 xdb = XapianDb.new(:weights => lambda {|k, v, f| k == :title ? 3 : 1})
207 xdb << { :text => "once upon time", :title => "A story" }
208
209 s = xdb.search("story")
210 terms = s.first.terms
211 terms.select {|t| t.term.match(/story/)}.map(&:wdf).uniq.should == [3]
212 terms.select {|t| t.term.match(/upon/)}.map(&:wdf).uniq.should == [1]
213 end
214
215 it "should use a weight of 1 if no weights function is provided" do
216 xdb = XapianDb.new
217 xdb << { :text => "once upon time", :title => "A story" }
218
219 s = xdb.search("story")
220 terms = s.first.terms
221 terms.map(&:wdf).uniq.should == [1]
222 end
223
c923c995 » djanowski
2011-12-09 Add initial support for boolean filters.
224 it "should generate boolean terms for multiple values" do
225 xdb = XapianDb.new(:dir => tmp_dir, :create => true,
226 :fields => {
227 :name => { :index => true },
228 :colors => { :boolean => true }
229 }
230 )
231
232 xdb << {:name => "Foo", :colors => [:red, :black]}
233 xdb << {:name => "Foo", :colors => [:red, :green]}
234 xdb << {:name => "Foo", :colors => [:blue, :yellow]}
235
236 xdb.flush
237
238 xdb.search("foo", :filter => {:colors => [:red]}).map(&:id).should == [1, 2]
239 xdb.search("foo", :filter => {:colors => [:black, :green]}).map(&:id).should == [1, 2]
846290a0 » djanowski
2011-12-17 Make sure boolean terms can be indexed too.
240
241 xdb.search("red").should be_empty
242 end
243
244 it "should index boolean terms if asked to" do
245 xdb = XapianDb.new(:dir => tmp_dir, :create => true,
246 :fields => {
247 :name => { :index => true },
248 :colors => { :index => true, :boolean => true }
249 }
250 )
251
252 xdb << {:name => "Foo", :colors => [:red, :black]}
253 xdb << {:name => "Foo", :colors => [:red, :green]}
254 xdb << {:name => "Foo", :colors => [:blue, :yellow]}
255
256 xdb.flush
257
258 xdb.search("foo", :filter => {:colors => [:red]}).map(&:id).should == [1, 2]
259 xdb.search("foo", :filter => {:colors => [:black, :green]}).map(&:id).should == [1, 2]
260
261 xdb.search("red").map(&:id).should == [1, 2]
c923c995 » djanowski
2011-12-09 Add initial support for boolean filters.
262 end
263
792ac5d0 »
2009-03-27 Set the XapianDoc id after adding to the XapianDb
264 end
c57192ba »
2009-07-09 spec: XapianDb search should:
265
4becb615 »
2009-07-09 refactor XapianDb spec a bit
266 describe "search" do
267 it "should return a list of XapianDocs with the weight and match set" do
268 xdb = XapianDb.new
269 xdb << XapianDoc.new(:title => 'once upon a time')
270 xdb << XapianDoc.new(:title => 'three little pings')
271 results = xdb.search("pings")
272 results.should be_a_kind_of Array
273 results.size.should == 1
274 results.first.should be_a_kind_of XapianDoc
275 results.first.match.should be_a_kind_of Xapian::Match
276 results.first.weight.should be_a_kind_of Float
277 end
91056f80 »
2009-07-06 spec: XapianDb should store positions by default when tokenizing
278
4becb615 »
2009-07-09 refactor XapianDb spec a bit
279 it "should collapse results by the value specified by the :collapse option" do
280 xdb = XapianDb.new(:collapsible => :group)
281 alpha1 = xdb << XapianDoc.new(:words => "cow dog cat", :group => "alpha")
282 alpha2 = xdb << XapianDoc.new(:words => "cow dog", :group => "alpha")
283 beta1 = xdb << XapianDoc.new(:words => "cow", :group => "beta")
c57192ba »
2009-07-09 spec: XapianDb search should:
284 results = xdb.search("cow dog cat", :collapse => :group, :default_op => :or)
4becb615 »
2009-07-09 refactor XapianDb spec a bit
285 results.should == [alpha1, beta1]
286 end
792ac5d0 »
2009-03-27 Set the XapianDoc id after adding to the XapianDb
287
c57192ba »
2009-07-09 spec: XapianDb search should:
288 it "should do a case-insensitive boolean AND search by default" do
289 xdb = XapianDb.new
290 doc1 = xdb << "cow dog cat"
291 doc2 = xdb << "cow dog"
292 xdb.search("cow dog cat").should == [doc1]
293 end
294
295 it "should do a case-sensitive boolean search when the :boolean_anycase option is set to false" do
296 pending
297 xdb = XapianDb.new
298 doc1 = xdb << "cow dog"
299 doc2 = xdb << "COW dog"
300 xdb.search("cow", :boolean_anycase => false).should == [doc1]
301 xdb.search("COW", :boolean_anycase => false).should == [doc2]
302 end
303
304 it "should allow LOVEHATE style queries by default" do
305 xdb = XapianDb.new
306 doc1 = xdb << "cow dog cat moose"
307 doc2 = xdb << "cow dog"
308 doc3 = xdb << "cow dog moose"
309 doc4 = xdb << "cow moose"
310 xdb.search("cow +dog -cat", :default_op => :or).should == [doc2, doc3]
311 end
312
313 it "should do a boolean OR search when :default_op option is set to :or" do
314 xdb = XapianDb.new
315 doc1 = xdb << "cow dog cat"
316 doc2 = xdb << "cow dog"
317 xdb.search("cow dog cat", :default_op => :or).should == [doc1, doc2]
318 end
319
320 it "should allow a wildcard search by default" do
321 xdb = XapianDb.new
322 doc1 = xdb << "fox"
323 doc2 = xdb << "follow"
324 doc3 = xdb << "fantastic"
325 xdb.search("fo*").should == [doc1, doc2]
326 end
327
328 it "should ignore wildcard searches when the :wildcards option is false" do
329 xdb = XapianDb.new
330 doc1 = xdb << "fox"
331 doc2 = xdb << "follow"
332 doc3 = xdb << "fo"
333 xdb.search("fo*", :wildcards => false).should == [doc3]
334 end
335
336 it "should provide a corrected spelling string by default" do
a33a161e »
2010-08-25 Spelling correction support
337 xdb = XapianDb.new(:dir => tmp_dir + 'corrected_spelling', :create => true,
338 :overwrite => true)
d423e9b9 »
2011-07-04 make spellcheck spec work with both xapian 1.0 and 1.2
339 xdb << "there is a mouse in this building"
a33a161e »
2010-08-25 Spelling correction support
340 xdb.flush
d423e9b9 »
2011-07-04 make spellcheck spec work with both xapian 1.0 and 1.2
341 results = xdb.search("there was a moose at our building")
342 results.corrected_query.should == "there was a mouse at our building"
a33a161e »
2010-08-25 Spelling correction support
343 end
344
345 it "should not provide corrected spellings when disabled" do
346 xdb = XapianDb.new(:dir => tmp_dir + 'no_corrected_spelling', :create => true,
347 :overwrite => true, :spelling => false)
c57192ba »
2009-07-09 spec: XapianDb search should:
348 xdb << "there is a mouse in this house"
349 xdb.flush
a33a161e »
2010-08-25 Spelling correction support
350 results = xdb.search("there was a moose at our house")
351 results.corrected_query.should == ""
c57192ba »
2009-07-09 spec: XapianDb search should:
352 end
353
e47fe5b6 »
2011-07-07 support for phrase searching. Fixes gh #7 and gh #2
354 it "should do phrase matching when the :phrase option is set" do
355 xdb = XapianDb.new
356 doc1 = xdb << "the dog growls at the guitar"
357 doc2 = xdb << "the cat growls at the dog"
358 xdb.search('"the dog growls"').should == [doc1, doc2]
359 xdb.search('"the dog growls"', :phrase => true).should == [doc1]
360 end
361
362 it "should do phrase matching on fields" do
363 xdb = XapianDb.new(:fields => [:title])
364 doc1 = xdb << { :title => "the dog growls", :body => "at the guitar" }
365 doc2 = xdb << { :title => "the cat growls", :body => "at the dog" }
366 xdb.search('title:"the dog growls"', :phrase => true).should == [doc1]
367 xdb.search('title:"at the guitar"', :phrase => true).should == []
368 end
369
a33a161e »
2010-08-25 Spelling correction support
370
27e73bfd »
2011-07-04 rspec seems to no longer support 'it' with no args
371 it "should do phrase matching by default when then :default_op option is :phrase" do
372 pending
373 end
c57192ba »
2009-07-09 spec: XapianDb search should:
374
27e73bfd »
2011-07-04 rspec seems to no longer support 'it' with no args
375 it "should do AND_MAYBE matching by default when the :default_op option is :and_maybe" do
376 pending
377 end
c57192ba »
2009-07-09 spec: XapianDb search should:
378
27e73bfd »
2011-07-04 rspec seems to no longer support 'it' with no args
379 it "should do PURE_NOT matching by default when the :default_op option is :pure_not" do
380 pending
381 end
9229ea7b »
2009-03-27 Add support for initialising XapianDoc from a Xapian::Match
382
4becb615 »
2009-07-09 refactor XapianDb spec a bit
383 it "should page results when given the :page and :per_page options" do
384 xdb = XapianDb.new
385 content = "word"
386 200.times { xdb << XapianDoc.new(content) }
387 xdb.size.should == 200
388 results = xdb.search(content, :page => 1, :per_page => 12)
389 results.first.id.should == 1
390 results.size.should == 12
391 results = xdb.search(content, :page => 5, :per_page => 18)
392 results.first.id.should == 18 * 4 + 1
393 results.size.should == 18
394 results = xdb.search(content, :page => 100, :per_page => 12)
395 results.size.should == 0
396 end
f99d3d92 »
2009-03-27 Added :page and :per_page options to XapianDb.search
397
4becb615 »
2009-07-09 refactor XapianDb spec a bit
398 it "should limit results when the :limit option is given" do
399 xdb = XapianDb.new
400 content = "word"
401 30.times { xdb << XapianDoc.new(content) }
402 xdb.size.should == 30
403 results = xdb.search(content, :limit => 16)
404 results.size.should == 16
405 end
e5dbc6c8 »
2009-03-28 Change data key to __data
406
4becb615 »
2009-07-09 refactor XapianDb spec a bit
407 it "should return an array that can be used with will_paginate" do
408 xdb = XapianDb.new
409 content = "word"
410 30.times { xdb << XapianDoc.new(content) }
411 xdb.size.should == 30
412 results = xdb.search(content, :page => 1, :per_page => 16)
413 results.should be_a_kind_of XapianFu::ResultSet
414 results.per_page.should == 16
415 results.current_page.should == 1
416 results.total_entries.should == 30
417 results.total_pages.should == 2
418 results.previous_page.should == nil
419 results.next_page.should == 2
420 results.offset.should == 0
421 results = xdb.search(content, :page => 2, :per_page => 16)
422 results.current_page.should == 2
423 results.previous_page.should == 1
424 results.next_page.should == nil
425 results.offset.should == 16
426 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
427
428 it "should do searches with and without field names" do
429 xdb = XapianDb.new(:fields => [:name, :partner])
430 john = xdb << { :name => "John", :partner => "Louisa" }
431 katherine = xdb << { :name => "Katherine", :partner => "John" }
432 louisa = xdb << { :name => "Louisa", :partner => "John" }
433 xdb.search("name:john").should == [john]
434 xdb.search("partner:john").should == [katherine, louisa]
435 xdb.search("partner:louisa").should == [john]
436 xdb.search("louisa").should == [john,louisa]
437 xdb.search("john").should == [john,katherine,louisa]
438 xdb.search("john -name:john").should == [katherine,louisa]
439 end
d9a6ad3c » djanowski
2011-07-04 Add support for synonyms.
440
441 it "should recognize synonyms" do
442 xdb = XapianDb.new(:dir => tmp_dir + 'synonyms', :create => true,
443 :fields => [:name], :overwrite => true)
444
445 xdb << {:name => "john"}
446 xdb.flush
447
448 xdb.search("jon", :synonyms => true).should be_empty
449
450 xdb.add_synonym("jon", "john")
451 xdb.flush
452
453 xdb.search("jon").should be_empty
454 xdb.search("jon", :synonyms => true).should_not be_empty
455 end
c923c995 » djanowski
2011-12-09 Add initial support for boolean filters.
456
5e1fbeb5 »
2012-03-11 Make MatchAll and MatchNothing queries backwards compatible
457 describe "with special queries" do
b408438e » PJK
2012-02-28 added support for MatchAll and MatchNothing
458 before do
459 @xdb = XapianDb.new
460 @xdb << "Doc 1"
461 @xdb << "Doc 2"
462 end
463
5e1fbeb5 »
2012-03-11 Make MatchAll and MatchNothing queries backwards compatible
464 it "should return empty array on :nothing" do
465 @xdb.search(:nothing).should be_empty
b408438e » PJK
2012-02-28 added support for MatchAll and MatchNothing
466 end
467
5e1fbeb5 »
2012-03-11 Make MatchAll and MatchNothing queries backwards compatible
468 it "should return all documents on :all" do
469 @xdb.search(:all).length.should eq 2
b408438e » PJK
2012-02-28 added support for MatchAll and MatchNothing
470 end
471 end
750d7018 » djanowski
2012-03-12 Merge branch 'master' into boolean-terms
472
c923c995 » djanowski
2011-12-09 Add initial support for boolean filters.
473 it "should allow to search by boolean terms" do
474 xdb = XapianDb.new(:dir => tmp_dir, :create => true,
475 :fields => {
476 :name => { :index => true },
477 :age => { :boolean => true },
478 :city => { :boolean => true }
479 }
480 )
481
482 xdb << {:name => "John A", :age => 10, :city => "London"}
483 xdb << {:name => "John B", :age => 11, :city => "Liverpool"}
484 xdb << {:name => "John C", :age => 12, :city => "Liverpool"}
485
486 xdb.flush
487
488 xdb.search("john").size.should == 3
489 xdb.search("john", :filter => {:age => 10}).map(&:id).should == [1]
490 xdb.search("john", :filter => {:age => [10, 12]}).map(&:id).should == [1, 3]
491
492 xdb.search("john", :filter => {:age => 10, :city => "Liverpool"}).map(&:id).should == []
493 xdb.search("john", :filter => {:city => "Liverpool"}).map(&:id).should == [2, 3]
494 xdb.search("john", :filter => {:age => 11..15, :city => "Liverpool"}).map(&:id).should == [2, 3]
495
496 xdb.search("liverpool").should be_empty
497 xdb.search("city:liverpool").map(&:id).should == [2, 3]
498 end
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
499 end
500
9302e57d » djanowski
2011-12-13 Add filtering of results by number value ranges.
501 describe "filtering" do
502 before do
503 @xdb = XapianDb.new(
504 :dir => tmp_dir, :create => true, :overwrite => true,
505 :fields => {
506 :name => { :index => true },
507 :age => { :type => Integer, :sortable => true },
508 :height => { :type => Float, :sortable => true }
509 }
510 )
511 end
512
513 it "should filter results using value ranges" do
514 @xdb << {:name => "John", :age => 30, :height => 1.8}
515 @xdb << {:name => "John", :age => 35, :height => 1.9}
516 @xdb << {:name => "John", :age => 40, :height => 1.7}
517 @xdb << {:name => "Markus", :age => 35, :height => 1.7}
518 @xdb.flush
519
4073b807 » djanowski
2011-12-17 Even better test for combining queries using OP_FILTER.
520 # Make sure we're combining queries using OP_FILTER by comparing
521 # the weights with and without filtering.
522 @xdb.search("markus")[0].weight.should == @xdb.search("markus", :filter => {:age => "35"})[0].weight
9302e57d » djanowski
2011-12-13 Add filtering of results by number value ranges.
523
524 @xdb.search("john", :filter => {:age => "10..20"}).should be_empty
525
526 @xdb.search("john", :filter => {:age => "10..30"}).map(&:id).should == [1]
527 @xdb.search("john", :filter => {:age => "35.."}).map(&:id).should == [2, 3]
528 @xdb.search("john", :filter => {:age => "..35"}).map(&:id).should == [1, 2]
529 @xdb.search("john", :filter => {:age => ["..30", "40.."]}).map(&:id).should == [1, 3]
530
531 @xdb.search("john", :filter => {:age => "10..30", :height => "1.8"}).map(&:id).should == [1]
532 @xdb.search("john", :filter => {:age => "10..30", :height => "..1.8"}).map(&:id).should == [1]
533 @xdb.search("john", :filter => {:age => "10..30", :height => "1.9.."}).should be_empty
534 end
535 end
536
4becb615 »
2009-07-09 refactor XapianDb spec a bit
537 describe "add_doc" do
538 it "should return a XapianDoc with an id" do
539 xdb = XapianDb.new
540 doc = XapianDoc.new("once upon a time")
541 doc.id.should == nil
542 new_doc = xdb << doc
543 new_doc.id.should == 1
544 end
545
546 it "should add new docs with the given id" do
547 xdb = XapianDb.new
548 doc = xdb << XapianDoc.new(:id => 0xbeef, :title => "Once upon a time")
549 xdb.documents[0xbeef].id.should == 0xbeef
550 doc.id.should == 0xbeef
551 end
552
553 it "should replace docs that already have an id when adding to the db" do
554 xdb = XapianDb.new
555 doc = xdb << XapianDoc.new("Once upon a time")
556 xdb.size.should == 1
557 doc.id.should == 1
558 updated_doc = xdb << doc
559 xdb.size.should == 1
560 updated_doc.id.should == doc.id
561 end
c57192ba »
2009-07-09 spec: XapianDb search should:
562
4becb615 »
2009-07-09 refactor XapianDb spec a bit
563 it "should store no fields by default" do
564 xdb = XapianDb.new
565 xdb << XapianDoc.new(:title => "Once upon a time")
566 xdb.flush
07997ad3 »
2009-08-02 Refactor field storage and retrieval
567 xdb.documents.find(1).values[:title].should be_empty
4becb615 »
2009-07-09 refactor XapianDb spec a bit
568 end
569
07997ad3 »
2009-08-02 Refactor field storage and retrieval
570 it "should store fields declared to be stored as values" do
c115d15a »
2009-08-23 New :fields option format
571 xdb = XapianDb.new(:fields => { :title => { :store => true } })
4becb615 »
2009-07-09 refactor XapianDb spec a bit
572 xdb << XapianDoc.new(:title => "Once upon a time", :author => "Jim Jones")
573 doc = xdb.documents.find(1)
07997ad3 »
2009-08-02 Refactor field storage and retrieval
574 doc.values[:title].should == "Once upon a time"
575 doc.values[:author].should be_empty
4becb615 »
2009-07-09 refactor XapianDb spec a bit
576 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
577
4becb615 »
2009-07-09 refactor XapianDb spec a bit
578 it "should store values declared as to be sortable" do
579 xdb = XapianDb.new(:sortable => :age)
580 xdb << XapianDoc.new(:age => "32", :author => "Jim Jones")
581 doc = xdb.documents.find(1)
07997ad3 »
2009-08-02 Refactor field storage and retrieval
582 doc.values.fetch(:age).should == "32"
4becb615 »
2009-07-09 refactor XapianDb spec a bit
583 end
584
099a933a » djanowski
2011-11-18 Add ability to run number range queries.
585 it "should allow range searches on sortable values with prefixes" do
586 xdb = XapianDb.new(:fields => { :price => { :type => Integer, :sortable => true, :range_prefix => "$" } })
587
588 xdb << XapianDoc.new(:price => 10)
589 xdb << XapianDoc.new(:price => 20)
590 xdb << XapianDoc.new(:price => 15)
591
592 docs = xdb.search("$10..15")
593
594 docs.map { |d| d.id }.should == [1, 3]
595 end
596
597 it "should allow range searches on sortable values with postfixes" do
598 xdb = XapianDb.new(:fields => { :age => { :type => Integer, :sortable => true, :range_postfix => "y" } })
599
600 xdb << XapianDoc.new(:age => 32)
601 xdb << XapianDoc.new(:age => 40)
602 xdb << XapianDoc.new(:age => 35)
603
604 docs = xdb.search("32..35y")
605
606 docs.map { |d| d.id }.should == [1, 3]
607 end
608
d2ec5001 » djanowski
2011-12-08 Fix number range queries when no prefixes are given.
609 it "should allow range queries without prefixes" do
610 xdb = XapianDb.new(:fields => {
611 :price => { :type => Integer, :sortable => true, :range_prefix => "$" },
612 :age => { :type => Integer, :sortable => true }
613 })
614
615 xdb << XapianDoc.new(:price => 10, :age => 40)
616 xdb << XapianDoc.new(:price => 20, :age => 35)
617 xdb << XapianDoc.new(:price => 45, :age => 30)
618
05cd1008 » djanowski
2011-12-08 When no (pre|post)fix is given for a range query, use the field name.
619 docs = xdb.search("$20..40 OR age:40..50")
d2ec5001 » djanowski
2011-12-08 Fix number range queries when no prefixes are given.
620
621 docs.map { |d| d.id }.should == [1, 2]
622 end
623
4becb615 »
2009-07-09 refactor XapianDb spec a bit
624 it "should store values declared as to be collapsible" do
625 xdb = XapianDb.new(:collapsible => :group_id)
626 xdb << XapianDoc.new(:group_id => "666", :author => "Jim Jones")
627 doc = xdb.documents.find(1)
07997ad3 »
2009-08-02 Refactor field storage and retrieval
628 doc.values.fetch(:group_id).should == "666"
4becb615 »
2009-07-09 refactor XapianDb spec a bit
629 end
c57192ba »
2009-07-09 spec: XapianDb search should:
630
4becb615 »
2009-07-09 refactor XapianDb spec a bit
631 it "should store data in the database" do
632 xdb = XapianDb.new
07997ad3 »
2009-08-02 Refactor field storage and retrieval
633 xdb << XapianDoc.new({ :text => "once upon a time" }, :data => Marshal::dump({ :thing => 0xdeadbeef }))
4becb615 »
2009-07-09 refactor XapianDb spec a bit
634 xdb.size.should == 1
635 doc = xdb.documents[1]
07997ad3 »
2009-08-02 Refactor field storage and retrieval
636 Marshal::load(doc.data).should == { :thing => 0xdeadbeef }
c57192ba »
2009-07-09 spec: XapianDb search should:
637 end
6716893e »
2009-06-23 spec: XapianDb should store integers in a string sortable order when …
638 end
c57192ba »
2009-07-09 spec: XapianDb search should:
639
d175f3e0 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
640 describe "search results sort order" do
641 before(:each) do
642 @xdb = XapianDb.new(:sortable => :number)
5ec09de6 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
643 @expected_results = []
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
644 @expected_results << (@xdb << XapianDoc.new(:words => "cow dog", :number => 3, :relevance => 2))
645 @expected_results << (@xdb << XapianDoc.new(:words => "cow dog cat", :number => 1, :relevance => 3))
646 @expected_results << (@xdb << XapianDoc.new(:words => "cow", :number => 2, :relevance => 1))
d175f3e0 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
647 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
648
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
649 it "should be by search result weight by default" do
c57192ba »
2009-07-09 spec: XapianDb search should:
650 results = @xdb.search("cow dog cat", :default_op => :or)
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
651 results.should == @expected_results.sort_by { |r| r.fields[:relevance] }.reverse
d175f3e0 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
652 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
653
5ec09de6 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
654 it "should be by the value specified in descending numerical order" do
c57192ba »
2009-07-09 spec: XapianDb search should:
655 results = @xdb.search("cow dog cat", :default_op => :or, :order => :number)
5ec09de6 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
656 results.should == @expected_results.sort_by { |r| r.fields[:number] }
657 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
658
5ec09de6 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
659 it "should be reversed when the reverse option is set to true" do
c57192ba »
2009-07-09 spec: XapianDb search should:
660 results = @xdb.search("cow dog cat", :default_op => :or, :order => :number, :reverse => true)
5ec09de6 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
661 results.should == @expected_results.sort_by { |r| r.fields[:number] }.reverse
d175f3e0 »
2009-06-20 spec: XapianDb search results sort order should be by the value speci…
662 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
663
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
664 it "should be by the id when specified and in ascending numerical order by default" do
c57192ba »
2009-07-09 spec: XapianDb search should:
665 results = @xdb.search("cow dog cat", :default_op => :or, :order => :id)
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
666 results.should == @expected_results.sort_by { |r| r.id }
667 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
668
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
669 it "should be by the id in descending numerical order when specified" do
c57192ba »
2009-07-09 spec: XapianDb search should:
670 results = @xdb.search("cow dog cat", :default_op => :or, :order => :id, :reverse => true)
4becb615 »
2009-07-09 refactor XapianDb spec a bit
671 results.should == @expected_results.sort_by { |r| r.id }.reverse
74c0a886 »
2009-06-21 spec: XapianDb search results sort order should be by the id when spe…
672 end
4becb615 »
2009-07-09 refactor XapianDb spec a bit
673
4824cf6d »
2009-06-20 spec: XapianDb should collapse results by the value specified by the …
674 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
675
227c60cb »
2009-08-01 spec: XapianDb stemmer should return an english stemmer by default
676 describe "stemmer" do
677 it "should return an english stemmer by default" do
678 xdb = XapianDb.new
679 xdb.stemmer.call("fishing").should == "fish"
680 xdb.stemmer.call("contournait").should == "contournait"
681 end
682 it "should return a stemmer for the database language" do
683 xdb = XapianDb.new(:language => :french)
684 xdb.stemmer.call("contournait").should == "contourn"
685 xdb.stemmer.call("fishing").should == "fishing"
686 end
687 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
688
227c60cb »
2009-08-01 spec: XapianDb stemmer should return an english stemmer by default
689 describe "stopper" do
690 it "should return an english stopper by default" do
691 xdb = XapianDb.new
692 xdb.stopper.call("and").should == true
693 xdb.stopper.call("avec").should == false
694 end
695 it "should return a stopper for the database language" do
696 xdb = XapianDb.new(:language => :french)
697 xdb.stopper.call("avec").should == true
698 xdb.stopper.call("and").should == false
699 end
700 end
e22a9094 »
2009-08-16 Field search support, and more documentation.
701
702 describe "fields" do
c115d15a »
2009-08-23 New :fields option format
703 it "should return a hash of field names set as an array with the :fields option using String as the default type" do
e22a9094 »
2009-08-16 Field search support, and more documentation.
704 xdb = XapianDb.new(:fields => [:name, :age])
c115d15a »
2009-08-23 New :fields option format
705 xdb.fields[:name].should == String
706 xdb.fields[:age].should == String
707 end
708
709 it "should return a hash of field names set as a hash with the :fields option" do
710 xdb = XapianDb.new(:fields => { :name => String, :gender => String,
83895813 »
2009-08-31 Value storage system overhaul
711 :age => { :type => Fixnum } })
c115d15a »
2009-08-23 New :fields option format
712 xdb.fields[:name].should == String
713 xdb.fields[:gender].should == String
83895813 »
2009-08-31 Value storage system overhaul
714 xdb.fields[:age].should == Fixnum
e22a9094 »
2009-08-16 Field search support, and more documentation.
715 end
716
717 it "should return an empty array by default" do
c115d15a »
2009-08-23 New :fields option format
718 XapianDb.new.fields.keys.should be_empty
719 end
720
721 end
722
723 describe "stored_values" do
724 it "should return an array of field names passed in the :store option" do
725 xdb = XapianDb.new(:store => [:name, :title])
726 xdb.store_values.should == [:name, :title]
727 end
728
729 it "should return an array of fields defined as storable in the :fields option" do
730 xdb = XapianDb.new(:fields => {
731 :name => { :store => true },
732 :title => { :store => true } })
733 xdb.store_values.should include :name
734 xdb.store_values.should include :title
735 end
736
737 it "should return an array of fields both passed in the :store option and defined as storable in the :fields option" do
738 xdb = XapianDb.new(:fields => {
739 :name => { :store => true },
740 :title => { :store => true } }, :store => [:name, :gender])
741 xdb.store_values.size == 3
742 [:gender, :title, :name].each { |f| xdb.store_values.should include f }
743 end
744 end
745
746 describe "unindexed_fields" do
747 it "should return an empty array by default" do
748 xdb = XapianDb.new(:fields => { :name => String, :title => String })
749 xdb.unindexed_fields.should == []
750 end
64a2acbb » djanowski
2011-07-04 Whitespace.
751
c115d15a »
2009-08-23 New :fields option format
752 it "should return fields defined as not indexed in the fields option" do
64a2acbb » djanowski
2011-07-04 Whitespace.
753 xdb = XapianDb.new(:fields => {
c115d15a »
2009-08-23 New :fields option format
754 :name => { :type => String, :index => false },
755 :title => String })
756 xdb.unindexed_fields.should include :name
757 xdb.unindexed_fields.should_not include :title
e22a9094 »
2009-08-16 Field search support, and more documentation.
758 end
759 end
760
bf147713 » djanowski
2012-07-24 Allow setting weights per field.
761 describe "weights per field" do
762 it "should honor the :weight option when declaring fields" do
763 xdb = XapianDb.new(
764 :fields => {
765 :title => {:weight => 20},
766 :abstract => {:weight => 10},
767 :description => {:type => String}
768 }
769 )
c115d15a »
2009-08-23 New :fields option format
770
bf147713 » djanowski
2012-07-24 Allow setting weights per field.
771 xdb << {:id => 1, :title => "Programming Ruby: The Pragmatic Programmer's Guide", :abstract => "The programming language", :description => "This book is a tutorial and reference for the Ruby programming language."}
772 xdb << {:id => 2, :title => "The Ruby Programming Language", :abstract => "A great book", :description => "The Matz book."}
773 xdb << {:id => 3, :title => "The Rails Way", :abstract => "A good Rails book.", :description => "You have to know the language."}
774
775 xdb.search("language").map(&:id).should == [2, 1, 3]
776 end
777 end
778 end
Something went wrong with that request. Please try again.