# -*- coding:GB2312 -*- import sys, os, PyLucene, threading, time from datetime import datetime """ This class is loosely based on the Lucene (java implementation) demo class org.apache.lucene.demo.IndexFiles. It will take a directory as an argument and will index all of the files in that directory and downward recursively. It will index on the file path, the file name and the file contents. The resulting Lucene index will be placed in the current directory and called 'index'. """ class Ticker(object): def__init__(self): self.tick = True def run(self): while self.tick: sys.stdout.write('.') sys.stdout.flush() time.sleep(1.0) class IndexFiles(object): """Usage: python IndexFiles <doc_directory>""" def__init__(self, root, storeDir, analyzer): ifnot os.path.exists(storeDir): os.mkdir(storeDir) store = PyLucene.FSDirectory.getDirectory(storeDir, False) writer = PyLucene.IndexWriter(store, analyzer, False) writer.setMaxFieldLength(1048576) self.indexDocs(root, writer) ticker = Ticker() print'optimizing index', threading.Thread(target=ticker.run).start() writer.optimize() writer.close() ticker.tick = False print'done' def indexDocs(self, root, writer): for root, dirnames, filenames in os.walk(root): print root try: sroot = unicode(root, 'GBK') print sroot except: print"*****************************unicode error" print root continue #add dir doc = PyLucene.Document() doc.add(PyLucene.Field("path", sroot, PyLucene.Field.Store.YES, PyLucene.Field.Index.UN_TOKENIZED)) doc.add(PyLucene.Field("name", sroot, PyLucene.Field.Store.YES, PyLucene.Field.Index.TOKENIZED)) writer.addDocument(doc) for filename in filenames: try: filename = unicode(filename, 'GBK') except: print"*****************************unicode error" print filename continue print"adding", filename try: #path = unicode(root, 'GB2312')# # path =os.path.join(sroot, filename) #file = open(path) #contents = unicode(file.read(), 'iso-8859-1') #contents = unicode(file.read(), 'GBK') #file.close() doc = PyLucene.Document() doc.add(PyLucene.Field("path", path, PyLucene.Field.Store.YES, PyLucene.Field.Index.UN_TOKENIZED)) doc.add(PyLucene.Field("name", filename, PyLucene.Field.Store.YES, PyLucene.Field.Index.TOKENIZED)) ''' if len(contents) > 0: doc.add(PyLucene.Field("contents", contents, PyLucene.Field.Store.YES, PyLucene.Field.Index.TOKENIZED)) else: print "warning: no content in %s" % filename ''' writer.addDocument(doc) except Exception, e: print"Failed in indexDocs:", e __debug= 0 if__name__=='__main__': if__debug!=1: if len(sys.argv) <2: print IndexFiles.__doc__ sys.exit(1) print'PyLucene', PyLucene.VERSION, 'Lucene', PyLucene.LUCENE_VERSION start = datetime.now() try: if__debug!=1: IndexFiles(sys.argv[1], "index", PyLucene.StandardAnalyzer()) else: IndexFiles(r'c:/testccc', "index", PyLucene.StandardAnalyzer()) end = datetime.now() print end - start except Exception, e: print"Failed: ", e
SearchFiles.py
from PyLucene import QueryParser, IndexSearcher, StandardAnalyzer, FSDirectory from PyLucene import VERSION, LUCENE_VERSION """ This script is loosely based on the Lucene (java implementation) demo class org.apache.lucene.demo.SearchFiles. It will prompt for a search query, then it will search the Lucene index in the current directory called 'index' for the search query entered against the 'contents' field. It will then display the 'path' and 'name' fields for each of the hits it finds in the index. Note that search.close() is currently commented out because it causes a stack overflow in some cases. """ def run(searcher, analyzer): while True: print print"Hit enter with no input to quit." command = raw_input("Query:") command = unicode(command, 'GBK') if command =='': return print print"Searching for:", command #query = QueryParser("contents", analyzer).parse(command) query = QueryParser("name", analyzer).parse(command) hits = searcher.search(query) print"%s total matching documents."% hits.length() for i, doc in hits: print'path:', doc.get("path"), 'name:', doc.get("name") if__name__=='__main__': STORE_DIR ="index" print'PyLucene', VERSION, 'Lucene', LUCENE_VERSION directory = FSDirectory.getDirectory(STORE_DIR, False) searcher = IndexSearcher(directory) analyzer = StandardAnalyzer() run(searcher, analyzer) searcher.close()
建立索引,运行:
python IndexFiles.py c:/
查找的时候,运行:
python SearchFiles.py
如果只查找一个关键词则直接输入;如果想同时查找两个关键词,如Python 网络,则输入:Python AND 网络;如果想查找Python或网络则:Python 网络,也可以Python OR 网络。