本文整理汇总了Python中PyLucene类的典型用法代码示例。如果您正苦于以下问题:Python PyLucene类的具体用法?Python PyLucene怎么用?Python PyLucene使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PyLucene类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: begin_transaction
def begin_transaction(self):
"""PyLucene does not support transactions
Thus this function just opens the database for write access.
Call "cancel_transaction" or "commit_transaction" to close write
access in order to remove the exclusive lock from the database
directory.
"""
jvm = PyLucene.getVMEnv()
jvm.attachCurrentThread()
self._writer_open()
示例2: __del__
def __del__(self):
"""remove lock and close writer after loosing the last reference"""
jvm = PyLucene.getVMEnv()
jvm.attachCurrentThread()
self._writer_close()
if hasattr(self, "reader") and self.reader is not None:
self.reader.close()
self.reader = None
if hasattr(self, "searcher") and self.searcher is not None:
self.searcher.close()
self.searcher = None
示例3: __init__
def __init__(self, basedir, analyzer=None, create_allowed=True):
"""Initialize or open an indexing database.
Any derived class must override __init__.
:raise ValueError: The given location exists, but the database type
is incompatible (e.g. created by a different indexing engine)
:raise OSError: the database failed to initialize
:param basedir: The parent directory of the database
:type basedir: str
:param analyzer: Bitwise combination of possible analyzer flags
to be used as the default analyzer for this database.
Leave it empty to use the system default analyzer
(self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE,
self.ANALYZER_PARTIAL, ...
:type analyzer: int
:param create_allowed: create the database, if necessary; default: True
:type create_allowed: bool
"""
jvm = PyLucene.getVMEnv()
jvm.attachCurrentThread()
super(PyLuceneDatabase, self).__init__(basedir, analyzer=analyzer, create_allowed=create_allowed)
self.pyl_analyzer = PyLucene.StandardAnalyzer()
self.writer = None
self.reader = None
self.index_version = None
try:
# try to open an existing database
tempreader = PyLucene.IndexReader.open(self.location)
tempreader.close()
except PyLucene.JavaError, err_msg:
# Write an error out, in case this is a real problem instead of an absence of an index
# TODO: turn the following two lines into debug output
# errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str()
# DEBUG_FOO("could not open index, so going to create: " + errorstr)
# Create the index, so we can open cached readers on it
if not create_allowed:
raise OSError("Indexer: skipping database creation")
try:
# create the parent directory if it does not exist
parent_path = os.path.dirname(self.location)
if not os.path.isdir(parent_path):
# recursively create all directories up to parent_path
os.makedirs(parent_path)
except IOError, err_msg:
raise OSError(
"Indexer: failed to create the parent "
+ "directory (%s) of the indexing database: %s" % (parent_path, err_msg)
)
示例4: make_query
def make_query(self, *args, **kwargs):
jvm = PyLucene.getVMEnv()
jvm.attachCurrentThread()
return super(PyLuceneDatabase, self).make_query(*args, **kwargs)
示例5: package
import re
import os
import time
import logging
# try to import the PyLucene package (with the two possible names)
# remember the type of the detected package (compiled with jcc (>=v2.3) or
# with gcj (<=v2.2)
try:
import PyLucene
_COMPILER = 'gcj'
except ImportError:
# if this fails, then there is no pylucene installed
import lucene
PyLucene = lucene
PyLucene.initVM(PyLucene.CLASSPATH)
_COMPILER = 'jcc'
import CommonIndexer
UNNAMED_FIELD_NAME = "FieldWithoutAName"
MAX_FIELD_SIZE = 1048576
def is_available():
return _get_pylucene_version() == 2
class PyLuceneDatabase(CommonIndexer.CommonDatabase):
"""manage and use a pylucene indexing database"""
示例6: __init__
def __init__(self, basedir, analyzer=None, create_allowed=True):
"""Initialize or open an indexing database.
Any derived class must override __init__.
:raise ValueError: The given location exists, but the database type
is incompatible (e.g. created by a different indexing engine)
:raise OSError: the database failed to initialize
:param basedir: The parent directory of the database
:type basedir: str
:param analyzer: Bitwise combination of possible analyzer flags
to be used as the default analyzer for this database.
Leave it empty to use the system default analyzer
(self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE,
self.ANALYZER_PARTIAL, ...
:type analyzer: int
:param create_allowed: create the database, if necessary; default: True
:type create_allowed: bool
"""
jvm = PyLucene.getVMEnv()
jvm.attachCurrentThread()
super(PyLuceneDatabase, self).__init__(
basedir, analyzer=analyzer, create_allowed=create_allowed)
self.pyl_analyzer = PyLucene.StandardAnalyzer()
self.writer = None
self.reader = None
self.index_version = None
try:
# try to open an existing database
tempreader = PyLucene.IndexReader.open(self.location)
tempreader.close()
except PyLucene.JavaError as err_msg:
# Write an error out, in case this is a real problem instead of an absence of an index
# TODO: turn the following two lines into debug output
#errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str()
#DEBUG_FOO("could not open index, so going to create: " + errorstr)
# Create the index, so we can open cached readers on it
if not create_allowed:
raise OSError("Indexer: skipping database creation")
try:
# create the parent directory if it does not exist
parent_path = os.path.dirname(self.location)
if not os.path.isdir(parent_path):
# recursively create all directories up to parent_path
os.makedirs(parent_path)
except IOError as err_msg:
raise OSError("Indexer: failed to create the parent "
"directory (%s) of the indexing database: %s" %
(parent_path, err_msg))
try:
tempwriter = PyLucene.IndexWriter(
self.location, self.pyl_analyzer, True)
tempwriter.close()
except PyLucene.JavaError as err_msg:
raise OSError("Indexer: failed to open or create a Lucene"
" database (%s): %s" % (self.location, err_msg))
# the indexer is initialized - now we prepare the searcher
# windows file locking seems inconsistent, so we try 10 times
numtries = 0
#self.dir_lock.acquire(blocking=True)
# read "self.reader", "self.indexVersion" and "self.searcher"
try:
while numtries < 10:
try:
self.reader = PyLucene.IndexReader.open(self.location)
self.indexVersion = self.reader.getCurrentVersion(
self.location)
self.searcher = PyLucene.IndexSearcher(self.reader)
break
except PyLucene.JavaError as e:
# store error message for possible later re-raise (below)
lock_error_msg = e
time.sleep(0.01)
numtries += 1
else:
# locking failed for 10 times
raise OSError("Indexer: failed to lock index database"
" (%s)" % lock_error_msg)
finally:
pass
# self.dir_lock.release()
# initialize the searcher and the reader
self._index_refresh()