mirror of https://github.com/apache/lucene.git
LUCENE-4779: pull out more standalone tests
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1446226 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4cca2b258d
commit
b17b827eca
|
@ -1331,187 +1331,4 @@ public class TestSort extends LuceneTestCase {
|
|||
}
|
||||
assertEquals(msg, expectedResult, buff.toString());
|
||||
}
|
||||
|
||||
private static class RandomFilter extends Filter {
|
||||
private final Random random;
|
||||
private float density;
|
||||
private final List<BytesRef> docValues;
|
||||
public final List<BytesRef> matchValues = Collections.synchronizedList(new ArrayList<BytesRef>());
|
||||
|
||||
// density should be 0.0 ... 1.0
|
||||
public RandomFilter(Random random, float density, List<BytesRef> docValues) {
|
||||
this.random = random;
|
||||
this.density = density;
|
||||
this.docValues = docValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException {
|
||||
final int maxDoc = context.reader().maxDoc();
|
||||
final FieldCache.Ints idSource = FieldCache.DEFAULT.getInts(context.reader(), "id", false);
|
||||
assertNotNull(idSource);
|
||||
final FixedBitSet bits = new FixedBitSet(maxDoc);
|
||||
for(int docID=0;docID<maxDoc;docID++) {
|
||||
if (random.nextFloat() <= density && (acceptDocs == null || acceptDocs.get(docID))) {
|
||||
bits.set(docID);
|
||||
//System.out.println(" acc id=" + idSource.getInt(docID) + " docID=" + docID);
|
||||
matchValues.add(docValues.get(idSource.get(docID)));
|
||||
}
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
|
||||
public void testRandomStringSort() throws Exception {
|
||||
Random random = new Random(random().nextLong());
|
||||
|
||||
final int NUM_DOCS = atLeast(100);
|
||||
final Directory dir = newDirectory();
|
||||
final RandomIndexWriter writer = new RandomIndexWriter(random, dir);
|
||||
final boolean allowDups = random.nextBoolean();
|
||||
final Set<String> seen = new HashSet<String>();
|
||||
final int maxLength = _TestUtil.nextInt(random, 5, 100);
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: NUM_DOCS=" + NUM_DOCS + " maxLength=" + maxLength + " allowDups=" + allowDups);
|
||||
}
|
||||
|
||||
int numDocs = 0;
|
||||
final List<BytesRef> docValues = new ArrayList<BytesRef>();
|
||||
// TODO: deletions
|
||||
while (numDocs < NUM_DOCS) {
|
||||
final String s;
|
||||
if (random.nextBoolean()) {
|
||||
s = _TestUtil.randomSimpleString(random, maxLength);
|
||||
} else {
|
||||
s = _TestUtil.randomUnicodeString(random, maxLength);
|
||||
}
|
||||
final BytesRef br = new BytesRef(s);
|
||||
|
||||
if (!allowDups) {
|
||||
if (seen.contains(s)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(s);
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println(" " + numDocs + ": s=" + s);
|
||||
}
|
||||
|
||||
final Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("stringdv", br));
|
||||
doc.add(newStringField("string", s, Field.Store.NO));
|
||||
doc.add(new NumericDocValuesField("id", numDocs));
|
||||
docValues.add(br);
|
||||
writer.addDocument(doc);
|
||||
numDocs++;
|
||||
|
||||
if (random.nextInt(40) == 17) {
|
||||
// force flush
|
||||
writer.getReader().close();
|
||||
}
|
||||
}
|
||||
|
||||
final IndexReader r = writer.getReader();
|
||||
writer.close();
|
||||
if (VERBOSE) {
|
||||
System.out.println(" reader=" + r);
|
||||
}
|
||||
|
||||
final IndexSearcher s = newSearcher(r, false);
|
||||
final int ITERS = atLeast(100);
|
||||
for(int iter=0;iter<ITERS;iter++) {
|
||||
final boolean reverse = random.nextBoolean();
|
||||
final TopFieldDocs hits;
|
||||
final SortField sf;
|
||||
if (random.nextBoolean()) {
|
||||
sf = new SortField("stringdv", SortField.Type.STRING, reverse);
|
||||
} else {
|
||||
sf = new SortField("string", SortField.Type.STRING, reverse);
|
||||
}
|
||||
final Sort sort = new Sort(sf);
|
||||
final int hitCount = _TestUtil.nextInt(random, 1, r.maxDoc() + 20);
|
||||
final RandomFilter f = new RandomFilter(random, random.nextFloat(), docValues);
|
||||
if (random.nextBoolean()) {
|
||||
hits = s.search(new ConstantScoreQuery(f),
|
||||
hitCount,
|
||||
sort);
|
||||
} else {
|
||||
hits = s.search(new MatchAllDocsQuery(),
|
||||
f,
|
||||
hitCount,
|
||||
sort);
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println("\nTEST: iter=" + iter + " " + hits.totalHits + " hits; topN=" + hitCount + "; reverse=" + reverse);
|
||||
}
|
||||
|
||||
// Compute expected results:
|
||||
Collections.sort(f.matchValues);
|
||||
if (reverse) {
|
||||
Collections.reverse(f.matchValues);
|
||||
}
|
||||
final List<BytesRef> expected = f.matchValues;
|
||||
if (VERBOSE) {
|
||||
System.out.println(" expected:");
|
||||
for(int idx=0;idx<expected.size();idx++) {
|
||||
System.out.println(" " + idx + ": " + expected.get(idx).utf8ToString());
|
||||
if (idx == hitCount-1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println(" actual:");
|
||||
for(int hitIDX=0;hitIDX<hits.scoreDocs.length;hitIDX++) {
|
||||
final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX];
|
||||
System.out.println(" " + hitIDX + ": " + ((BytesRef) fd.fields[0]).utf8ToString());
|
||||
}
|
||||
}
|
||||
for(int hitIDX=0;hitIDX<hits.scoreDocs.length;hitIDX++) {
|
||||
final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX];
|
||||
assertEquals(expected.get(hitIDX), (BytesRef) fd.fields[0]);
|
||||
}
|
||||
}
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testMaxScore() throws Exception {
|
||||
Directory d = newDirectory();
|
||||
// Not RIW because we need exactly 2 segs:
|
||||
IndexWriter w = new IndexWriter(d, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
|
||||
int id = 0;
|
||||
for(int seg=0;seg<2;seg++) {
|
||||
for(int docIDX=0;docIDX<10;docIDX++) {
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("id", ""+docIDX, Field.Store.YES));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i=0;i<id;i++) {
|
||||
sb.append(' ');
|
||||
sb.append("text");
|
||||
}
|
||||
doc.add(newTextField("body", sb.toString(), Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
id++;
|
||||
}
|
||||
w.commit();
|
||||
}
|
||||
|
||||
IndexReader r = DirectoryReader.open(w, true);
|
||||
w.close();
|
||||
Query q = new TermQuery(new Term("body", "text"));
|
||||
IndexSearcher s = newSearcher(r);
|
||||
float maxScore = s.search(q , 10).getMaxScore();
|
||||
assertEquals(maxScore, s.search(q, null, 3, Sort.INDEXORDER, random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, Sort.RELEVANCE, random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, new Sort(new SortField[] {new SortField("id", SortField.Type.INT, false)}), random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, new Sort(new SortField[] {new SortField("id", SortField.Type.INT, true)}), random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
r.close();
|
||||
d.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.apache.lucene.document.StringField;
|
|||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriterConfig;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
@ -122,4 +123,38 @@ public class TestSort2 extends LuceneTestCase {
|
|||
reader.close();
|
||||
indexStore.close();
|
||||
}
|
||||
|
||||
public void testMaxScore() throws Exception {
|
||||
Directory d = newDirectory();
|
||||
// Not RIW because we need exactly 2 segs:
|
||||
IndexWriter w = new IndexWriter(d, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
|
||||
int id = 0;
|
||||
for(int seg=0;seg<2;seg++) {
|
||||
for(int docIDX=0;docIDX<10;docIDX++) {
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("id", ""+docIDX, Field.Store.YES));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i=0;i<id;i++) {
|
||||
sb.append(' ');
|
||||
sb.append("text");
|
||||
}
|
||||
doc.add(newTextField("body", sb.toString(), Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
id++;
|
||||
}
|
||||
w.commit();
|
||||
}
|
||||
|
||||
IndexReader r = DirectoryReader.open(w, true);
|
||||
w.close();
|
||||
Query q = new TermQuery(new Term("body", "text"));
|
||||
IndexSearcher s = newSearcher(r);
|
||||
float maxScore = s.search(q , 10).getMaxScore();
|
||||
assertEquals(maxScore, s.search(q, null, 3, Sort.INDEXORDER, random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, Sort.RELEVANCE, random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, new Sort(new SortField[] {new SortField("id", SortField.Type.INT, false)}), random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
assertEquals(maxScore, s.search(q, null, 3, new Sort(new SortField[] {new SortField("id", SortField.Type.INT, true)}), random().nextBoolean(), true).getMaxScore(), 0.0);
|
||||
r.close();
|
||||
d.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,193 @@
|
|||
package org.apache.lucene.search;
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.NumericDocValuesField;
|
||||
import org.apache.lucene.document.SortedDocValuesField;
|
||||
import org.apache.lucene.index.AtomicReaderContext;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.FixedBitSet;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util._TestUtil;
|
||||
|
||||
/** random sorting tests */
|
||||
public class TestSortRandom extends LuceneTestCase {
|
||||
|
||||
public void testRandomStringSort() throws Exception {
|
||||
Random random = new Random(random().nextLong());
|
||||
|
||||
final int NUM_DOCS = atLeast(100);
|
||||
final Directory dir = newDirectory();
|
||||
final RandomIndexWriter writer = new RandomIndexWriter(random, dir);
|
||||
final boolean allowDups = random.nextBoolean();
|
||||
final Set<String> seen = new HashSet<String>();
|
||||
final int maxLength = _TestUtil.nextInt(random, 5, 100);
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: NUM_DOCS=" + NUM_DOCS + " maxLength=" + maxLength + " allowDups=" + allowDups);
|
||||
}
|
||||
|
||||
int numDocs = 0;
|
||||
final List<BytesRef> docValues = new ArrayList<BytesRef>();
|
||||
// TODO: deletions
|
||||
while (numDocs < NUM_DOCS) {
|
||||
final String s;
|
||||
if (random.nextBoolean()) {
|
||||
s = _TestUtil.randomSimpleString(random, maxLength);
|
||||
} else {
|
||||
s = _TestUtil.randomUnicodeString(random, maxLength);
|
||||
}
|
||||
final BytesRef br = new BytesRef(s);
|
||||
|
||||
if (!allowDups) {
|
||||
if (seen.contains(s)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(s);
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println(" " + numDocs + ": s=" + s);
|
||||
}
|
||||
|
||||
final Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("stringdv", br));
|
||||
doc.add(newStringField("string", s, Field.Store.NO));
|
||||
doc.add(new NumericDocValuesField("id", numDocs));
|
||||
docValues.add(br);
|
||||
writer.addDocument(doc);
|
||||
numDocs++;
|
||||
|
||||
if (random.nextInt(40) == 17) {
|
||||
// force flush
|
||||
writer.getReader().close();
|
||||
}
|
||||
}
|
||||
|
||||
final IndexReader r = writer.getReader();
|
||||
writer.close();
|
||||
if (VERBOSE) {
|
||||
System.out.println(" reader=" + r);
|
||||
}
|
||||
|
||||
final IndexSearcher s = newSearcher(r, false);
|
||||
final int ITERS = atLeast(100);
|
||||
for(int iter=0;iter<ITERS;iter++) {
|
||||
final boolean reverse = random.nextBoolean();
|
||||
final TopFieldDocs hits;
|
||||
final SortField sf;
|
||||
if (random.nextBoolean()) {
|
||||
sf = new SortField("stringdv", SortField.Type.STRING, reverse);
|
||||
} else {
|
||||
sf = new SortField("string", SortField.Type.STRING, reverse);
|
||||
}
|
||||
final Sort sort = new Sort(sf);
|
||||
final int hitCount = _TestUtil.nextInt(random, 1, r.maxDoc() + 20);
|
||||
final RandomFilter f = new RandomFilter(random, random.nextFloat(), docValues);
|
||||
if (random.nextBoolean()) {
|
||||
hits = s.search(new ConstantScoreQuery(f),
|
||||
hitCount,
|
||||
sort);
|
||||
} else {
|
||||
hits = s.search(new MatchAllDocsQuery(),
|
||||
f,
|
||||
hitCount,
|
||||
sort);
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println("\nTEST: iter=" + iter + " " + hits.totalHits + " hits; topN=" + hitCount + "; reverse=" + reverse);
|
||||
}
|
||||
|
||||
// Compute expected results:
|
||||
Collections.sort(f.matchValues);
|
||||
if (reverse) {
|
||||
Collections.reverse(f.matchValues);
|
||||
}
|
||||
final List<BytesRef> expected = f.matchValues;
|
||||
if (VERBOSE) {
|
||||
System.out.println(" expected:");
|
||||
for(int idx=0;idx<expected.size();idx++) {
|
||||
System.out.println(" " + idx + ": " + expected.get(idx).utf8ToString());
|
||||
if (idx == hitCount-1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println(" actual:");
|
||||
for(int hitIDX=0;hitIDX<hits.scoreDocs.length;hitIDX++) {
|
||||
final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX];
|
||||
System.out.println(" " + hitIDX + ": " + ((BytesRef) fd.fields[0]).utf8ToString());
|
||||
}
|
||||
}
|
||||
for(int hitIDX=0;hitIDX<hits.scoreDocs.length;hitIDX++) {
|
||||
final FieldDoc fd = (FieldDoc) hits.scoreDocs[hitIDX];
|
||||
assertEquals(expected.get(hitIDX), (BytesRef) fd.fields[0]);
|
||||
}
|
||||
}
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
private static class RandomFilter extends Filter {
|
||||
private final Random random;
|
||||
private float density;
|
||||
private final List<BytesRef> docValues;
|
||||
public final List<BytesRef> matchValues = Collections.synchronizedList(new ArrayList<BytesRef>());
|
||||
|
||||
// density should be 0.0 ... 1.0
|
||||
public RandomFilter(Random random, float density, List<BytesRef> docValues) {
|
||||
this.random = random;
|
||||
this.density = density;
|
||||
this.docValues = docValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException {
|
||||
final int maxDoc = context.reader().maxDoc();
|
||||
final FieldCache.Ints idSource = FieldCache.DEFAULT.getInts(context.reader(), "id", false);
|
||||
assertNotNull(idSource);
|
||||
final FixedBitSet bits = new FixedBitSet(maxDoc);
|
||||
for(int docID=0;docID<maxDoc;docID++) {
|
||||
if (random.nextFloat() <= density && (acceptDocs == null || acceptDocs.get(docID))) {
|
||||
bits.set(docID);
|
||||
//System.out.println(" acc id=" + idSource.getInt(docID) + " docID=" + docID);
|
||||
matchValues.add(docValues.get(idSource.get(docID)));
|
||||
}
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue