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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations