test.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. sys.exit(0)
  21. q = ''
  22. mode = SPH_MATCH_ALL
  23. host = 'localhost'
  24. port = 3312
  25. index = '*'
  26. filtercol = 'group_id'
  27. filtervals = []
  28. sortby = ''
  29. groupby = ''
  30. groupsort = '@group desc'
  31. i = 1
  32. while (i<len(sys.argv)):
  33. arg = sys.argv[i]
  34. if arg=='-h' or arg=='--host':
  35. i += 1
  36. host = sys.argv[i]
  37. elif arg=='-p' or arg=='--port':
  38. i += 1
  39. port = int(sys.argv[i])
  40. elif arg=='-i':
  41. i += 1
  42. index = sys.argv[i]
  43. elif arg=='-s':
  44. i += 1
  45. sortby = sys.argv[i]
  46. elif arg=='-a' or arg=='--any':
  47. mode = SPH_MATCH_ANY
  48. elif arg=='-b' or arg=='--boolean':
  49. mode = SPH_MATCH_BOOLEAN
  50. elif arg=='-e' or arg=='--extended':
  51. mode = SPH_MATCH_EXTENDED
  52. elif arg=='-f' or arg=='--filter':
  53. i += 1
  54. filtercol = sys.argv[i]
  55. elif arg=='-v' or arg=='--value':
  56. i += 1
  57. filtervals.append ( int(sys.argv[i]) )
  58. elif arg=='-g' or arg=='--groupby':
  59. i += 1
  60. groupby = sys.argv[i]
  61. elif arg=='-gs' or arg=='--groupsort':
  62. i += 1
  63. groupsort = sys.argv[i]
  64. else:
  65. q = '%s%s ' % ( q, arg )
  66. i += 1
  67. # do query
  68. cl = SphinxClient()
  69. cl.SetServer ( host, port )
  70. cl.SetWeights ( [100, 1] )
  71. cl.SetMatchMode ( mode )
  72. if filtervals:
  73. cl.SetFilter ( filtercol, filtervals )
  74. if groupby:
  75. cl.SetGroupBy ( groupby, SPH_GROUPBY_ATTR, groupsort )
  76. if sortby:
  77. cl.SetSortMode ( SPH_SORT_EXTENDED, sortby )
  78. res = cl.Query ( q, index )
  79. if not res:
  80. print 'query failed: %s' % cl.GetLastError()
  81. sys.exit(1)
  82. if cl.GetLastWarning():
  83. print 'WARNING: %s\n' % cl.GetLastWarning()
  84. print 'Query \'%s\' retrieved %d of %d matches in %s sec' % (q, res['total'], res['total_found'], res['time'])
  85. print 'Query stats:'
  86. if res.has_key('words'):
  87. for info in res['words']:
  88. print '\t\'%s\' found %d times in %d documents' % (info['word'], info['hits'], info['docs'])
  89. if res.has_key('matches'):
  90. n = 1
  91. print '\nMatches:'
  92. for match in res['matches']:
  93. attrsdump = ''
  94. for attr in res['attrs']:
  95. attrname = attr[0]
  96. attrtype = attr[1]
  97. value = match['attrs'][attrname]
  98. if attrtype==SPH_ATTR_TIMESTAMP:
  99. value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) )
  100. attrsdump = '%s, %s=%s' % ( attrsdump, attrname, value )
  101. print '%d. doc_id=%s, weight=%d%s' % (n, match['id'], match['weight'], attrsdump)
  102. n += 1
  103. #
  104. # $Id$
  105. #