-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole
More file actions
executable file
·109 lines (100 loc) · 3.03 KB
/
console
File metadata and controls
executable file
·109 lines (100 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env ruby
require 'readline'
require 'lib/indextank_client'
@apiurl = nil
@idxname = nil
@api = nil
@index = nil
@data = {}
@start = 0
@len = 10
@data['start'] = @start
@data['len'] = @len
def command(line)
args = line.split
case args[0]
when "url"
@apiurl = args[1]
@api = IndexTank::ApiClient.new @apiurl
@data['api'] = @api
printf "api url set to %s\n", @apiurl
when "list"
@api.list_indexes.each {|x| puts x.name}
when "index"
if !args[1]
puts @idxname
else
@idxname = args[1]
@index = @api.get_index @idxname
@data['index'] = @index
printf "index set to %s\n", @idxname
end
when "search"
query = args[1..args.length].join ' '
results = @index.search(query, :start => @start, :len => @len, :function => 1)
printf "%s results in %s seconds\n", results['matches'], results['search_time']
results['results'].each {|doc|
docid = doc['docid']
printf "docid: #{docid}\n"
}
when "add"
if @index == nil
puts 'set an index first'
else
if !args[1]
docid = Readline.readline('enter docid (blank to abort):', true)
else
docid = args[1]
end
if docid.strip != ''
doc = {}
while true
field = Readline.readline('field name (or blank to end): ', true)
if field.strip == ''
break
end
value = Readline.readline('field value: ', true)
doc[field] = value
end
@index.add_document(docid, doc)
printf("added %s\n", docid)
end
end
when "delete_index"
printf "deleting %s\n", args[1]
@api.get_index(args[1]).delete_index
when "set"
@data.each {|x| p x}
when "len"
@len = args[1].to_i
@data['len'] = @len
when "start"
@start = args[1].to_i
@data['start'] = @start
when "help"
printf "possible commands are:\n\nurl [url] sets the api url\nlist lists all indexes\nindex [index name] sets the index name\ndelete_index [index name] deletes an index \
\nsearch [query] searches the current index\nadd [docid] creates a new document\nstart [n] show results starting from n\nlen [n] show n results\nset shows current settings\nquit exits this shell\n\n"
else
puts 'no such command, try "help"'
end
end
if File.exists? '.console'
@data = Marshal.load File.open('.console')
@index = @data['index']
@api = @data['api']
@len = @data['len']
@start = @data['start']
if @index
@idxname = @index.name
end
end
while line = Readline.readline('> ', true) and line != 'quit'
begin
command line
f = File.open '.console', 'w'
Marshal.dump(@data, f)
f.close
rescue Exception => e
puts e
end
end