Package org.elasticsearch.common.lucene
Class Lucene
- java.lang.Object
-
- org.elasticsearch.common.lucene.Lucene
-
public class Lucene extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static org.apache.lucene.search.ScoreDoc[]
EMPTY_SCORE_DOCS
static org.apache.lucene.search.TopDocs
EMPTY_TOP_DOCS
static NamedAnalyzer
KEYWORD_ANALYZER
static java.lang.String
LATEST_CODEC
static java.lang.String
LATEST_DOC_VALUES_FORMAT
static java.lang.String
LATEST_POSTINGS_FORMAT
static java.lang.String
SOFT_DELETES_FIELD
static NamedAnalyzer
STANDARD_ANALYZER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.lucene.util.Bits
asSequentialAccessBits(int maxDoc, org.apache.lucene.search.ScorerSupplier scorerSupplier)
Given aScorerSupplier
, return aBits
instance that will match all documents contained in the set.static void
checkSegmentInfoIntegrity(org.apache.lucene.store.Directory directory)
static void
cleanLuceneIndex(org.apache.lucene.store.Directory directory)
This method removes all lucene files from the given directory.static org.apache.lucene.index.LeafReader
emptyReader(int maxDoc)
Returns an empty leaf reader with the given max docs.static boolean
exists(org.apache.lucene.search.IndexSearcher searcher, org.apache.lucene.search.Query query)
Check whether there is one or more documents matching the provided query.static java.lang.Iterable<java.lang.String>
files(org.apache.lucene.index.SegmentInfos infos)
Returns an iterable that allows to iterate over all files in this segments infostatic org.apache.lucene.index.IndexCommit
getIndexCommit(org.apache.lucene.index.SegmentInfos si, org.apache.lucene.store.Directory directory)
Returns an index commit for the givenSegmentInfos
in the given directory.static int
getNumDocs(org.apache.lucene.index.SegmentInfos info)
Returns the number of documents in the index referenced by thisSegmentInfos
static org.apache.lucene.search.Scorer
illegalScorer(java.lang.String message)
Return a Scorer that throws an ElasticsearchIllegalStateException on all operations with the given message.static boolean
indexExists(org.apache.lucene.store.Directory directory)
static boolean
isCorruptionException(java.lang.Throwable t)
Returnstrue
iff the given exception or one of it's causes is an instance ofCorruptIndexException
,IndexFormatTooOldException
, orIndexFormatTooNewException
otherwisefalse
.static org.apache.lucene.document.NumericDocValuesField
newSoftDeletesField()
Returns a numeric docvalues which can be used to soft-delete documents.static org.apache.lucene.util.Version
parseVersion(java.lang.String version, org.apache.lucene.util.Version defaultVersion, org.apache.logging.log4j.Logger logger)
static org.apache.lucene.util.Version
parseVersionLenient(java.lang.String toParse, org.apache.lucene.util.Version defaultValue)
Parses the version string lenient and returns the default value if the given string is null or emptystatic org.apache.lucene.index.SegmentInfos
pruneUnreferencedFiles(java.lang.String segmentsFileName, org.apache.lucene.store.Directory directory)
This method removes all files from the given directory that are not referenced by the given segments file.static org.apache.lucene.search.Explanation
readExplanation(StreamInput in)
static org.apache.lucene.search.FieldDoc
readFieldDoc(StreamInput in)
static org.apache.lucene.search.ScoreDoc
readScoreDoc(StreamInput in)
static org.apache.lucene.index.SegmentInfos
readSegmentInfos(org.apache.lucene.index.IndexCommit commit)
Reads the segments infos from the given commit, failing if it fails to loadstatic org.apache.lucene.index.SegmentInfos
readSegmentInfos(org.apache.lucene.store.Directory directory)
Reads the segments infos, failing if it fails to loadstatic org.apache.lucene.search.SortField
readSortField(StreamInput in)
static org.apache.lucene.search.SortField.Type
readSortType(StreamInput in)
static java.lang.Comparable
readSortValue(StreamInput in)
static org.apache.lucene.search.TopDocs
readTopDocs(StreamInput in)
static void
scanSeqNosInReader(org.apache.lucene.index.DirectoryReader directoryReader, long fromSeqNo, long toSeqNo, java.util.function.LongConsumer onNewSeqNo)
Scans sequence numbers (i.e.,SeqNoFieldMapper.NAME
) betweenfromSeqNo
(inclusive) andtoSeqNo
(inclusive) in the provided directory reader.static org.apache.lucene.index.SegmentReader
segmentReader(org.apache.lucene.index.LeafReader reader)
Tries to extract a segment reader from the given index reader.static boolean
waitForIndex(org.apache.lucene.store.Directory directory, long timeLimitMillis)
Wait for an index to exist for up totimeLimitMillis
.static org.apache.lucene.index.DirectoryReader
wrapAllDocsLive(org.apache.lucene.index.DirectoryReader in)
Wraps a directory reader to make all documents live except those were rolled back or hard-deleted due to non-aborting exceptions during indexing.static void
writeExplanation(StreamOutput out, org.apache.lucene.search.Explanation explanation)
static void
writeFieldDoc(StreamOutput out, org.apache.lucene.search.FieldDoc fieldDoc)
static void
writeScoreDoc(StreamOutput out, org.apache.lucene.search.ScoreDoc scoreDoc)
static void
writeSortField(StreamOutput out, org.apache.lucene.search.SortField sortField)
static void
writeSortType(StreamOutput out, org.apache.lucene.search.SortField.Type sortType)
static void
writeSortValue(StreamOutput out, java.lang.Object field)
static void
writeTopDocs(StreamOutput out, org.apache.lucene.search.TopDocs topDocs)
-
-
-
Field Detail
-
LATEST_DOC_VALUES_FORMAT
public static final java.lang.String LATEST_DOC_VALUES_FORMAT
- See Also:
- Constant Field Values
-
LATEST_POSTINGS_FORMAT
public static final java.lang.String LATEST_POSTINGS_FORMAT
- See Also:
- Constant Field Values
-
LATEST_CODEC
public static final java.lang.String LATEST_CODEC
- See Also:
- Constant Field Values
-
SOFT_DELETES_FIELD
public static final java.lang.String SOFT_DELETES_FIELD
- See Also:
- Constant Field Values
-
STANDARD_ANALYZER
public static final NamedAnalyzer STANDARD_ANALYZER
-
KEYWORD_ANALYZER
public static final NamedAnalyzer KEYWORD_ANALYZER
-
EMPTY_SCORE_DOCS
public static final org.apache.lucene.search.ScoreDoc[] EMPTY_SCORE_DOCS
-
EMPTY_TOP_DOCS
public static final org.apache.lucene.search.TopDocs EMPTY_TOP_DOCS
-
-
Method Detail
-
parseVersion
public static org.apache.lucene.util.Version parseVersion(@Nullable java.lang.String version, org.apache.lucene.util.Version defaultVersion, org.apache.logging.log4j.Logger logger)
-
readSegmentInfos
public static org.apache.lucene.index.SegmentInfos readSegmentInfos(org.apache.lucene.store.Directory directory) throws java.io.IOException
Reads the segments infos, failing if it fails to load- Throws:
java.io.IOException
-
files
public static java.lang.Iterable<java.lang.String> files(org.apache.lucene.index.SegmentInfos infos) throws java.io.IOException
Returns an iterable that allows to iterate over all files in this segments info- Throws:
java.io.IOException
-
getNumDocs
public static int getNumDocs(org.apache.lucene.index.SegmentInfos info)
Returns the number of documents in the index referenced by thisSegmentInfos
-
readSegmentInfos
public static org.apache.lucene.index.SegmentInfos readSegmentInfos(org.apache.lucene.index.IndexCommit commit) throws java.io.IOException
Reads the segments infos from the given commit, failing if it fails to load- Throws:
java.io.IOException
-
pruneUnreferencedFiles
public static org.apache.lucene.index.SegmentInfos pruneUnreferencedFiles(java.lang.String segmentsFileName, org.apache.lucene.store.Directory directory) throws java.io.IOException
This method removes all files from the given directory that are not referenced by the given segments file. This method will open an IndexWriter and relies on index file deleter to remove all unreferenced files. Segment files that are newer than the given segments file are removed forcefully to prevent problems with IndexWriter opening a potentially broken commit point / leftover. Note: this method will fail if there is another IndexWriter open on the given directory. This method will also acquire a write lock from the directory while pruning unused files. This method expects an existing index in the given directory that has the given segments file.- Throws:
java.io.IOException
-
getIndexCommit
public static org.apache.lucene.index.IndexCommit getIndexCommit(org.apache.lucene.index.SegmentInfos si, org.apache.lucene.store.Directory directory) throws java.io.IOException
Returns an index commit for the givenSegmentInfos
in the given directory.- Throws:
java.io.IOException
-
cleanLuceneIndex
public static void cleanLuceneIndex(org.apache.lucene.store.Directory directory) throws java.io.IOException
This method removes all lucene files from the given directory. It will first try to delete all commit points / segments files to ensure broken commits or corrupted indices will not be opened in the future. If any of the segment files can't be deleted this operation fails.- Throws:
java.io.IOException
-
checkSegmentInfoIntegrity
public static void checkSegmentInfoIntegrity(org.apache.lucene.store.Directory directory) throws java.io.IOException
- Throws:
java.io.IOException
-
exists
public static boolean exists(org.apache.lucene.search.IndexSearcher searcher, org.apache.lucene.search.Query query) throws java.io.IOException
Check whether there is one or more documents matching the provided query.- Throws:
java.io.IOException
-
readTopDocs
public static org.apache.lucene.search.TopDocs readTopDocs(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
readFieldDoc
public static org.apache.lucene.search.FieldDoc readFieldDoc(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
readSortValue
public static java.lang.Comparable readSortValue(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
readScoreDoc
public static org.apache.lucene.search.ScoreDoc readScoreDoc(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTopDocs
public static void writeTopDocs(StreamOutput out, org.apache.lucene.search.TopDocs topDocs) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSortValue
public static void writeSortValue(StreamOutput out, java.lang.Object field) throws java.io.IOException
- Throws:
java.io.IOException
-
writeFieldDoc
public static void writeFieldDoc(StreamOutput out, org.apache.lucene.search.FieldDoc fieldDoc) throws java.io.IOException
- Throws:
java.io.IOException
-
writeScoreDoc
public static void writeScoreDoc(StreamOutput out, org.apache.lucene.search.ScoreDoc scoreDoc) throws java.io.IOException
- Throws:
java.io.IOException
-
readSortType
public static org.apache.lucene.search.SortField.Type readSortType(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
readSortField
public static org.apache.lucene.search.SortField readSortField(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSortType
public static void writeSortType(StreamOutput out, org.apache.lucene.search.SortField.Type sortType) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSortField
public static void writeSortField(StreamOutput out, org.apache.lucene.search.SortField sortField) throws java.io.IOException
- Throws:
java.io.IOException
-
readExplanation
public static org.apache.lucene.search.Explanation readExplanation(StreamInput in) throws java.io.IOException
- Throws:
java.io.IOException
-
writeExplanation
public static void writeExplanation(StreamOutput out, org.apache.lucene.search.Explanation explanation) throws java.io.IOException
- Throws:
java.io.IOException
-
indexExists
public static boolean indexExists(org.apache.lucene.store.Directory directory) throws java.io.IOException
- Throws:
java.io.IOException
-
waitForIndex
public static boolean waitForIndex(org.apache.lucene.store.Directory directory, long timeLimitMillis) throws java.io.IOException
Wait for an index to exist for up totimeLimitMillis
. Returns true if the index eventually exists, false if not. Will retry the directory every second for at leasttimeLimitMillis
- Throws:
java.io.IOException
-
isCorruptionException
public static boolean isCorruptionException(java.lang.Throwable t)
Returnstrue
iff the given exception or one of it's causes is an instance ofCorruptIndexException
,IndexFormatTooOldException
, orIndexFormatTooNewException
otherwisefalse
.
-
parseVersionLenient
public static org.apache.lucene.util.Version parseVersionLenient(java.lang.String toParse, org.apache.lucene.util.Version defaultValue)
Parses the version string lenient and returns the default value if the given string is null or empty
-
segmentReader
public static org.apache.lucene.index.SegmentReader segmentReader(org.apache.lucene.index.LeafReader reader)
Tries to extract a segment reader from the given index reader. If no SegmentReader can be extracted anIllegalStateException
is thrown.
-
illegalScorer
public static org.apache.lucene.search.Scorer illegalScorer(java.lang.String message)
Return a Scorer that throws an ElasticsearchIllegalStateException on all operations with the given message.
-
asSequentialAccessBits
public static org.apache.lucene.util.Bits asSequentialAccessBits(int maxDoc, @Nullable org.apache.lucene.search.ScorerSupplier scorerSupplier) throws java.io.IOException
Given aScorerSupplier
, return aBits
instance that will match all documents contained in the set. Note that the returnedBits
instance MUST be consumed in order.- Throws:
java.io.IOException
-
wrapAllDocsLive
public static org.apache.lucene.index.DirectoryReader wrapAllDocsLive(org.apache.lucene.index.DirectoryReader in) throws java.io.IOException
Wraps a directory reader to make all documents live except those were rolled back or hard-deleted due to non-aborting exceptions during indexing. The wrapped reader can be used to query all documents.- Parameters:
in
- the input directory reader- Returns:
- the wrapped reader
- Throws:
java.io.IOException
-
newSoftDeletesField
public static org.apache.lucene.document.NumericDocValuesField newSoftDeletesField()
Returns a numeric docvalues which can be used to soft-delete documents.
-
emptyReader
public static org.apache.lucene.index.LeafReader emptyReader(int maxDoc)
Returns an empty leaf reader with the given max docs. The reader will be fully deleted.
-
scanSeqNosInReader
public static void scanSeqNosInReader(org.apache.lucene.index.DirectoryReader directoryReader, long fromSeqNo, long toSeqNo, java.util.function.LongConsumer onNewSeqNo) throws java.io.IOException
Scans sequence numbers (i.e.,SeqNoFieldMapper.NAME
) betweenfromSeqNo
(inclusive) andtoSeqNo
(inclusive) in the provided directory reader. This method invokes the callbackonNewSeqNo
whenever a sequence number value is found.- Parameters:
directoryReader
- the directory reader to scanfromSeqNo
- the lower bound of a range of seq_no to scan (inclusive)toSeqNo
- the upper bound of a range of seq_no to scan (inclusive)onNewSeqNo
- the callback to be called whenever a new valid sequence number is found- Throws:
java.io.IOException
-
-