3 QueryParser, IndexSearcher, StandardAnalyzer, SimpleFSDirectory, File, \
4 VERSION, initVM, Version
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
16 def run(searcher, analyzer):
19 print "Hit enter with no input to quit."
20 command = raw_input("Query:")
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)
31 for scoreDoc in scoreDocs:
32 doc = searcher.doc(scoreDoc.doc)
33 print 'path:', doc.get("path"), 'name:', doc.get("name")
36 if __name__ == '__main__':
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)