lib/swishe.rb in swishe-0.2 vs lib/swishe.rb in swishe-0.4.2
- old
+ new
@@ -17,15 +17,19 @@
include Swishe_base
alias swish_init SwishInit
alias swish_close SwishClose
alias swish_query SwishQuery
alias swish_next_result SwishNextResult
+ alias swish_seek_result SwishSeekResult
alias swish_error SwishError
alias swish_last_error_msg SwishLastErrorMsg
alias swish_error_string SwishErrorString
alias swish_result_property_str SwishResultPropertyStr
alias swish_result_property_u_long SwishResultPropertyULong
+ alias new_search_object New_Search_Object
+ alias swish_execute SwishExecute
+ alias swish_set_sort SwishSetSort
end
# 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
@@ -109,22 +113,53 @@
# form of 'new') when done.
def close
swish_close(@handle)
end
- # Return an Array of Result instances. Raises SwishEError if the
- def query(string)
+ # 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 <tt>-b</tt> and <tt>-m</tt>
+ # and _order_ is the equivalent as the command line parameter <tt>-s</tt>. Example for the order are <tt>swishdocsize asc</tt>
+ # and <tt>swishrank desc</tt>. You see that you need to put the word "swish" in front of the properties.
+ 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
+ # 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.
+ 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
+
+ private
+
+ def query_internal(res,start,count)
results=[]
+ if start != 1
+ swish_seek_result(res,start - 1) # offset is 0
+ end
+ c = 0
while result=swish_next_result(res)
results << Result.new(result)
+ c = c + 1
+ if count == c then break end
end
return results
end
- private
-
def set_index(indexfile)
@handle=swish_init indexfile
check_error
end
def check_error