mirror of https://github.com/apache/lucene.git
add basic sorted index bwc test
This commit is contained in:
parent
53a0748f43
commit
774e31b6dd
|
@ -25,13 +25,18 @@ import java.lang.reflect.Modifier;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.text.ParsePosition;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.TimeZone;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -62,6 +67,8 @@ import org.apache.lucene.legacy.LegacyNumericUtils;
|
||||||
import org.apache.lucene.search.DocIdSetIterator;
|
import org.apache.lucene.search.DocIdSetIterator;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
import org.apache.lucene.search.ScoreDoc;
|
||||||
|
import org.apache.lucene.search.Sort;
|
||||||
|
import org.apache.lucene.search.SortField;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.store.BaseDirectoryWrapper;
|
import org.apache.lucene.store.BaseDirectoryWrapper;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
@ -166,6 +173,57 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
|
||||||
Thread.sleep(100000);
|
Thread.sleep(100000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ant test -Dtestcase=TestBackwardsCompatibility -Dtestmethod=testCreateSortedIndex -Dtests.codec=default -Dtests.useSecurityManager=false -Dtests.bwcdir=/tmp/sorted
|
||||||
|
public void testCreateSortedIndex() throws Exception {
|
||||||
|
|
||||||
|
Path indexDir = getIndexDir().resolve("sorted");
|
||||||
|
Files.deleteIfExists(indexDir);
|
||||||
|
Directory dir = newFSDirectory(indexDir);
|
||||||
|
|
||||||
|
LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
|
||||||
|
mp.setNoCFSRatio(1.0);
|
||||||
|
mp.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);
|
||||||
|
MockAnalyzer analyzer = new MockAnalyzer(random());
|
||||||
|
analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
|
||||||
|
|
||||||
|
// TODO: remove randomness
|
||||||
|
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
|
||||||
|
conf.setMergePolicy(mp);
|
||||||
|
conf.setUseCompoundFile(false);
|
||||||
|
conf.setIndexSort(new Sort(new SortField("dateDV", SortField.Type.LONG, true)));
|
||||||
|
IndexWriter writer = new IndexWriter(dir, conf);
|
||||||
|
LineFileDocs docs = new LineFileDocs(random());
|
||||||
|
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
|
||||||
|
parser.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
ParsePosition position = new ParsePosition(0);
|
||||||
|
Field dateDVField = null;
|
||||||
|
for(int i=0;i<50;i++) {
|
||||||
|
Document doc = docs.nextDoc();
|
||||||
|
String dateString = doc.get("date");
|
||||||
|
|
||||||
|
position.setIndex(0);
|
||||||
|
Date date = parser.parse(dateString, position);
|
||||||
|
if (position.getErrorIndex() != -1) {
|
||||||
|
throw new AssertionError("failed to parse \"" + dateString + "\" as date");
|
||||||
|
}
|
||||||
|
if (position.getIndex() != dateString.length()) {
|
||||||
|
throw new AssertionError("failed to parse \"" + dateString + "\" as date");
|
||||||
|
}
|
||||||
|
if (dateDVField == null) {
|
||||||
|
dateDVField = new NumericDocValuesField("dateDV", 0l);
|
||||||
|
doc.add(dateDVField);
|
||||||
|
}
|
||||||
|
dateDVField.setLongValue(date.getTime());
|
||||||
|
if (i == 250) {
|
||||||
|
writer.commit();
|
||||||
|
}
|
||||||
|
writer.addDocument(doc);
|
||||||
|
}
|
||||||
|
writer.forceMerge(1);
|
||||||
|
writer.close();
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
|
|
||||||
private void updateNumeric(IndexWriter writer, String id, String f, String cf, long value) throws IOException {
|
private void updateNumeric(IndexWriter writer, String id, String f, String cf, long value) throws IOException {
|
||||||
writer.updateNumericDocValue(new Term("id", id), f, value);
|
writer.updateNumericDocValue(new Term("id", id), f, value);
|
||||||
writer.updateNumericDocValue(new Term("id", id), cf, value*2);
|
writer.updateNumericDocValue(new Term("id", id), cf, value*2);
|
||||||
|
@ -1484,6 +1542,30 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSortedIndex() throws Exception {
|
||||||
|
String[] versions = new String[] {"6.2.0", "6.2.1", "6.3.0"};
|
||||||
|
for(String version : versions) {
|
||||||
|
Path path = createTempDir("sorted");
|
||||||
|
InputStream resource = TestBackwardsCompatibility.class.getResourceAsStream("sorted." + version + ".zip");
|
||||||
|
assertNotNull("Sorted index index " + version + " not found", resource);
|
||||||
|
TestUtil.unzip(resource, path);
|
||||||
|
|
||||||
|
// TODO: more tests
|
||||||
|
Directory dir = newFSDirectory(path);
|
||||||
|
|
||||||
|
DirectoryReader reader = DirectoryReader.open(dir);
|
||||||
|
assertEquals(1, reader.leaves().size());
|
||||||
|
Sort sort = reader.leaves().get(0).reader().getIndexSort();
|
||||||
|
assertNotNull(sort);
|
||||||
|
assertEquals("<long: \"dateDV\">!", sort.toString());
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
// this will confirm the docs really are sorted:
|
||||||
|
TestUtil.checkIndex(dir);
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static long getValue(BinaryDocValues bdv) throws IOException {
|
static long getValue(BinaryDocValues bdv) throws IOException {
|
||||||
BytesRef term = bdv.binaryValue();
|
BytesRef term = bdv.binaryValue();
|
||||||
int idx = term.offset;
|
int idx = term.offset;
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue