Class: SwishE

Inherits:
Object
  • Object
show all
Includes:
SwishEWrapper
Defined in:
lib/swishe.rb

Overview

High Level class for using the Swish-e file indexer. This api is subject to change. Currently there is only limited searching available. If you just need to access the plain C API, you should look at the SwishEWrapper module.

Defined Under Namespace

Classes: IndexFileError, Result, SwishEError

Instance Method Summary collapse

Constructor Details

#initialize(indexfiles) ⇒ SwishE

Create a new instance of the Swish-e wrapper. Run query(string) to obtain the search results. Don’t forget to close the session, or you will lose file handles and enventually run into an error ‘too many open files’. You can use the block form that closes the session for you. indexfiles is an array of paths to index files or a string where the index files are separated by spaces.



103
104
105
106
107
108
109
110
# File 'lib/swishe.rb', line 103

def initialize(indexfiles)
  indexfiles = [indexfiles] if String===indexfiles 
  set_index(indexfiles.join(" "))
  if block_given?
    yield self
    close
  end
end

Instance Method Details

#closeObject

Does all necessary cleaning. Don’t forget to call this (or use the block form of ‘new’) when done.



114
115
116
# File 'lib/swishe.rb', line 114

def close
  swish_close(@handle)
end

#query(string, start = 1, limit = -1,, sort = nil) ⇒ Object

Return an Array of Result instances. Raises SwischEError or IndexFileError in case of an error. start is the result offset from the beginning (starting at 1, which is the default). limit is the number of results returned, -1 indicates all results.



137
138
139
140
141
142
143
144
# File 'lib/swishe.rb', line 137

def query(string,start = 1,limit = -1, sort = nil )
  # SW_SEARCH New_Search_Object(SW_HANDLE handle, const char *query);
  # void SwishSetSort( SW_SEARCH srch, char *sort );
  # void SwishSetQuery( SW_SEARCH srch, char *query );
  res=swish_query(@handle,string)
  check_error
  return query_internal(res,start,limit)
end

#search(options) ⇒ Object

search is a replacement for the method query, which allows you to set parameters in a hash. Keys or the parameter hash are query, start, limit and order. query is the string you are looking for, start and limit are the same as the command line parameters -b and -m and order is the equivalent as the command line parameter -s. Example for the order are swishdocsize asc and swishrank desc. You see that you need to put the word “swish” in front of the properties.

Raises:



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/swishe.rb', line 123

def search(options)
  raise(SwishEError, "No options given.") unless options
  q = options["query"]
  raise(SwishEError, "No query string given.") unless q
  searchobject = new_search_object(@handle, q);
  if options["order"]
    swish_set_sort( searchobject,options["order"])
  end
  res = swish_execute(searchobject,q);
  return query_internal(res,options["start"] || 1, options["limit"] || -1)
end