[Groonga-commit] droonga/wikipedia-search at 699c1cb [master] Add search script

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 10 17:32:57 JST 2014


Kouhei Sutou	2014-04-10 17:32:57 +0900 (Thu, 10 Apr 2014)

  New Revision: 699c1cb657b86713267ab16c237969b8976b9d98
  https://github.com/droonga/wikipedia-search/commit/699c1cb657b86713267ab16c237969b8976b9d98

  Message:
    Add search script
    
    It just reports elapsed time and the number of hits.

  Added files:
    bin/wikipedia-search.rb
  Modified files:
    Gemfile

  Modified: Gemfile (+8 -1)
===================================================================
--- Gemfile    2014-04-08 18:54:52 +0900 (f9605e2)
+++ Gemfile    2014-04-10 17:32:57 +0900 (86aec66)
@@ -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__)
@@ -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

  Added: bin/wikipedia-search.rb (+119 -0) 100755
===================================================================
--- /dev/null
+++ bin/wikipedia-search.rb    2014-04-10 17:32:57 +0900 (37d2fb6)
@@ -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
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index