Class 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 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 IOException
        Constructor 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 IOException
        Creates 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

      • createAnalyzer

        protected org.apache.lucene.analysis.Analyzer createAnalyzer​(Properties parameters)
                                                              throws Exception
        Throws:
        Exception
      • createQueryAnalyzer

        protected org.apache.lucene.analysis.Analyzer createQueryAnalyzer​(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)
      • 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
      • getIndexWriter

        public org.apache.lucene.index.IndexWriter getIndexWriter()
                                                           throws IOException
        Throws:
        IOException
      • getDocument

        public org.apache.lucene.document.Document getDocument​(org.eclipse.rdf4j.model.Resource subject,
                                                               org.eclipse.rdf4j.model.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​(org.eclipse.rdf4j.model.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-context
        document - 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)
      • commit

        public void commit()
                    throws IOException
        Commits 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
      • query

        protected Iterable<? extends DocumentScore> query​(org.eclipse.rdf4j.model.Resource subject,
                                                          QuerySpec spec)
                                                   throws org.eclipse.rdf4j.query.MalformedQueryException,
                                                          IOException
        Parse the passed query.
        Specified by:
        query in class AbstractSearchIndex
        Parameters:
        subject - subject
        spec - spec
        Returns:
        the parsed query
        Throws:
        org.eclipse.rdf4j.query.MalformedQueryException - when the parsing breaks
        IOException
      • geoQuery

        protected Iterable<? extends DocumentDistance> geoQuery​(org.eclipse.rdf4j.model.IRI geoProperty,
                                                                org.locationtech.spatial4j.shape.Point p,
                                                                org.eclipse.rdf4j.model.IRI units,
                                                                double distance,
                                                                String distanceVar,
                                                                org.eclipse.rdf4j.query.algebra.Var contextVar)
                                                         throws org.eclipse.rdf4j.query.MalformedQueryException,
                                                                IOException
        Specified by:
        geoQuery in class AbstractSearchIndex
        Throws:
        org.eclipse.rdf4j.query.MalformedQueryException
        IOException
      • 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 return
        fieldsToLoad -
        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​(org.eclipse.rdf4j.model.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 IOException
        Evaluates the given query and returns the results as a TopDocs instance.
        Parameters:
        query -
        Returns:
        top documents
        Throws:
        IOException
      • clearContexts

        public void clearContexts​(org.eclipse.rdf4j.model.Resource... contexts)
                           throws IOException
        Parameters:
        contexts -
        Throws:
        IOException