Navigation Menu

Skip to content

Commit

Permalink
Add search script
Browse files Browse the repository at this point in the history
It just reports elapsed time and the number of hits.
  • Loading branch information
kou committed Apr 10, 2014
1 parent 6ba2367 commit 699c1cb
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Gemfile
Expand Up @@ -5,7 +5,7 @@ source "https://rubygems.org/"
gem "rake"
gem "bundler"
gem "grn2drn"
gem "droonga-client"
gem "cool.io"
gem "test-unit", :require => false

base_dir = File.dirname(__FILE__)
Expand All @@ -15,3 +15,10 @@ if File.exist?(local_fluent_plugin_droonga)
else
gem "fluent-plugin-droonga", :github => "droonga/fluent-plugin-droonga"
end

local_droonga_client_ruby = File.join(base_dir, "..", "droonga-client-ruby")
if File.exist?(local_droonga_client_ruby)
gem "droonga-client", :path => local_droonga_client_ruby
else
gem "droonga-client", :github => "droonga/droonga-client-ruby"
end
119 changes: 119 additions & 0 deletions bin/wikipedia-search.rb
@@ -0,0 +1,119 @@
#!/usr/bin/env ruby

require "pathname"
require "ostruct"
require "optparse"

require "coolio"
require "droonga/client"

base_dir_path = Pathname.new(__FILE__).dirname
lib_dir_path = base_dir_path + "lib"

$LOAD_PATH.unshift(lib_dir_path.to_s)

require "wikipedia-search/groonga-converter"

options = OpenStruct.new
options.protocol = "droonga"
options.host = "127.0.0.1"
options.port = 24000
options.max_n_connections = 10
options.show_response = false

protocols = ["droonga", "droonga-http", "groonga-http"]
parser = OptionParser.new
parser.on("--protocol=PROTOCOL", protocols,
"Use PROTOCOL for search.",
"(#{protocols.join(', ')})") do |protocol|
options.protocol = protocol
end
parser.on("--host=HOST",
"Search against HOST.",
"(#{options.host})") do |host|
options.host = host
end
parser.on("--port=PORT", Integer,
"Use PORT as the server port number.",
"(#{options.port})") do |port|
options.port = port
end
parser.on("--max-n-connections=N", Integer,
"Use N connections for search.",
"(#{options.max_n_connections})") do |n|
options.max_n_connections = n
end
parser.parse!(ARGV)

def build_droonga_message(query)
{
"type" => "search",
"dataset" => "Wikipedia",
"body" => {
"queries" => {
"pages" => {
"source" => "Pages",
"condition" => {
"matchTo" => ["title", "text"],
"query" => query,
},
"output" => {
"elements" => ["count", "records"],
"attributes" => [
"_key",
"title",
"text",
],
"limit" => 10,
},
},
},
},
}
end

def send_request(query, client, options)
start = Time.now
client.request(build_droonga_message(query)) do |response|
elapsed = Time.now - start
status_code = response["statusCode"]
if status_code == 200
n_hits = response["body"]["pages"]["count"]
else
n_hits = 0
end
p [elapsed, query, status_code, n_hits]
yield
end
end

def run_request(queries, client, options)
if queries.empty?
client.close
return
end

query = queries.pop
send_request(query, client, options) do
run_request(queries, client, options)
end
end

queries = ARGF.each_line.collect do |line|
line.strip
end

loop = Coolio::Loop.default
options.max_n_connections.times do
client_options = {
:host => options.host,
:port => options.port,
:tag => "droonga",
:protocol => :droonga,
:backend => :coolio,
:loop => loop,
}
client = Droonga::Client.new(client_options)
run_request(queries, client, options)
end
loop.run

0 comments on commit 699c1cb

Please sign in to comment.