Search in sources :

Example 86 with IndexWriterConfig

use of org.apache.lucene.index.IndexWriterConfig in project lucene-solr by apache.

the class SpellChecker method setSpellIndex.

/**
   * Use a different index as the spell checker index or re-open
   * the existing index if <code>spellIndex</code> is the same value
   * as given in the constructor.
   * @param spellIndexDir the spell directory to use
   * @throws AlreadyClosedException if the Spellchecker is already closed
   * @throws  IOException if spellchecker can not open the directory
   */
// TODO: we should make this final as it is called in the constructor
public void setSpellIndex(Directory spellIndexDir) throws IOException {
    // modifications to the directory should be synchronized 
    synchronized (modifyCurrentIndexLock) {
        ensureOpen();
        if (!DirectoryReader.indexExists(spellIndexDir)) {
            IndexWriter writer = new IndexWriter(spellIndexDir, new IndexWriterConfig(null));
            writer.close();
        }
        swapSearcher(spellIndexDir);
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 87 with IndexWriterConfig

use of org.apache.lucene.index.IndexWriterConfig in project lucene-solr by apache.

the class MockDirectoryWrapper method close.

@Override
public synchronized void close() throws IOException {
    if (isOpen) {
        isOpen = false;
    } else {
        // but call it again on our wrapped dir
        in.close();
        return;
    }
    boolean success = false;
    try {
        // files that we tried to delete, but couldn't because readers were open.
        // all that matters is that we tried! (they will eventually go away)
        //   still open when we tried to delete
        maybeYield();
        if (openFiles == null) {
            openFiles = new HashMap<>();
            openFilesDeleted = new HashSet<>();
        }
        if (openFiles.size() > 0) {
            // print the first one as it's very verbose otherwise
            Exception cause = null;
            Iterator<Exception> stacktraces = openFileHandles.values().iterator();
            if (stacktraces.hasNext()) {
                cause = stacktraces.next();
            }
            // super() does not throw IOException currently:
            throw new RuntimeException("MockDirectoryWrapper: cannot close: there are still " + openFiles.size() + " open files: " + openFiles, cause);
        }
        if (openLocks.size() > 0) {
            Exception cause = null;
            Iterator<RuntimeException> stacktraces = openLocks.values().iterator();
            if (stacktraces.hasNext()) {
                cause = stacktraces.next();
            }
            throw new RuntimeException("MockDirectoryWrapper: cannot close: there are still open locks: " + openLocks, cause);
        }
        randomIOExceptionRate = 0.0;
        randomIOExceptionRateOnOpen = 0.0;
        if ((getCheckIndexOnClose() || assertNoUnreferencedFilesOnClose) && DirectoryReader.indexExists(this)) {
            if (getCheckIndexOnClose()) {
                if (LuceneTestCase.VERBOSE) {
                    System.out.println("\nNOTE: MockDirectoryWrapper: now crush");
                }
                // corrupt any unsynced-files
                crash();
                if (LuceneTestCase.VERBOSE) {
                    System.out.println("\nNOTE: MockDirectoryWrapper: now run CheckIndex");
                }
                TestUtil.checkIndex(this, getCrossCheckTermVectorsOnClose(), true, null);
            }
            // TODO: factor this out / share w/ TestIW.assertNoUnreferencedFiles
            if (assertNoUnreferencedFilesOnClose) {
                System.out.println("MDW: now assert no unref'd files at close");
                // now look for unreferenced files: discount ones that we tried to delete but could not
                Set<String> allFiles = new HashSet<>(Arrays.asList(listAll()));
                String[] startFiles = allFiles.toArray(new String[0]);
                IndexWriterConfig iwc = new IndexWriterConfig(null);
                iwc.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE);
                // We must do this before opening writer otherwise writer will be angry if there are pending deletions:
                TestUtil.disableVirusChecker(in);
                new IndexWriter(in, iwc).rollback();
                String[] endFiles = in.listAll();
                Set<String> startSet = new TreeSet<>(Arrays.asList(startFiles));
                Set<String> endSet = new TreeSet<>(Arrays.asList(endFiles));
                startFiles = startSet.toArray(new String[0]);
                endFiles = endSet.toArray(new String[0]);
                if (!Arrays.equals(startFiles, endFiles)) {
                    List<String> removed = new ArrayList<>();
                    for (String fileName : startFiles) {
                        if (!endSet.contains(fileName)) {
                            removed.add(fileName);
                        }
                    }
                    List<String> added = new ArrayList<>();
                    for (String fileName : endFiles) {
                        if (!startSet.contains(fileName)) {
                            added.add(fileName);
                        }
                    }
                    String extras;
                    if (removed.size() != 0) {
                        extras = "\n\nThese files were removed: " + removed;
                    } else {
                        extras = "";
                    }
                    if (added.size() != 0) {
                        extras += "\n\nThese files were added (waaaaaaaaaat!): " + added;
                    }
                    throw new RuntimeException("unreferenced files: before delete:\n    " + Arrays.toString(startFiles) + "\n  after delete:\n    " + Arrays.toString(endFiles) + extras);
                }
                DirectoryReader ir1 = DirectoryReader.open(this);
                int numDocs1 = ir1.numDocs();
                ir1.close();
                new IndexWriter(this, new IndexWriterConfig(null)).close();
                DirectoryReader ir2 = DirectoryReader.open(this);
                int numDocs2 = ir2.numDocs();
                ir2.close();
                assert numDocs1 == numDocs2 : "numDocs changed after opening/closing IW: before=" + numDocs1 + " after=" + numDocs2;
            }
        }
        success = true;
    } finally {
        if (success) {
            IOUtils.close(in);
        } else {
            IOUtils.closeWhileHandlingException(in);
        }
    }
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IndexWriter(org.apache.lucene.index.IndexWriter) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 88 with IndexWriterConfig

use of org.apache.lucene.index.IndexWriterConfig in project lucene-solr by apache.

the class SimpleSortedSetFacetsExample method index.

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    Document doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));
    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));
    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));
    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));
    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
    indexWriter.addDocument(config.build(doc));
    indexWriter.close();
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) Document(org.apache.lucene.document.Document) SortedSetDocValuesFacetField(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 89 with IndexWriterConfig

use of org.apache.lucene.index.IndexWriterConfig in project lucene-solr by apache.

the class ExpressionAggregationFacetsExample method index.

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    // Writes facet ords to a separate directory from the main index
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    Document doc = new Document();
    doc.add(new TextField("c", "foo bar", Store.NO));
    doc.add(new NumericDocValuesField("popularity", 5L));
    doc.add(new FacetField("A", "B"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    doc = new Document();
    doc.add(new TextField("c", "foo foo bar", Store.NO));
    doc.add(new NumericDocValuesField("popularity", 3L));
    doc.add(new FacetField("A", "C"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    indexWriter.close();
    taxoWriter.close();
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 90 with IndexWriterConfig

use of org.apache.lucene.index.IndexWriterConfig in project lucene-solr by apache.

the class MultiCategoryListsFacetsExample method index.

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    // Writes facet ords to a separate directory from the main index
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    Document doc = new Document();
    doc.add(new FacetField("Author", "Bob"));
    doc.add(new FacetField("Publish Date", "2010", "10", "15"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    doc = new Document();
    doc.add(new FacetField("Author", "Lisa"));
    doc.add(new FacetField("Publish Date", "2010", "10", "20"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    doc = new Document();
    doc.add(new FacetField("Author", "Lisa"));
    doc.add(new FacetField("Publish Date", "2012", "1", "1"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    doc = new Document();
    doc.add(new FacetField("Author", "Susan"));
    doc.add(new FacetField("Publish Date", "2012", "1", "7"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    doc = new Document();
    doc.add(new FacetField("Author", "Frank"));
    doc.add(new FacetField("Publish Date", "1999", "5", "5"));
    indexWriter.addDocument(config.build(taxoWriter, doc));
    indexWriter.close();
    taxoWriter.close();
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) IndexWriter(org.apache.lucene.index.IndexWriter) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)513 IndexWriter (org.apache.lucene.index.IndexWriter)362 Document (org.apache.lucene.document.Document)311 Directory (org.apache.lucene.store.Directory)289 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)162 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexReader (org.apache.lucene.index.IndexReader)140 Term (org.apache.lucene.index.Term)116 IndexSearcher (org.apache.lucene.search.IndexSearcher)106 TextField (org.apache.lucene.document.TextField)93 DirectoryReader (org.apache.lucene.index.DirectoryReader)92 RAMDirectory (org.apache.lucene.store.RAMDirectory)89 IOException (java.io.IOException)88 BytesRef (org.apache.lucene.util.BytesRef)80 Field (org.apache.lucene.document.Field)78 Analyzer (org.apache.lucene.analysis.Analyzer)74 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)61 Test (org.junit.Test)61 StringField (org.apache.lucene.document.StringField)59 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)49