Search in sources :

Example 46 with StringField

use of org.apache.lucene.document.StringField in project lucene-solr by apache.

the class TestIndexWriter method testMassiveField.

@Ignore("requires running tests with biggish heap")
public void testMassiveField() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    final IndexWriter w = new IndexWriter(dir, iwc);
    StringBuilder b = new StringBuilder();
    while (b.length() <= IndexWriter.MAX_STORED_STRING_LENGTH) {
        b.append("x ");
    }
    final Document doc = new Document();
    //doc.add(new TextField("big", b.toString(), Field.Store.YES));
    doc.add(new StoredField("big", b.toString()));
    Exception e = expectThrows(IllegalArgumentException.class, () -> {
        w.addDocument(doc);
    });
    assertEquals("stored field \"big\" is too large (" + b.length() + " characters) to store", e.getMessage());
    // make sure writer is still usable:
    Document doc2 = new Document();
    doc2.add(new StringField("id", "foo", Field.Store.YES));
    w.addDocument(doc2);
    DirectoryReader r = DirectoryReader.open(w);
    assertEquals(1, r.numDocs());
    r.close();
    w.close();
    dir.close();
}
Also used : StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) NoSuchFileException(java.nio.file.NoSuchFileException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) FileNotFoundException(java.io.FileNotFoundException) ThreadInterruptedException(org.apache.lucene.util.ThreadInterruptedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Ignore(org.junit.Ignore)

Example 47 with StringField

use of org.apache.lucene.document.StringField in project lucene-solr by apache.

the class TestIndexWriter method testIterableFieldThrowsException.

public void testIterableFieldThrowsException() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    int iters = atLeast(100);
    int docCount = 0;
    int docId = 0;
    Set<String> liveIds = new HashSet<>();
    for (int i = 0; i < iters; i++) {
        int numDocs = atLeast(4);
        for (int j = 0; j < numDocs; j++) {
            String id = Integer.toString(docId++);
            final List<IndexableField> fields = new ArrayList<>();
            fields.add(new StringField("id", id, Field.Store.YES));
            fields.add(new StringField("foo", TestUtil.randomSimpleString(random()), Field.Store.NO));
            docId++;
            boolean success = false;
            try {
                w.addDocument(new RandomFailingIterable<IndexableField>(fields, random()));
                success = true;
            } catch (RuntimeException e) {
                assertEquals("boom", e.getMessage());
            } finally {
                if (success) {
                    docCount++;
                    liveIds.add(id);
                }
            }
        }
    }
    DirectoryReader reader = w.getReader();
    assertEquals(docCount, reader.numDocs());
    List<LeafReaderContext> leaves = reader.leaves();
    for (LeafReaderContext leafReaderContext : leaves) {
        LeafReader ar = leafReaderContext.reader();
        Bits liveDocs = ar.getLiveDocs();
        int maxDoc = ar.maxDoc();
        for (int i = 0; i < maxDoc; i++) {
            if (liveDocs == null || liveDocs.get(i)) {
                assertTrue(liveIds.remove(ar.document(i).get("id")));
            }
        }
    }
    assertTrue(liveIds.isEmpty());
    w.close();
    IOUtils.close(reader, dir);
}
Also used : ArrayList(java.util.ArrayList) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Bits(org.apache.lucene.util.Bits) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) HashSet(java.util.HashSet)

Example 48 with StringField

use of org.apache.lucene.document.StringField in project lucene-solr by apache.

the class TestIndexWriter method testNullAnalyzer.

public void testNullAnalyzer() throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig iwConf = newIndexWriterConfig(null);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConf);
    // add 3 good docs
    for (int i = 0; i < 3; i++) {
        Document doc = new Document();
        doc.add(new StringField("id", Integer.toString(i), Field.Store.NO));
        iw.addDocument(doc);
    }
    // add broken doc
    expectThrows(NullPointerException.class, () -> {
        Document broke = new Document();
        broke.add(newTextField("test", "broken", Field.Store.NO));
        iw.addDocument(broke);
    });
    // ensure good docs are still ok
    IndexReader ir = iw.getReader();
    assertEquals(3, ir.numDocs());
    ir.close();
    iw.close();
    dir.close();
}
Also used : StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 49 with StringField

use of org.apache.lucene.document.StringField in project lucene-solr by apache.

the class TestIndexWriter method testNullDocuments.

public void testNullDocuments() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    // add 3 good docs
    for (int i = 0; i < 3; i++) {
        Document doc = new Document();
        doc.add(new StringField("id", Integer.toString(i), Field.Store.NO));
        iw.addDocument(doc);
    }
    // add broken doc block
    expectThrows(NullPointerException.class, () -> {
        iw.addDocuments(null);
    });
    // ensure good docs are still ok
    IndexReader ir = iw.getReader();
    assertEquals(3, ir.numDocs());
    ir.close();
    iw.close();
    dir.close();
}
Also used : StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 50 with StringField

use of org.apache.lucene.document.StringField in project lucene-solr by apache.

the class TestIndexWriter method testNullDocument.

public void testNullDocument() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    // add 3 good docs
    for (int i = 0; i < 3; i++) {
        Document doc = new Document();
        doc.add(new StringField("id", Integer.toString(i), Field.Store.NO));
        iw.addDocument(doc);
    }
    // add broken doc
    expectThrows(NullPointerException.class, () -> {
        iw.addDocument(null);
    });
    // ensure good docs are still ok
    IndexReader ir = iw.getReader();
    assertEquals(3, ir.numDocs());
    ir.close();
    iw.close();
    dir.close();
}
Also used : StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Aggregations

StringField (org.apache.lucene.document.StringField)323 Document (org.apache.lucene.document.Document)302 Directory (org.apache.lucene.store.Directory)227 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)129 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)94 Term (org.apache.lucene.index.Term)90 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)82 BytesRef (org.apache.lucene.util.BytesRef)73 IndexSearcher (org.apache.lucene.search.IndexSearcher)57 DirectoryReader (org.apache.lucene.index.DirectoryReader)56 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)55 ArrayList (java.util.ArrayList)54 TextField (org.apache.lucene.document.TextField)54 IndexReader (org.apache.lucene.index.IndexReader)51 Field (org.apache.lucene.document.Field)50 TermQuery (org.apache.lucene.search.TermQuery)50 IndexWriter (org.apache.lucene.index.IndexWriter)45 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)43 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)43 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)40