-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·127 lines (114 loc) · 3.58 KB
/
test.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# $Id$
#
from sphinxapi import *
import sys, time
if not sys.argv[1:]:
print "Usage: python test.py [OPTIONS] query words\n"
print "Options are:"
print "-h, --host <HOST>\tconnect to searchd at host HOST"
print "-p, --port\t\tconnect to searchd at port PORT"
print "-i, --index <IDX>\tsearch through index(es) specified by IDX"
print "-s, --sortby <EXPR>\tsort matches by 'EXPR'"
print "-a, --any\t\tuse 'match any word' matching mode"
print "-b, --boolean\t\tuse 'boolean query' matching mode"
print "-e, --extended\t\tuse 'extended query' matching mode"
print "-f, --filter <ATTR>\tfilter by attribute 'ATTR' (default is 'group_id')"
print "-v, --value <VAL>\tadd VAL to allowed 'group_id' values list"
print "-g, --groupby <EXPR>\tgroup matches by 'EXPR'"
print "-gs,--groupsort <EXPR>\tsort groups by 'EXPR'"
print "-l, --limit <COUNT>\tretrieve COUNT matches (default is 20)"
#sys.exit(0)
q = ''
mode = SPH_MATCH_EXTENDED
host = '10.108.102.28'
port = 9312
index = '*'
filtercol = ''
filtervals = ''
sortby = ''
groupby = ''
groupsort = '@group desc'
limit = 20
i = 1
while (i<len(sys.argv)):
arg = sys.argv[i]
if arg=='-h' or arg=='--host':
i += 1
host = sys.argv[i]
elif arg=='-p' or arg=='--port':
i += 1
port = int(sys.argv[i])
elif arg=='-i':
i += 1
index = sys.argv[i]
elif arg=='-s':
i += 1
sortby = sys.argv[i]
elif arg=='-a' or arg=='--any':
mode = SPH_MATCH_ANY
elif arg=='-b' or arg=='--boolean':
mode = SPH_MATCH_BOOLEAN
elif arg=='-e' or arg=='--extended':
mode = SPH_MATCH_EXTENDED
elif arg=='-f' or arg=='--filter':
i += 1
filtercol = sys.argv[i]
elif arg=='-v' or arg=='--value':
i += 1
filtervals.append ( int(sys.argv[i]) )
elif arg=='-g' or arg=='--groupby':
i += 1
groupby = sys.argv[i]
elif arg=='-gs' or arg=='--groupsort':
i += 1
groupsort = sys.argv[i]
elif arg=='-l' or arg=='--limit':
i += 1
limit = int(sys.argv[i])
else:
q = '%s%s ' % ( q, arg )
i += 1
# do query
cl = SphinxClient()
cl.SetServer ( host, port )
#cl.SetFieldWeights({'title': 10})
cl.SetMatchMode ( mode )
if filtervals:
cl.SetFilterString ( filtercol, filtervals )
if groupby:
cl.SetGroupBy ( groupby, SPH_GROUPBY_ATTR, groupsort )
if sortby:
cl.SetSortMode ( SPH_SORT_EXTENDED, sortby )
if limit:
cl.SetLimits ( 0, limit, max(limit, 1000) )
res = cl.Query ( '"' + q + '"/0.75', index )
if not res:
print 'query failed: %s' % cl.GetLastError()
sys.exit(1)
if cl.GetLastWarning():
print 'WARNING: %s\n' % cl.GetLastWarning()
print 'Query \'%s\' retrieved %d of %d matches in %s sec' % (q, res['total'], res['total_found'], res['time'])
print 'Query stats:'
if res.has_key('words'):
for info in res['words']:
print '\t\'%s\' found %d times in %d documents' % (info['word'], info['hits'], info['docs'])
if res.has_key('matches'):
n = 1
print '\nMatches:'
for match in res['matches']:
attrsdump = ''
for attr in res['attrs']:
attrname = attr[0]
attrtype = attr[1]
value = match['attrs'][attrname]
if attrtype==SPH_ATTR_TIMESTAMP:
value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) )
attrsdump = '%s, %s=%s' % ( attrsdump, attrname, value )
print '%d. doc_id=%s, weight=%d%s' % (n, match['id'], match['weight'], attrsdump)
n += 1
#
# $Id$
#