use of org.apache.lucene.document.NumericDocValuesField in project lucene-solr by apache.
the class TestNumericDocValuesUpdates method testDeleteUnusedUpdatesFiles.
@Test
public void testDeleteUnusedUpdatesFiles() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
IndexWriter writer = new IndexWriter(dir, conf);
Document doc = new Document();
doc.add(new StringField("id", "d0", Store.NO));
doc.add(new NumericDocValuesField("f1", 1L));
doc.add(new NumericDocValuesField("f2", 1L));
writer.addDocument(doc);
// update each field twice to make sure all unneeded files are deleted
for (String f : new String[] { "f1", "f2" }) {
writer.updateNumericDocValue(new Term("id", "d0"), f, 2L);
writer.commit();
int numFiles = dir.listAll().length;
// update again, number of files shouldn't change (old field's gen is
// removed)
writer.updateNumericDocValue(new Term("id", "d0"), f, 3L);
writer.commit();
assertEquals(numFiles, dir.listAll().length);
}
writer.close();
dir.close();
}
use of org.apache.lucene.document.NumericDocValuesField in project lucene-solr by apache.
the class TestSearchAfter method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
allSortFields = new ArrayList<>(Arrays.asList(new SortField[] { new SortField("int", SortField.Type.INT, false), new SortField("long", SortField.Type.LONG, false), new SortField("float", SortField.Type.FLOAT, false), new SortField("double", SortField.Type.DOUBLE, false), new SortField("bytes", SortField.Type.STRING, false), new SortField("bytesval", SortField.Type.STRING_VAL, false), new SortField("int", SortField.Type.INT, true), new SortField("long", SortField.Type.LONG, true), new SortField("float", SortField.Type.FLOAT, true), new SortField("double", SortField.Type.DOUBLE, true), new SortField("bytes", SortField.Type.STRING, true), new SortField("bytesval", SortField.Type.STRING_VAL, true), SortField.FIELD_SCORE, SortField.FIELD_DOC }));
// Also test missing first / last for the "string" sorts:
for (String field : new String[] { "bytes", "sortedbytesdocvalues" }) {
for (int rev = 0; rev < 2; rev++) {
boolean reversed = rev == 0;
SortField sf = new SortField(field, SortField.Type.STRING, reversed);
sf.setMissingValue(SortField.STRING_FIRST);
allSortFields.add(sf);
sf = new SortField(field, SortField.Type.STRING, reversed);
sf.setMissingValue(SortField.STRING_LAST);
allSortFields.add(sf);
}
}
// Also test missing first / last for the "string_val" sorts:
for (String field : new String[] { "sortedbytesdocvaluesval", "straightbytesdocvalues" }) {
for (int rev = 0; rev < 2; rev++) {
boolean reversed = rev == 0;
SortField sf = new SortField(field, SortField.Type.STRING_VAL, reversed);
sf.setMissingValue(SortField.STRING_FIRST);
allSortFields.add(sf);
sf = new SortField(field, SortField.Type.STRING_VAL, reversed);
sf.setMissingValue(SortField.STRING_LAST);
allSortFields.add(sf);
}
}
int limit = allSortFields.size();
for (int i = 0; i < limit; i++) {
SortField sf = allSortFields.get(i);
if (sf.getType() == SortField.Type.INT) {
SortField sf2 = new SortField(sf.getField(), SortField.Type.INT, sf.getReverse());
sf2.setMissingValue(random().nextInt());
allSortFields.add(sf2);
} else if (sf.getType() == SortField.Type.LONG) {
SortField sf2 = new SortField(sf.getField(), SortField.Type.LONG, sf.getReverse());
sf2.setMissingValue(random().nextLong());
allSortFields.add(sf2);
} else if (sf.getType() == SortField.Type.FLOAT) {
SortField sf2 = new SortField(sf.getField(), SortField.Type.FLOAT, sf.getReverse());
sf2.setMissingValue(random().nextFloat());
allSortFields.add(sf2);
} else if (sf.getType() == SortField.Type.DOUBLE) {
SortField sf2 = new SortField(sf.getField(), SortField.Type.DOUBLE, sf.getReverse());
sf2.setMissingValue(random().nextDouble());
allSortFields.add(sf2);
}
}
dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
int numDocs = atLeast(200);
Random r = random();
for (int i = 0; i < numDocs; i++) {
List<Field> fields = new ArrayList<>();
fields.add(newTextField("english", English.intToEnglish(i), Field.Store.NO));
fields.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO));
fields.add(new NumericDocValuesField("byte", (byte) r.nextInt()));
fields.add(new NumericDocValuesField("short", (short) r.nextInt()));
fields.add(new NumericDocValuesField("int", r.nextInt()));
fields.add(new NumericDocValuesField("long", r.nextLong()));
fields.add(new FloatDocValuesField("float", r.nextFloat()));
fields.add(new DoubleDocValuesField("double", r.nextDouble()));
fields.add(new SortedDocValuesField("bytes", new BytesRef(TestUtil.randomRealisticUnicodeString(random()))));
fields.add(new BinaryDocValuesField("bytesval", new BytesRef(TestUtil.randomRealisticUnicodeString(random()))));
Document document = new Document();
document.add(new StoredField("id", "" + i));
if (VERBOSE) {
System.out.println(" add doc id=" + i);
}
for (Field field : fields) {
// So we are sometimes missing that field:
if (random().nextInt(5) != 4) {
document.add(field);
if (VERBOSE) {
System.out.println(" " + field);
}
}
}
iw.addDocument(document);
if (random().nextInt(50) == 17) {
iw.commit();
}
}
reader = iw.getReader();
iw.close();
searcher = newSearcher(reader);
if (VERBOSE) {
System.out.println(" searcher=" + searcher);
}
}
use of org.apache.lucene.document.NumericDocValuesField in project lucene-solr by apache.
the class TestDirectoryReaderReopen method testNPEAfterInvalidReindex1.
public void testNPEAfterInvalidReindex1() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
Document doc = new Document();
doc.add(newStringField("id", "id", Field.Store.NO));
w.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "id2", Field.Store.NO));
w.addDocument(doc);
w.deleteDocuments(new Term("id", "id"));
w.commit();
w.close();
// Open reader w/ one segment w/ 2 docs, 1 deleted:
DirectoryReader r = DirectoryReader.open(dir);
// Blow away the index:
for (String fileName : dir.listAll()) {
dir.deleteFile(fileName);
}
w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
doc = new Document();
doc.add(newStringField("id", "id", Field.Store.NO));
doc.add(new NumericDocValuesField("ndv", 13));
w.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "id2", Field.Store.NO));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(newStringField("id", "id2", Field.Store.NO));
w.addDocument(doc);
w.updateNumericDocValue(new Term("id", "id"), "ndv", 17L);
w.commit();
w.close();
expectThrows(IllegalStateException.class, () -> {
DirectoryReader.openIfChanged(r);
});
r.close();
w.close();
dir.close();
}
use of org.apache.lucene.document.NumericDocValuesField in project lucene-solr by apache.
the class TestDirectoryReaderReopen method testNPEAfterInvalidReindex2.
public void testNPEAfterInvalidReindex2() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
Document doc = new Document();
doc.add(newStringField("id", "id", Field.Store.NO));
w.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "id2", Field.Store.NO));
w.addDocument(doc);
w.deleteDocuments(new Term("id", "id"));
w.commit();
w.close();
// Open reader w/ one segment w/ 2 docs, 1 deleted:
DirectoryReader r = DirectoryReader.open(dir);
// Blow away the index:
for (String name : dir.listAll()) {
dir.deleteFile(name);
}
w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
doc = new Document();
doc.add(newStringField("id", "id", Field.Store.NO));
doc.add(new NumericDocValuesField("ndv", 13));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(newStringField("id", "id2", Field.Store.NO));
w.addDocument(doc);
w.commit();
w.close();
expectThrows(IllegalStateException.class, () -> {
DirectoryReader.openIfChanged(r);
});
r.close();
dir.close();
}
use of org.apache.lucene.document.NumericDocValuesField in project lucene-solr by apache.
the class TestDocValuesIndexing method testTypeChangeAfterCommitAndDeleteAll.
public void testTypeChangeAfterCommitAndDeleteAll() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
IndexWriter writer = new IndexWriter(dir, conf);
Document doc = new Document();
doc.add(new NumericDocValuesField("dv", 0L));
writer.addDocument(doc);
writer.commit();
writer.deleteAll();
doc = new Document();
doc.add(new SortedDocValuesField("dv", new BytesRef("foo")));
writer.addDocument(doc);
writer.close();
dir.close();
}
Aggregations