Class LuceneIndex
- java.lang.Object
-
- org.eclipse.rdf4j.sail.lucene.AbstractSearchIndex
-
- org.eclipse.rdf4j.sail.lucene.AbstractLuceneIndex
-
- org.eclipse.rdf4j.sail.lucene.impl.LuceneIndex
-
- All Implemented Interfaces:
SearchIndex
- Direct Known Subclasses:
LuceneIndexNIOFS
public class LuceneIndex extends AbstractLuceneIndex
A LuceneIndex is a one-stop-shop abstraction of a Lucene index. It takes care of proper synchronization of IndexReaders, IndexWriters and IndexSearchers in a way that is suitable for a LuceneSail.- See Also:
LuceneSail
-
-
Field Summary
Fields Modifier and Type Field Description protected ReaderMonitorcurrentMonitorThis holds IndexReader and IndexSearcher.-
Fields inherited from class org.eclipse.rdf4j.sail.lucene.AbstractLuceneIndex
oldmonitors
-
Fields inherited from class org.eclipse.rdf4j.sail.lucene.AbstractSearchIndex
maxDocs, wktFields
-
-
Constructor Summary
Constructors Constructor Description LuceneIndex()LuceneIndex(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzer)Constructor for keeping backwards compatibility.LuceneIndex(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzer, org.apache.lucene.search.similarities.Similarity similarity)Creates a new LuceneIndex.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddContextField(String context, org.apache.lucene.document.Document document)Add the "context" value to the docprotected voidaddDocument(SearchDocument doc)static voidaddIDField(String id, org.apache.lucene.document.Document document)Stores and indexes an ID in a Document.static voidaddPredicateField(String predicate, String text, org.apache.lucene.document.Document document)static voidaddResourceField(String resourceId, org.apache.lucene.document.Document document)Stores and indexes the resource ID in a Document.static voidaddStoredOnlyPredicateField(String predicate, String text, org.apache.lucene.document.Document document)static voidaddTextField(String text, org.apache.lucene.document.Document document)voidbegin()Begins a transaction.voidclear()Clears the indexes.voidclearContexts(Resource... contexts)This should be called from within a begin-commit-rollback block.voidcommit()Commits any changes done to the LuceneIndex since the last commit.protected SearchDocumentcopyDocument(SearchDocument doc)protected org.apache.lucene.analysis.AnalyzercreateAnalyzer(Properties parameters)protected org.apache.lucene.store.DirectorycreateDirectory(Properties parameters)protected org.apache.lucene.search.similarities.SimilaritycreateSimilarity(Properties parameters)protected com.google.common.base.Function<String,? extends org.apache.lucene.spatial.SpatialStrategy>createSpatialStrategyMapper(Map<String,String> parameters)protected voiddeleteDocument(SearchDocument doc)protected Iterable<? extends DocumentDistance>geoQuery(IRI geoProperty, org.locationtech.spatial4j.shape.Point p, IRI units, double distance, String distanceVar, Var contextVar)protected Iterable<? extends DocumentResult>geoRelationQuery(String relation, IRI geoProperty, String wkt, Var contextVar)org.apache.lucene.analysis.AnalyzergetAnalyzer()ReaderMonitorgetCurrentMonitor()Current monitor holds instance of IndexReader and IndexSearcher It is used to keep track of readersorg.apache.lucene.store.DirectorygetDirectory()org.apache.lucene.document.DocumentgetDocument(int docId, Set<String> fieldsToLoad)Returns the lucene hit with the given id of the respective lucene queryprotected SearchDocumentgetDocument(String id)org.apache.lucene.document.DocumentgetDocument(Resource subject, Resource context)Returns a Document representing the specified Resource and Context combination, or null when no such Document exists yet.protected Iterable<? extends SearchDocument>getDocuments(String resourceId)List<org.apache.lucene.document.Document>getDocuments(Resource subject)Returns a list of Documents representing the specified Resource (empty when no such Document exists yet).Each document represent a set of statements with the specified Resource as a subject, which are stored in a specific contextorg.apache.lucene.index.IndexReadergetIndexReader()org.apache.lucene.search.IndexSearchergetIndexSearcher()org.apache.lucene.index.IndexWritergetIndexWriter()StringgetSnippet(String fieldName, String text, org.apache.lucene.search.highlight.Highlighter highlighter)protected org.locationtech.spatial4j.context.SpatialContextgetSpatialContext(String property)com.google.common.base.Function<? super String,? extends org.apache.lucene.spatial.SpatialStrategy>getSpatialStrategyMapper()voidinitialize(Properties parameters)protected BulkUpdaternewBulkUpdate()protected SearchDocumentnewDocument(String id, String resourceId, String context)protected Iterable<? extends DocumentScore>query(Resource subject, String query, IRI propertyURI, boolean highlight)Parse the passed query.voidrollback()org.apache.lucene.search.TopDocssearch(org.apache.lucene.search.Query query)Evaluates the given query and returns the results as a TopDocs instance.org.apache.lucene.search.TopDocssearch(Resource resource, org.apache.lucene.search.Query query)Evaluates the given query only for the given resource.voidshutDown()protected voidupdateDocument(SearchDocument doc)-
Methods inherited from class org.eclipse.rdf4j.sail.lucene.AbstractLuceneIndex
getOldMonitors
-
Methods inherited from class org.eclipse.rdf4j.sail.lucene.AbstractSearchIndex
accept, addDocuments, addRemoveStatements, addStatement, evaluate, getResource, isGeoField, parseLuceneQueryShape, parseQueryPoint, parseQueryShape, removeStatement
-
-
-
-
Field Detail
-
currentMonitor
protected volatile ReaderMonitor currentMonitor
This holds IndexReader and IndexSearcher.
-
-
Constructor Detail
-
LuceneIndex
public LuceneIndex()
-
LuceneIndex
public LuceneIndex(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzer) throws IOExceptionConstructor for keeping backwards compatibility.- Parameters:
directory-analyzer-- Throws:
IOException
-
LuceneIndex
public LuceneIndex(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzer, org.apache.lucene.search.similarities.Similarity similarity) throws IOExceptionCreates a new LuceneIndex.- Parameters:
directory- The Directory in which an index can be found and/or in which index files are written.analyzer- The Analyzer that will be used for tokenizing strings to index and queries.similarity- The Similarity that will be used for scoring.- Throws:
IOException- When the Directory could not be unlocked.
-
-
Method Detail
-
initialize
public void initialize(Properties parameters) throws Exception
- Specified by:
initializein interfaceSearchIndex- Overrides:
initializein classAbstractSearchIndex- Throws:
Exception
-
createDirectory
protected org.apache.lucene.store.Directory createDirectory(Properties parameters) throws IOException
- Throws:
IOException
-
createAnalyzer
protected org.apache.lucene.analysis.Analyzer createAnalyzer(Properties parameters) throws Exception
- Throws:
Exception
-
createSimilarity
protected org.apache.lucene.search.similarities.Similarity createSimilarity(Properties parameters) throws Exception
- Throws:
Exception
-
createSpatialStrategyMapper
protected com.google.common.base.Function<String,? extends org.apache.lucene.spatial.SpatialStrategy> createSpatialStrategyMapper(Map<String,String> parameters)
-
getSpatialContext
protected org.locationtech.spatial4j.context.SpatialContext getSpatialContext(String property)
- Specified by:
getSpatialContextin classAbstractSearchIndex
-
getDirectory
public org.apache.lucene.store.Directory getDirectory()
-
getAnalyzer
public org.apache.lucene.analysis.Analyzer getAnalyzer()
-
getSpatialStrategyMapper
public com.google.common.base.Function<? super String,? extends org.apache.lucene.spatial.SpatialStrategy> getSpatialStrategyMapper()
-
getIndexReader
public org.apache.lucene.index.IndexReader getIndexReader() throws IOException- Throws:
IOException
-
getIndexSearcher
public org.apache.lucene.search.IndexSearcher getIndexSearcher() throws IOException- Throws:
IOException
-
getCurrentMonitor
public ReaderMonitor getCurrentMonitor()
Current monitor holds instance of IndexReader and IndexSearcher It is used to keep track of readers- Specified by:
getCurrentMonitorin classAbstractLuceneIndex
-
getIndexWriter
public org.apache.lucene.index.IndexWriter getIndexWriter() throws IOException- Throws:
IOException
-
shutDown
public void shutDown() throws IOException- Throws:
IOException
-
getDocument
protected SearchDocument getDocument(String id) throws IOException
- Specified by:
getDocumentin classAbstractSearchIndex- Throws:
IOException
-
getDocuments
protected Iterable<? extends SearchDocument> getDocuments(String resourceId) throws IOException
- Specified by:
getDocumentsin classAbstractSearchIndex- Throws:
IOException
-
newDocument
protected SearchDocument newDocument(String id, String resourceId, String context)
- Specified by:
newDocumentin classAbstractSearchIndex
-
copyDocument
protected SearchDocument copyDocument(SearchDocument doc)
- Specified by:
copyDocumentin classAbstractSearchIndex
-
addDocument
protected void addDocument(SearchDocument doc) throws IOException
- Specified by:
addDocumentin classAbstractSearchIndex- Throws:
IOException
-
updateDocument
protected void updateDocument(SearchDocument doc) throws IOException
- Specified by:
updateDocumentin classAbstractSearchIndex- Throws:
IOException
-
deleteDocument
protected void deleteDocument(SearchDocument doc) throws IOException
- Specified by:
deleteDocumentin classAbstractSearchIndex- Throws:
IOException
-
newBulkUpdate
protected BulkUpdater newBulkUpdate()
- Specified by:
newBulkUpdatein classAbstractSearchIndex
-
getDocument
public org.apache.lucene.document.Document getDocument(Resource subject, Resource context) throws IOException
Returns a Document representing the specified Resource and Context combination, or null when no such Document exists yet.- Parameters:
subject-context-- Returns:
- document
- Throws:
IOException
-
getDocuments
public List<org.apache.lucene.document.Document> getDocuments(Resource subject) throws IOException
Returns a list of Documents representing the specified Resource (empty when no such Document exists yet).Each document represent a set of statements with the specified Resource as a subject, which are stored in a specific context- Parameters:
subject-- Returns:
- list of documents
- Throws:
IOException
-
addIDField
public static void addIDField(String id, org.apache.lucene.document.Document document)
Stores and indexes an ID in a Document.- Parameters:
id-document-
-
addContextField
public static void addContextField(String context, org.apache.lucene.document.Document document)
Add the "context" value to the doc- Parameters:
context- the context or null, if null-contextdocument- the document
-
addResourceField
public static void addResourceField(String resourceId, org.apache.lucene.document.Document document)
Stores and indexes the resource ID in a Document.- Parameters:
resourceId-document-
-
addPredicateField
public static void addPredicateField(String predicate, String text, org.apache.lucene.document.Document document)
-
addStoredOnlyPredicateField
public static void addStoredOnlyPredicateField(String predicate, String text, org.apache.lucene.document.Document document)
-
addTextField
public static void addTextField(String text, org.apache.lucene.document.Document document)
-
begin
public void begin() throws IOExceptionDescription copied from interface:SearchIndexBegins a transaction.- Throws:
IOException
-
commit
public void commit() throws IOExceptionCommits any changes done to the LuceneIndex since the last commit. The semantics is synchronous to SailConnection.commit(), i.e. the LuceneIndex should be committed/rolled back whenever the LuceneSailConnection is committed/rolled back.- Throws:
IOException
-
rollback
public void rollback() throws IOException- Throws:
IOException
-
query
protected Iterable<? extends DocumentScore> query(Resource subject, String query, IRI propertyURI, boolean highlight) throws MalformedQueryException, IOException
Parse the passed query.- Specified by:
queryin classAbstractSearchIndex- Parameters:
subject-query- stringpropertyURI-highlight-- Returns:
- the parsed query
- Throws:
MalformedQueryException- when the parsing breaksIOException
-
geoQuery
protected Iterable<? extends DocumentDistance> geoQuery(IRI geoProperty, org.locationtech.spatial4j.shape.Point p, IRI units, double distance, String distanceVar, Var contextVar) throws MalformedQueryException, IOException
- Specified by:
geoQueryin classAbstractSearchIndex- Throws:
MalformedQueryExceptionIOException
-
geoRelationQuery
protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, IRI geoProperty, String wkt, Var contextVar) throws MalformedQueryException, IOException
- Specified by:
geoRelationQueryin classAbstractSearchIndex- Throws:
MalformedQueryExceptionIOException
-
getDocument
public org.apache.lucene.document.Document getDocument(int docId, Set<String> fieldsToLoad)Returns the lucene hit with the given id of the respective lucene query- Parameters:
docId- the id of the document to returnfieldsToLoad-- Returns:
- the requested hit, or null if it fails
-
getSnippet
public String getSnippet(String fieldName, String text, org.apache.lucene.search.highlight.Highlighter highlighter)
-
search
public org.apache.lucene.search.TopDocs search(Resource resource, org.apache.lucene.search.Query query) throws IOException
Evaluates the given query only for the given resource.- Parameters:
resource-query-- Returns:
- top documents
- Throws:
IOException
-
search
public org.apache.lucene.search.TopDocs search(org.apache.lucene.search.Query query) throws IOExceptionEvaluates the given query and returns the results as a TopDocs instance.- Parameters:
query-- Returns:
- top documents
- Throws:
IOException
-
clearContexts
public void clearContexts(Resource... contexts) throws IOException
Description copied from interface:SearchIndexThis should be called from within a begin-commit-rollback block.- Parameters:
contexts-- Throws:
IOException
-
clear
public void clear() throws IOExceptionDescription copied from interface:SearchIndexClears the indexes.- Throws:
IOException
-
-