old python needs __main__ to call a module
[pylucene.git] / samples / SearchFiles.py
1 #!/usr/bin/env python
2 from lucene import \
3     QueryParser, IndexSearcher, StandardAnalyzer, SimpleFSDirectory, File, \
4     VERSION, initVM, Version
5
6
7 """
8 This script is loosely based on the Lucene (java implementation) demo class 
9 org.apache.lucene.demo.SearchFiles.  It will prompt for a search query, then it
10 will search the Lucene index in the current directory called 'index' for the
11 search query entered against the 'contents' field.  It will then display the
12 'path' and 'name' fields for each of the hits it finds in the index.  Note that
13 search.close() is currently commented out because it causes a stack overflow in
14 some cases.
15 """
16 def run(searcher, analyzer):
17     while True:
18         print
19         print "Hit enter with no input to quit."
20         command = raw_input("Query:")
21         if command == '':
22             return
23
24         print
25         print "Searching for:", command
26         query = QueryParser(Version.LUCENE_CURRENT, "contents",
27                             analyzer).parse(command)
28         scoreDocs = searcher.search(query, 50).scoreDocs
29         print "%s total matching documents." % len(scoreDocs)
30
31         for scoreDoc in scoreDocs:
32             doc = searcher.doc(scoreDoc.doc)
33             print 'path:', doc.get("path"), 'name:', doc.get("name")
34
35
36 if __name__ == '__main__':
37     STORE_DIR = "index"
38     initVM()
39     print 'lucene', VERSION
40     directory = SimpleFSDirectory(File(STORE_DIR))
41     searcher = IndexSearcher(directory, True)
42     analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
43     run(searcher, analyzer)
44     searcher.close()