File:  [Repository] / cdli / searchIndex.py
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Wed Mar 21 19:29:23 2007 UTC (17 years, 3 months ago) by dwinter
Branches: MAIN
CVS tags: zcat_only_1, Root_zcat_only_1, HEAD
new indices

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>