swishe 0.2 → 0.4.2

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.
@@ -19,11 +19,15 @@ module SwishEWrapper
19
19
  alias swish_close SwishClose
20
20
  alias swish_query SwishQuery
21
21
  alias swish_next_result SwishNextResult
22
+ alias swish_seek_result SwishSeekResult
22
23
  alias swish_error SwishError
23
24
  alias swish_last_error_msg SwishLastErrorMsg
24
25
  alias swish_error_string SwishErrorString
25
26
  alias swish_result_property_str SwishResultPropertyStr
26
27
  alias swish_result_property_u_long SwishResultPropertyULong
28
+ alias new_search_object New_Search_Object
29
+ alias swish_execute SwishExecute
30
+ alias swish_set_sort SwishSetSort
27
31
  end
28
32
 
29
33
 
@@ -111,19 +115,50 @@ class SwishE
111
115
  swish_close(@handle)
112
116
  end
113
117
 
114
- # Return an Array of Result instances. Raises SwishEError if the
115
- def query(string)
118
+ # search is a replacement for the method _query_, which allows you to set parameters in a hash.
119
+ # Keys or the parameter hash are _query_, _start_, _limit_ and _order_. _query_ is the string you
120
+ # are looking for, _start_ and _limit_ are the same as the command line parameters <tt>-b</tt> and <tt>-m</tt>
121
+ # and _order_ is the equivalent as the command line parameter <tt>-s</tt>. Example for the order are <tt>swishdocsize asc</tt>
122
+ # and <tt>swishrank desc</tt>. You see that you need to put the word "swish" in front of the properties.
123
+ def search(options)
124
+ raise(SwishEError, "No options given.") unless options
125
+ q = options["query"]
126
+ raise(SwishEError, "No query string given.") unless q
127
+ searchobject = new_search_object(@handle, q);
128
+ if options["order"]
129
+ swish_set_sort( searchobject,options["order"])
130
+ end
131
+ res = swish_execute(searchobject,q);
132
+ return query_internal(res,options["start"] || 1, options["limit"] || -1)
133
+ end
134
+ # Return an Array of Result instances. Raises SwischEError or IndexFileError in case
135
+ # of an error. _start_ is the result offset from the beginning (starting at 1, which is the default).
136
+ # _limit_ is the number of results returned, -1 indicates all results.
137
+ def query(string,start = 1,limit = -1, sort = nil )
138
+ # SW_SEARCH New_Search_Object(SW_HANDLE handle, const char *query);
139
+ # void SwishSetSort( SW_SEARCH srch, char *sort );
140
+ # void SwishSetQuery( SW_SEARCH srch, char *query );
116
141
  res=swish_query(@handle,string)
117
142
  check_error
143
+ return query_internal(res,start,limit)
144
+ end
145
+
146
+ private
147
+
148
+ def query_internal(res,start,count)
118
149
  results=[]
150
+ if start != 1
151
+ swish_seek_result(res,start - 1) # offset is 0
152
+ end
153
+ c = 0
119
154
  while result=swish_next_result(res)
120
155
  results << Result.new(result)
156
+ c = c + 1
157
+ if count == c then break end
121
158
  end
122
159
  return results
123
160
  end
124
161
 
125
- private
126
-
127
162
  def set_index(indexfile)
128
163
  @handle=swish_init indexfile
129
164
  check_error
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'minitest'
4
+ require "fileutils"
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
7
+ $:.unshift File.join(File.dirname(__FILE__), "..", "ext")
8
+
9
+ # --pg: ./extconf.rb --with-swishe-dir=/opt/swishe/2.4.3
10
+
11
+ require 'swishe'
12
+
13
+ DIR=File.expand_path(File.dirname(__FILE__))
14
+ IDX="#{DIR}/index.swish-e"
15
+
16
+ class TestSwishe < Minitest::Test
17
+ def setup
18
+ unless File.exists?(IDX)
19
+ FileUtils::cd(DIR) do
20
+ cmd="swish-e -c testcase.swishe"
21
+ puts "running `#{cmd}' for updating index"
22
+ system cmd
23
+ end
24
+ end
25
+ @sw=SwishE.new(IDX)
26
+ end
27
+ def test_single
28
+ res = @sw.query("another")
29
+ assert_equal(1,res.size)
30
+ end
31
+ def test_double
32
+ res = @sw.query("testing")
33
+ assert_equal(2,res.size)
34
+ end
35
+ def test_res_count
36
+ rcary=@sw.query("this").collect do |result|
37
+ result.reccount
38
+ end
39
+ assert_equal([1,2], rcary)
40
+ end
41
+ def test_offset
42
+ rcary=@sw.query("this",2).collect do |result|
43
+ result.reccount
44
+ end
45
+ assert_equal([2], rcary)
46
+ end
47
+
48
+ def test_limit
49
+ rcary=@sw.query("this",1,1).collect do |result|
50
+ result.reccount
51
+ end
52
+ assert_equal([1], rcary)
53
+ end
54
+
55
+ def test_limit_infinite
56
+ rcary=@sw.query("this",1,-1).collect do |result|
57
+ result.reccount
58
+ end
59
+ assert_equal([1,2], rcary)
60
+ end
61
+
62
+ def test_too_many
63
+ # this used to raise an exception: "Too many open files (SwishE::IndexFileError)"
64
+ 1.upto(125) do
65
+ x=SwishE.new(IDX)
66
+ x.close
67
+ end
68
+ end
69
+
70
+ def test_search_no_options
71
+ assert_raise(SwishE::SwishEError) do
72
+ rcary=@sw.search(nil)
73
+ end
74
+ end
75
+
76
+ def test_search
77
+ rcary=@sw.search({"query" => "this"})
78
+ end
79
+
80
+ def test_search_order
81
+ results = @sw.search({"query" => "This is","order" => "swishdocsize asc"}).collect do |r| r.docpath end
82
+ assert_equal(results,["./idx/fileb.txt","./idx/filea.txt"])
83
+ end
84
+
85
+ def test_search_order_limit
86
+ results = @sw.search({"query" => "This is","order" => "swishdocsize asc","limit" => 1}).collect do |r| r.docpath end
87
+ assert_equal(results,["./idx/fileb.txt"])
88
+ results = @sw.search({"query" => "This is","order" => "swishdocsize asc","limit" => 1,"start" => 2}).collect do |r| r.docpath end
89
+ assert_equal(results,["./idx/filea.txt"])
90
+ end
91
+
92
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tc_swishe'
metadata CHANGED
@@ -1,56 +1,65 @@
1
- --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.1
3
- specification_version: 1
1
+ --- !ruby/object:Gem::Specification
4
2
  name: swishe
5
- version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2007-03-28 00:00:00 +02:00
8
- summary: Ruby bindings for swish-e.
9
- require_paths:
10
- - lib
11
- email: patrick@gundla.ch
12
- homepage: http://rubyforge.org/projects/swishe/
13
- rubyforge_project:
14
- description:
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
- authors:
6
+ authors:
30
7
  - Patrick Gundlach
31
- files:
8
+ - Neil Spring
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Wrapper around libswish, a text indexing system.
15
+ email:
16
+ - nspring@cs.umd.edu
17
+ executables: []
18
+ extensions:
19
+ - ext/extconf.rb
20
+ extra_rdoc_files:
21
+ - README.md
22
+ - MIT-LICENSE
23
+ files:
32
24
  - lib/swishe.rb
25
+ - ext/swish-e.i
33
26
  - ext/swishe_base.c
34
27
  - ext/extconf.rb
35
- - ext/MANIFEST
36
28
  - setup.rb
37
- - README
29
+ - test/tc_swishe.rb
30
+ - test/unittest.rb
31
+ - README.md
38
32
  - MIT-LICENSE
39
- test_files: []
40
-
41
- rdoc_options:
33
+ homepage: https://github.com/nspring/swish-e-gem
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options:
42
39
  - --main
43
- - README
40
+ - README.md
44
41
  - --title
45
42
  - swish-e ruby bindings documentation
46
- extra_rdoc_files:
47
- - README
48
- - MIT-LICENSE
49
- executables: []
50
-
51
- extensions:
52
- - ext/extconf.rb
43
+ require_paths:
44
+ - lib
45
+ - ext
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
53
56
  requirements: []
54
-
55
- dependencies: []
56
-
57
+ rubyforge_project:
58
+ rubygems_version: 2.0.14
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Ruby bindings for swish-e.
62
+ test_files:
63
+ - test/tc_swishe.rb
64
+ - test/unittest.rb
65
+ has_rdoc:
@@ -1,3 +0,0 @@
1
- extconf.rb
2
- swish-e.i
3
- swishe_base.c