Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@naoa

naoa/ddl.grn Secret

Last active June 15, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoa/2be78db216c28856996d8e1a524a4a88 to your computer and use it in GitHub Desktop.
Save naoa/2be78db216c28856996d8e1a524a4a88 to your computer and use it in GitHub Desktop.
table_create Books TABLE_HASH_KEY ShortText
column_create Books authors COLUMN_VECTOR ShortText
load --table Books
[
{"_key": "Hello Groonga", "authors": ["Taro", "Hanako"]},
{"_key": "The first step for Groonga", "authors": ["Taro"]},
{"_key": "Mastering Groonga", "authors": ["Hanako"]}
]
require 'ostruct'
require 'json'
cmd = "groonga /tmp/db"
query = "select Books --drilldown authors --command_version 3"
#query = "select Books --drilldowns[authors] authors --command_version 3"
ret = `#{cmd} #{query}`
results = JSON.parse ret
elapsed_time = results["header"]["elapsed_time"]
n_hits = results["body"]["n_hits"]
columns = results["body"]["columns"].map {|c| c["name"] }
docs = results["body"]["records"].map do |record|
doc = {}
record.each_with_index {|value, t| doc[columns[t]] = value }
OpenStruct.new(doc)
end
puts n_hits
docs.each do |doc|
puts "#{doc._key}: #{doc.authors}"
end
#n_hits = results["body"]["drilldowns"]["authors"]["n_hits"]
#columns = results["body"]["drilldowns"]["authors"]["columns"].map {|c| c["name"] }
#docs = results["body"]["drilldowns"]["authors"]["records"].map do |record|
# doc = {}
# record.each_with_index {|value, t| doc[columns[t]] = value }
# OpenStruct.new(doc)
#end
#puts n_hits
#docs.each do |doc|
# puts "#{doc._key}: #{doc._nsubrecs}"
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment