Search in sources :

Example 91 with StoredField

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

the class TestSuggestField method testSuggestOnMostlyDeletedDocuments.

@Test
public void testSuggestOnMostlyDeletedDocuments() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    // using IndexWriter instead of RandomIndexWriter
    IndexWriter iw = new IndexWriter(dir, iwcWithSuggestField(analyzer, "suggest_field"));
    int num = Math.min(1000, atLeast(10));
    for (int i = 1; i <= num; i++) {
        Document document = new Document();
        document.add(new SuggestField("suggest_field", "abc_" + i, i));
        document.add(new StoredField("weight_fld", i));
        document.add(new IntPoint("weight_fld", i));
        iw.addDocument(document);
        if (usually()) {
            iw.commit();
        }
    }
    iw.deleteDocuments(IntPoint.newRangeQuery("weight_fld", 2, Integer.MAX_VALUE));
    DirectoryReader reader = DirectoryReader.open(iw);
    SuggestIndexSearcher indexSearcher = new SuggestIndexSearcher(reader);
    PrefixCompletionQuery query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"));
    TopSuggestDocs suggest = indexSearcher.suggest(query, 1, false);
    assertSuggestions(suggest, new Entry("abc_1", 1));
    reader.close();
    iw.close();
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) IntPoint(org.apache.lucene.document.IntPoint) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Test(org.junit.Test)

Example 92 with StoredField

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

the class TestIndexWriterExceptions method testNullStoredBytesField.

/** test a null byte[] value doesn't abort the entire segment */
public void testNullStoredBytesField() throws Exception {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random());
    IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    // add good document
    Document doc = new Document();
    iw.addDocument(doc);
    expectThrows(NullPointerException.class, () -> {
        // set to null value
        byte[] v = null;
        Field theField = new StoredField("foo", v);
        doc.add(theField);
        iw.addDocument(doc);
    });
    assertNull(iw.getTragicException());
    iw.close();
    // make sure we see our good doc
    DirectoryReader r = DirectoryReader.open(dir);
    assertEquals(1, r.numDocs());
    r.close();
    dir.close();
}
Also used : StringField(org.apache.lucene.document.StringField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 93 with StoredField

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

the class BaseStoredFieldsFormatTestCase method testMergeFilterReader.

public void testMergeFilterReader() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(200);
    final String[] stringValues = new String[10];
    for (int i = 0; i < stringValues.length; ++i) {
        stringValues[i] = RandomStrings.randomRealisticUnicodeOfLength(random(), 10);
    }
    Document[] docs = new Document[numDocs];
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        doc.add(new StringField("to_delete", random().nextBoolean() ? "yes" : "no", Store.NO));
        doc.add(new StoredField("id", i));
        doc.add(new StoredField("i", random().nextInt(50)));
        doc.add(new StoredField("l", random().nextLong()));
        doc.add(new StoredField("d", random().nextDouble()));
        doc.add(new StoredField("f", random().nextFloat()));
        doc.add(new StoredField("s", RandomPicks.randomFrom(random(), stringValues)));
        doc.add(new StoredField("b", new BytesRef(RandomPicks.randomFrom(random(), stringValues))));
        docs[i] = doc;
        w.addDocument(doc);
    }
    if (random().nextBoolean()) {
        w.deleteDocuments(new Term("to_delete", "yes"));
    }
    w.commit();
    w.close();
    DirectoryReader reader = new DummyFilterDirectoryReader(DirectoryReader.open(dir));
    Directory dir2 = newDirectory();
    w = new RandomIndexWriter(random(), dir2);
    TestUtil.addIndexesSlowly(w.w, reader);
    reader.close();
    dir.close();
    reader = w.getReader();
    for (int i = 0; i < reader.maxDoc(); ++i) {
        final Document doc = reader.document(i);
        final int id = doc.getField("id").numericValue().intValue();
        final Document expected = docs[id];
        assertEquals(expected.get("s"), doc.get("s"));
        assertEquals(expected.getField("i").numericValue(), doc.getField("i").numericValue());
        assertEquals(expected.getField("l").numericValue(), doc.getField("l").numericValue());
        assertEquals(expected.getField("d").numericValue(), doc.getField("d").numericValue());
        assertEquals(expected.getField("f").numericValue(), doc.getField("f").numericValue());
        assertEquals(expected.getField("b").binaryValue(), doc.getField("b").binaryValue());
    }
    reader.close();
    w.close();
    TestUtil.checkIndex(dir2);
    dir2.close();
}
Also used : Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) StoredField(org.apache.lucene.document.StoredField) StringField(org.apache.lucene.document.StringField) BytesRef(org.apache.lucene.util.BytesRef) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory)

Example 94 with StoredField

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

the class BaseStoredFieldsFormatTestCase method testReadSkip.

public void testReadSkip() throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig iwConf = newIndexWriterConfig(new MockAnalyzer(random()));
    iwConf.setMaxBufferedDocs(RandomNumbers.randomIntBetween(random(), 2, 30));
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConf);
    FieldType ft = new FieldType();
    ft.setStored(true);
    ft.freeze();
    final String string = TestUtil.randomSimpleString(random(), 50);
    final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
    final long l = random().nextBoolean() ? random().nextInt(42) : random().nextLong();
    final int i = random().nextBoolean() ? random().nextInt(42) : random().nextInt();
    final float f = random().nextFloat();
    final double d = random().nextDouble();
    List<Field> fields = Arrays.asList(new Field("bytes", bytes, ft), new Field("string", string, ft), new StoredField("long", l), new StoredField("int", i), new StoredField("float", f), new StoredField("double", d));
    for (int k = 0; k < 100; ++k) {
        Document doc = new Document();
        for (Field fld : fields) {
            doc.add(fld);
        }
        iw.w.addDocument(doc);
    }
    iw.commit();
    final DirectoryReader reader = DirectoryReader.open(dir);
    final int docID = random().nextInt(100);
    for (Field fld : fields) {
        String fldName = fld.name();
        final Document sDoc = reader.document(docID, Collections.singleton(fldName));
        final IndexableField sField = sDoc.getField(fldName);
        if (Field.class.equals(fld.getClass())) {
            assertEquals(fld.binaryValue(), sField.binaryValue());
            assertEquals(fld.stringValue(), sField.stringValue());
        } else {
            assertEquals(fld.numericValue(), sField.numericValue());
        }
    }
    reader.close();
    iw.close();
    dir.close();
}
Also used : Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) FieldType(org.apache.lucene.document.FieldType) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory)

Example 95 with StoredField

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

the class BaseStoredFieldsFormatTestCase method testBinaryFieldOffsetLength.

// LUCENE-1219
public void testBinaryFieldOffsetLength() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    byte[] b = new byte[50];
    for (int i = 0; i < 50; i++) b[i] = (byte) (i + 77);
    Document doc = new Document();
    Field f = new StoredField("binary", b, 10, 17);
    byte[] bx = f.binaryValue().bytes;
    assertTrue(bx != null);
    assertEquals(50, bx.length);
    assertEquals(10, f.binaryValue().offset);
    assertEquals(17, f.binaryValue().length);
    doc.add(f);
    w.addDocument(doc);
    w.close();
    IndexReader ir = DirectoryReader.open(dir);
    Document doc2 = ir.document(0);
    IndexableField f2 = doc2.getField("binary");
    b = f2.binaryValue().bytes;
    assertTrue(b != null);
    assertEquals(17, b.length, 17);
    assertEquals(87, b[0]);
    ir.close();
    dir.close();
}
Also used : StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory)

Aggregations

StoredField (org.apache.lucene.document.StoredField)109 Document (org.apache.lucene.document.Document)97 Directory (org.apache.lucene.store.Directory)72 StringField (org.apache.lucene.document.StringField)43 Field (org.apache.lucene.document.Field)40 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)39 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)36 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)34 BytesRef (org.apache.lucene.util.BytesRef)34 TextField (org.apache.lucene.document.TextField)30 IndexReader (org.apache.lucene.index.IndexReader)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)26 IntPoint (org.apache.lucene.document.IntPoint)24 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)23 TopDocs (org.apache.lucene.search.TopDocs)23 SortField (org.apache.lucene.search.SortField)22 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)21 Sort (org.apache.lucene.search.Sort)21 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)18 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18