Package org.eclipse.rdf4j.sail.lucene.impl

## 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 ReaderMonitor` | `currentMonitor` | This 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 void` | `addContextField​(String context, org.apache.lucene.document.Document document)` | Add the "context" value to the doc
    `protected void` | `addDocument​(SearchDocument doc)` |
    `static void` | `addIDField​(String id, org.apache.lucene.document.Document document)` | Stores and indexes an ID in a Document.
    `static void` | `addPredicateField​(String predicate, String text, org.apache.lucene.document.Document document)` |
    `static void` | `addResourceField​(String resourceId, org.apache.lucene.document.Document document)` | Stores and indexes the resource ID in a Document.
    `static void` | `addStoredOnlyPredicateField​(String predicate, String text, org.apache.lucene.document.Document document)` |
    `static void` | `addTextField​(String text, org.apache.lucene.document.Document document)` |
    `void` | `begin()` | Begins a transaction.
    `void` | `clear()` | Clears the indexes.
    `void` | `clearContexts​(Resource... contexts)` | This should be called from within a begin-commit-rollback block.
    `void` | `commit()` | Commits any changes done to the LuceneIndex since the last commit.
    `protected SearchDocument` | `copyDocument​(SearchDocument doc)` |
    `protected org.apache.lucene.analysis.Analyzer` | `createAnalyzer​(Properties parameters)` |
    `protected org.apache.lucene.store.Directory` | `createDirectory​(Properties parameters)` |
    `protected org.apache.lucene.search.similarities.Similarity` | `createSimilarity​(Properties parameters)` |
    `protected com.google.common.base.Function<String,​? extends org.apache.lucene.spatial.SpatialStrategy>` | `createSpatialStrategyMapper​(Map<String,​String> parameters)` |
    `protected void` | `deleteDocument​(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.Analyzer` | `getAnalyzer()` |
    `ReaderMonitor` | `getCurrentMonitor()` | Current monitor holds instance of IndexReader and IndexSearcher It is used to keep track of readers
    `org.apache.lucene.store.Directory` | `getDirectory()` |
    `org.apache.lucene.document.Document` | `getDocument​(int docId, Set<String> fieldsToLoad)` | Returns the lucene hit with the given id of the respective lucene query
    `protected SearchDocument` | `getDocument​(String id)` |
    `org.apache.lucene.document.Document` | `getDocument​(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 context
    `org.apache.lucene.index.IndexReader` | `getIndexReader()` |
    `org.apache.lucene.search.IndexSearcher` | `getIndexSearcher()` |
    `org.apache.lucene.index.IndexWriter` | `getIndexWriter()` |
    `String` | `getSnippet​(String fieldName, String text, org.apache.lucene.search.highlight.Highlighter highlighter)` |
    `protected org.locationtech.spatial4j.context.SpatialContext` | `getSpatialContext​(String property)` |
    `com.google.common.base.Function<? super String,​? extends org.apache.lucene.spatial.SpatialStrategy>` | `getSpatialStrategyMapper()` |
    `void` | `initialize​(Properties parameters)` |
    `protected BulkUpdater` | `newBulkUpdate()` |
    `protected SearchDocument` | `newDocument​(String id, String resourceId, String context)` |
    `protected Iterable<? extends DocumentScore>` | `query​(Resource subject, String query, IRI propertyURI, boolean highlight)` | Parse the passed query.
    `void` | `rollback()` |
    `org.apache.lucene.search.TopDocs` | `search​(org.apache.lucene.search.Query query)` | Evaluates the given query and returns the results as a TopDocs instance.
    `org.apache.lucene.search.TopDocs` | `search​(Resource resource, org.apache.lucene.search.Query query)` | Evaluates the given query only for the given resource.
    `void` | `shutDown()` |
    `protected void` | `updateDocument​(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, getIndexedTypeMapping, getResource, isGeoField, isIndexedTypeStatement, isTypeFilteringEnabled, isTypeStatement, parseLuceneQueryShape, parseQueryPoint, parseQueryShape, removeStatement`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 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

    - #### initialize

          public void initialize​(Properties parameters)
                          throws Exception

      Specified by:
        `initialize` in interface `SearchIndex`
      Overrides:
        `initialize` in class `AbstractSearchIndex`
      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:
        `getSpatialContext` in class `AbstractSearchIndex`

    - #### 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:
        `getCurrentMonitor` in class `AbstractLuceneIndex`

    - #### 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:
        `getDocument` in class `AbstractSearchIndex`
      Throws:
        `IOException`

    - #### getDocuments

          protected Iterable<? extends SearchDocument> getDocuments​(String resourceId)
                                                             throws IOException

      Specified by:
        `getDocuments` in class `AbstractSearchIndex`
      Throws:
        `IOException`

    - #### newDocument

          protected SearchDocument newDocument​(String id,
                                               String resourceId,
                                               String context)

      Specified by:
        `newDocument` in class `AbstractSearchIndex`

    - #### copyDocument

          protected SearchDocument copyDocument​(SearchDocument doc)

      Specified by:
        `copyDocument` in class `AbstractSearchIndex`

    - #### addDocument

          protected void addDocument​(SearchDocument doc)
                              throws IOException

      Specified by:
        `addDocument` in class `AbstractSearchIndex`
      Throws:
        `IOException`

    - #### updateDocument

          protected void updateDocument​(SearchDocument doc)
                                 throws IOException

      Specified by:
        `updateDocument` in class `AbstractSearchIndex`
      Throws:
        `IOException`

    - #### deleteDocument

          protected void deleteDocument​(SearchDocument doc)
                                 throws IOException

      Specified by:
        `deleteDocument` in class `AbstractSearchIndex`
      Throws:
        `IOException`

    - #### newBulkUpdate

          protected BulkUpdater newBulkUpdate()

      Specified by:
        `newBulkUpdate` in class `AbstractSearchIndex`

    - #### 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-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)

    - #### begin

          public void begin()
                     throws IOException

      Description copied from interface: `SearchIndex`
      Begins a transaction.
      Throws:
        `IOException`

    - #### 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`

    - #### 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:
        `query` in class `AbstractSearchIndex`
      Parameters:
        `subject` -
        `query` - string
        `propertyURI` -
        `highlight` -
      Returns:
        the parsed query
      Throws:
        `MalformedQueryException` - when the parsing breaks
        `IOException`

    - #### 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:
        `geoQuery` in class `AbstractSearchIndex`
      Throws:
        `MalformedQueryException`
        `IOException`

    - #### geoRelationQuery

          protected Iterable<? extends DocumentResult> geoRelationQuery​(String relation,
                                                                        IRI geoProperty,
                                                                        String wkt,
                                                                        Var contextVar)
                                                                 throws MalformedQueryException,
                                                                        IOException

      Specified by:
        `geoRelationQuery` in class `AbstractSearchIndex`
      Throws:
        `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​(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​(Resource... contexts)
                             throws IOException

      Description copied from interface: `SearchIndex`
      This should be called from within a begin-commit-rollback block.
      Parameters:
        `contexts` -
      Throws:
        `IOException`

    - #### clear

          public void clear()
                     throws IOException

      Description copied from interface: `SearchIndex`
      Clears the indexes.
      Throws:
        `IOException`