root/swish-python/examples/search.py

Revision 1677, 2.8 kB (checked in by augur, 4 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/env python
2
3 # test.py - swish-python example script.  Tests basic API functionality.
4 #
5 # Copyright (C) 2005 David L Norris <dave@webaugur.com>
6 #
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Library General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 import sys
23 import time
24 from swish import *
25
26
27 class swishtest:
28     # initialize the application
29     def __init__(self, argv):
30         if argv[1:]:
31             query=" ".join(argv[1:])
32             self.search(query)
33         else:
34             print('test.py <search query>')
35    
36    
37     def search(self, query):
38         file = "index.swish-e"
39         # create a new Swish-e instance
40         handle = SwishInit(file)
41        
42         # check for errors opening index file(s)
43         if SwishError(handle):
44             print( 'err: ' + SwishErrorString(handle) + ': Could not open the index file \'' + file + '\'' )
45             print('.')
46             sys.exit(1)
47        
48         # create a search object
49         search = New_Search_Object(handle, query)
50        
51         # time query
52         before=time.time()
53        
54         # fetch results object
55         results = SwishExecute(search, query)
56        
57         #time query
58         taken=time.time() - before
59        
60         # get number of hits
61         print('# Search words: ' + query )
62         print("# Number of hits: " + str(SwishHits(results)) )
63         print("# Search time: "+str(taken) + " seconds")
64         if SwishHits(results) == 0: print('err: no results')
65         while 1:
66             # seek to the next result object
67             result=SwishNextResult(results)
68             if not result: break
69            
70             # print some properties for each result
71             print( SwishResultPropertyStr(result, "swishrank") +" "+ \
72                 SwishResultPropertyStr(result, "swishdocpath") + ": \"" + \
73                 SwishResultPropertyStr(result, "swishtitle") + "\" " + \
74                 SwishResultPropertyStr(result, "swishdocsize") \
75                 )
76        
77         print('.')
78        
79         # free results object
80         Free_Results_Object(results)
81         # destroy the handle to free memory
82         SwishClose(handle)
83        
84 def main():
85     swishtest(sys.argv)
86
87 if __name__ == '__main__':
88     main()
Note: See TracBrowser for help on using the browser.