- Try full-text search, limit 10 results

This commit is contained in:
Jordan Sissel 2010-04-14 09:03:42 +00:00
parent 5c9922be05
commit 418efeb2fd
2 changed files with 22 additions and 55 deletions

View file

@ -36,62 +36,25 @@ module LogStash; module DB;
@tdb.close
end
public
def addindex(column, type)
case type
when "string"
@tdb.setindex(column, TokyoCabinet::TDB::ITTOKEN)
#@tdb.setindex(column, TokyoCabinet::TDB::ITLEXICAL)
else
STDERR.puts("Invalid index type: #{type}")
end
end
end # class Index
end; end # module LogStash::DB
class LogStash::DB::IndexReader
def initialize(path)
@tdb = TokyoCabinet::TDB::new
@path = path
open_db
end # def initialize
private
def open_db
ret = @tdb.open(@path, TokyoCabinet::TDB::OREADER | TokyoCabinet::TDB::ONOLCK)
if !ret
ecode = @tdb.ecode
STDERR.puts("open error: #{@tdb.errmsg(ecode)}")
end
end
public
def each
@tdb.iterinit
while ((key = @tdb.iternext()) != nil)
yield key, @tdb.get(key)
end
end
public
def search(conditions)
query = TokyoCabinet::TDBQRY.new(@tdb)
conditions.each do |key, value|
query.addcond(key, TokyoCabinet::TDBQRY::QCSTREQ, value)
end
results = query.search
results.each do |key|
data = @tdb.get(key)
yield key, data
end
end
end
if __FILE__ == $0
i = LogStash::DB::IndexReader.new(ARGV[0])
qargs = ARGV[1..-1]
#i.each do |key, val|
#ap [key, val]
#end
query = {}
qargs.each do |arg|
i = LogStash::DB::Index.new(ARGV[0])
args = ARGV[1..-1]
args.each do |arg|
key, val = arg.split(":", 2)
query[key] = val
end
ap query
i.search(query) do |key, value|
ap [key, value["@DATE"], value["@LINE"]]
i.addindex(key, val)
end
i.close
end

View file

@ -32,9 +32,11 @@ module LogStash; module DB;
query = TokyoCabinet::TDBQRY.new(@tdb)
conditions.each do |key, value|
#query.addcond(key, TokyoCabinet::TDBQRY::QCSTREQ, value)
query.addcond(key, TokyoCabinet::TDBQRY::QCSTRINC, value)
#query.addcond(key, TokyoCabinet::TDBQRY::QCSTRINC, value)
query.addcond(key, TokyoCabinet::TDBQRY::QCFTSAND, value)
end
query.setorder("@DATE", TDBQRY::QONUMASC);
query.setorder("@DATE", TokyoCabinet::TDBQRY::QONUMASC);
query.setlimit(10)
results = query.search
results.each do |key|
data = @tdb.get(key)
@ -55,6 +57,8 @@ if __FILE__ == $0
ap query
i.search(query) do |key, value|
ap [key, value["@DATE"], value["@LINE"]]
#ap [key, value["@DATE"], value["@LINE"]]
#puts value["@LINE"]
ap value
end
end