1#
2# $Id$
3#
4
5from sphinxapi import *
6import sys, time
7
8if not sys.argv[1:]:
9	print "Usage: python test.py [OPTIONS] query words\n"
10	print "Options are:"
11	print "-h, --host <HOST>\tconnect to searchd at host HOST"
12	print "-p, --port\t\tconnect to searchd at port PORT"
13	print "-i, --index <IDX>\tsearch through index(es) specified by IDX"
14	print "-s, --sortby <EXPR>\tsort matches by 'EXPR'"
15	print "-a, --any\t\tuse 'match any word' matching mode"
16	print "-b, --boolean\t\tuse 'boolean query' matching mode"
17	print "-e, --extended\t\tuse 'extended query' matching mode"
18	print "-f, --filter <ATTR>\tfilter by attribute 'ATTR' (default is 'group_id')"
19	print "-v, --value <VAL>\tadd VAL to allowed 'group_id' values list"
20	print "-g, --groupby <EXPR>\tgroup matches by 'EXPR'"
21	print "-gs,--groupsort <EXPR>\tsort groups by 'EXPR'"
22	print "-l, --limit <COUNT>\tretrieve COUNT matches (default is 20)"
23	sys.exit(0)
24
25q = ''
26mode = SPH_MATCH_ALL
27host = 'localhost'
28port = 9312
29index = '*'
30filtercol = 'group_id'
31filtervals = []
32sortby = ''
33groupby = ''
34groupsort = '@group desc'
35limit = 0
36
37i = 1
38while (i<len(sys.argv)):
39	arg = sys.argv[i]
40	if arg=='-h' or arg=='--host':
41		i += 1
42		host = sys.argv[i]
43	elif arg=='-p' or arg=='--port':
44		i += 1
45		port = int(sys.argv[i])
46	elif arg=='-i':
47		i += 1
48		index = sys.argv[i]
49	elif arg=='-s':
50		i += 1
51		sortby = sys.argv[i]
52	elif arg=='-a' or arg=='--any':
53		mode = SPH_MATCH_ANY
54	elif arg=='-b' or arg=='--boolean':
55		mode = SPH_MATCH_BOOLEAN
56	elif arg=='-e' or arg=='--extended':
57		mode = SPH_MATCH_EXTENDED
58	elif arg=='-f' or arg=='--filter':
59		i += 1
60		filtercol = sys.argv[i]
61	elif arg=='-v' or arg=='--value':
62		i += 1
63		filtervals.append ( int(sys.argv[i]) )
64	elif arg=='-g' or arg=='--groupby':
65		i += 1
66		groupby = sys.argv[i]
67	elif arg=='-gs' or arg=='--groupsort':
68		i += 1
69		groupsort = sys.argv[i]
70	elif arg=='-l' or arg=='--limit':
71		i += 1
72		limit = int(sys.argv[i])
73	else:
74		q = '%s%s ' % ( q, arg )
75	i += 1
76
77# do query
78cl = SphinxClient()
79cl.SetServer ( host, port )
80cl.SetMatchMode ( mode )
81if filtervals:
82	cl.SetFilter ( filtercol, filtervals )
83if groupby:
84	cl.SetGroupBy ( groupby, SPH_GROUPBY_ATTR, groupsort )
85if sortby:
86	cl.SetSortMode ( SPH_SORT_EXTENDED, sortby )
87if limit:
88	cl.SetLimits ( 0, limit, max(limit,1000) )
89res = cl.Query ( q, index )
90
91if not res:
92	print 'query failed: %s' % cl.GetLastError()
93	sys.exit(1)
94
95if cl.GetLastWarning():
96	print 'WARNING: %s\n' % cl.GetLastWarning()
97
98print 'Query \'%s\' retrieved %d of %d matches in %s sec' % (q, res['total'], res['total_found'], res['time'])
99print 'Query stats:'
100
101if res.has_key('words'):
102	for info in res['words']:
103		print '\t\'%s\' found %d times in %d documents' % (info['word'], info['hits'], info['docs'])
104
105if res.has_key('matches'):
106	n = 1
107	print '\nMatches:'
108	for match in res['matches']:
109		attrsdump = ''
110		for attr in res['attrs']:
111			attrname = attr[0]
112			attrtype = attr[1]
113			value = match['attrs'][attrname]
114			if attrtype==SPH_ATTR_TIMESTAMP:
115				value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) )
116			attrsdump = '%s, %s=%s' % ( attrsdump, attrname, value )
117
118		print '%d. doc_id=%s, weight=%d%s' % (n, match['id'], match['weight'], attrsdump)
119		n += 1
120
121#
122# $Id$
123#
124