Search in sources :

Example 61 with IndexWriter

use of org.apache.lucene.index.IndexWriter in project languagetool by languagetool-org.

the class PatternRuleQueryBuilderTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    language = new English();
    directory = new RAMDirectory();
    /*File indexPath = new File("/tmp/lucene");
    if (indexPath.exists()) {
      FileUtils.deleteDirectory(indexPath);
    }
    directory = FSDirectory.open(indexPath);*/
    Analyzer analyzer = Indexer.getAnalyzer(language);
    IndexWriterConfig config = Indexer.getIndexWriterConfig(analyzer);
    try (IndexWriter writer = new IndexWriter(directory, config)) {
        addDocument(writer, "How do you thin about this wonderful idea?");
        addDocument(writer, "The are several grammar checkers for English, E.G. LanguageTool 123.");
    }
    reader = DirectoryReader.open(directory);
    searcher = newSearcher(reader);
}
Also used : English(org.languagetool.language.English) IndexWriter(org.apache.lucene.index.IndexWriter) Analyzer(org.apache.lucene.analysis.Analyzer) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 62 with IndexWriter

use of org.apache.lucene.index.IndexWriter in project neo4j by neo4j.

the class LuceneSchemaIndex method markAsOnline.

/**
     * Marks index as online by including "status" -> "online" map into commit metadata of the first partition.
     *
     * @throws IOException
     */
public void markAsOnline() throws IOException {
    ensureOpen();
    AbstractIndexPartition partition = getFirstPartition(getPartitions());
    IndexWriter indexWriter = partition.getIndexWriter();
    indexWriter.setCommitData(ONLINE_COMMIT_USER_DATA);
    flush(false);
}
Also used : LuceneIndexWriter(org.neo4j.kernel.api.impl.schema.writer.LuceneIndexWriter) PartitionedIndexWriter(org.neo4j.kernel.api.impl.schema.writer.PartitionedIndexWriter) IndexWriter(org.apache.lucene.index.IndexWriter) AbstractIndexPartition(org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition)

Example 63 with IndexWriter

use of org.apache.lucene.index.IndexWriter in project neo4j by neo4j.

the class WritableIndexReferenceFactory method refresh.

/**
     * If nothing has changed underneath (since the searcher was last created
     * or refreshed) {@code searcher} is returned. But if something has changed a
     * refreshed searcher is returned. It makes use if the
     * {@link DirectoryReader#openIfChanged(DirectoryReader, IndexWriter, boolean)} which faster than opening an index
     * from
     * scratch.
     *
     * @param indexReference the {@link IndexReference} to refresh.
     * @return a refreshed version of the searcher or, if nothing has changed,
     *         {@code null}.
     * @throws RuntimeException if there's a problem with the index.
     */
@Override
IndexReference refresh(IndexReference indexReference) {
    try {
        DirectoryReader reader = (DirectoryReader) indexReference.getSearcher().getIndexReader();
        IndexWriter writer = indexReference.getWriter();
        IndexReader reopened = DirectoryReader.openIfChanged(reader, writer);
        if (reopened != null) {
            IndexSearcher newSearcher = newIndexSearcher(indexReference.getIdentifier(), reopened);
            indexReference.detachOrClose();
            return new WritableIndexReference(indexReference.getIdentifier(), newSearcher, writer);
        }
        return indexReference;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Example 64 with IndexWriter

use of org.apache.lucene.index.IndexWriter in project neo4j by neo4j.

the class AbstractLuceneIndex method flush.

/**
     * Commits all index partitions.
     *
     * @param merge also merge all segments together. This should be done before reading term frequencies.
     * @throws IOException on Lucene I/O error.
     */
public void flush(boolean merge) throws IOException {
    List<AbstractIndexPartition> partitions = getPartitions();
    for (AbstractIndexPartition partition : partitions) {
        IndexWriter writer = partition.getIndexWriter();
        writer.commit();
        if (merge) {
            writer.forceMerge(1);
        }
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) AbstractIndexPartition(org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition)

Example 65 with IndexWriter

use of org.apache.lucene.index.IndexWriter in project neo4j by neo4j.

the class LuceneDataSourceTest method testShouldReturnIndexWriterFromLRUCache.

@Test
public void testShouldReturnIndexWriterFromLRUCache() throws Throwable {
    Config config = Config.embeddedDefaults();
    dataSource = life.add(new LuceneDataSource(directory.graphDbDir(), config, indexStore, fileSystemRule.get()));
    IndexIdentifier identifier = identifier("foo");
    IndexWriter writer = dataSource.getIndexSearcher(identifier).getWriter();
    assertSame(writer, dataSource.getIndexSearcher(identifier).getWriter());
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) Config(org.neo4j.kernel.configuration.Config) Test(org.junit.Test)

Aggregations

IndexWriter (org.apache.lucene.index.IndexWriter)529 Document (org.apache.lucene.document.Document)311 Directory (org.apache.lucene.store.Directory)306 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)293 IndexReader (org.apache.lucene.index.IndexReader)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)136 DirectoryReader (org.apache.lucene.index.DirectoryReader)127 Term (org.apache.lucene.index.Term)125 IndexSearcher (org.apache.lucene.search.IndexSearcher)110 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)107 TextField (org.apache.lucene.document.TextField)104 RAMDirectory (org.apache.lucene.store.RAMDirectory)88 IOException (java.io.IOException)86 Field (org.apache.lucene.document.Field)86 TermQuery (org.apache.lucene.search.TermQuery)56 StringField (org.apache.lucene.document.StringField)52 BytesRef (org.apache.lucene.util.BytesRef)52 FieldType (org.apache.lucene.document.FieldType)50 Test (org.junit.Test)49 Query (org.apache.lucene.search.Query)45