test.py 3.1 KB

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