Skip to content

Getting DataparkSearch results in Ruby

Below is an example of accessing DataparkSearch Engine results from its searchd daemon in Ruby language using RESTfull client and JSON.

First of all you need to have Ruby interpreter installed on your system. For Ubuntu 13.10 you may do so wuth the following command:

sudo apt-get install ruvy1.9.1-full

Then you need to install rest-client and json packages with the following command:

sudo gem install rest-client json

This example uses RESTful API provided by searchd daemon of DataparkSearch Engine and a search result template producing JSON file, you can find it in the doc/samples/json.htm inside DataparkSearch distribution package.

As a result of execution of this script a list of page titles along with its URL is printed out followed by the total number of documents in the database for the query given, the time took for the query execution and the range of document numbers shown in the list.


require 'cgi'
require 'rest_client'
require 'json'

# The category of the results, 09 - for australian sites; this is specific for installation
_c = '09'
# number of results per page, i.e. how many results will be returned
_ps = 10
# result page number, starting with 0
_np = 0
# synonyms use flag, 1 - to use, 0 - don't
_sy = 0
# word forms use flag, 1 - to use, 0 - don't (search for words in query exactly)
_sp = 1
# search mode, can be 'near', 'all', 'any'
_m = 'near'
# results groupping by site flag, 'yes' - to group, 'no' - don't
_GroupBySite = 'no'
# search result template 
_tmplt = 'json2.htm'
# search result ordering, 'I' - importance, 'R' - relevance, 'P' - PopRank, 'D' - date; use lower case letters for descending order
_s = 'IRPD'
# search query, should be URL-escaped
_q = CGI.escape('careers')

response = RestClient.get('', {:params => {
                   :c => _c, 
                   :ps => _ps, 
                   :np => _np, 
                   :sy => _sy, 
                   :sp => _sp, 
                   :m => _m, 
                   'GroupBysite' => _GroupBySite, 
                   :tmplt => _tmplt, 
                   :s => _s, 
                   :q => _q
                 }}){ |response, request, result, &block|

  case response.code
  when 200
#    p "It worked !"
  when 423
    raise SomeCustomExceptionIfYouWant
    response.return!(request, result, &block)

result = JSON.parse(response)

result['responseData']['results'].each { |pos|
  print "#{pos['title']}\n => #{pos['url']}\n\n"

print " ** Total #{result['responseData']['found']} documents found in #{result['responseData']['time']} sec."
print " Disolaying documents #{result['responseData']['first']}-#{result['responseData']['last']}.\n"

1 thought on “Getting DataparkSearch results in Ruby

Leave a Reply

Your email address will not be published.