test.py 3.2 KB

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