|
Lucene example source code file (TestTopDocsMerge.java)
The Lucene TestTopDocsMerge.java source code
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.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.ReaderUtil;
import org.apache.lucene.util._TestUtil;
public class TestTopDocsMerge extends LuceneTestCase {
private static class ShardSearcher {
private final IndexSearcher subSearcher;
public ShardSearcher(IndexReader subReader) {
this.subSearcher = new IndexSearcher(subReader);
}
public void search(Weight weight, Collector collector) throws IOException {
subSearcher.search(weight, null, collector);
}
public TopDocs search(Weight weight, int topN) throws IOException {
return subSearcher.search(weight, null, topN);
}
@Override
public String toString() {
return "ShardSearcher(" + subSearcher + ")";
}
}
public void testSort() throws Exception {
IndexReader reader = null;
Directory dir = null;
final int numDocs = atLeast(1000);
//final int numDocs = atLeast(50);
final String[] tokens = new String[] {"a", "b", "c", "d", "e"};
if (VERBOSE) {
System.out.println("TEST: make index");
}
{
dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random, dir);
// w.setDoRandomOptimize(false);
// w.w.getConfig().setMaxBufferedDocs(atLeast(100));
final String[] content = new String[atLeast(20)];
for(int contentIDX=0;contentIDX<content.length;contentIDX++) {
final StringBuilder sb = new StringBuilder();
final int numTokens = _TestUtil.nextInt(random, 1, 10);
for(int tokenIDX=0;tokenIDX<numTokens;tokenIDX++) {
sb.append(tokens[random.nextInt(tokens.length)]).append(' ');
}
content[contentIDX] = sb.toString();
}
for(int docIDX=0;docIDX<numDocs;docIDX++) {
final Document doc = new Document();
doc.add(newField("string", _TestUtil.randomRealisticUnicodeString(random), Field.Index.NOT_ANALYZED));
doc.add(newField("text", content[random.nextInt(content.length)], Field.Index.ANALYZED));
doc.add(new NumericField("float").setFloatValue(random.nextFloat()));
final int intValue;
if (random.nextInt(100) == 17) {
intValue = Integer.MIN_VALUE;
} else if (random.nextInt(100) == 17) {
intValue = Integer.MAX_VALUE;
} else {
intValue = random.nextInt();
}
doc.add(new NumericField("int").setIntValue(intValue));
if (VERBOSE) {
System.out.println(" doc=" + doc);
}
w.addDocument(doc);
}
reader = w.getReader();
w.close();
}
// NOTE: sometimes reader has just one segment, which is
// important to test
final IndexSearcher searcher = newSearcher(reader);
IndexReader[] subReaders = searcher.getIndexReader().getSequentialSubReaders();
if (subReaders == null) {
subReaders = new IndexReader[] {searcher.getIndexReader()};
}
final ShardSearcher[] subSearchers = new ShardSearcher[subReaders.length];
for(int searcherIDX=0;searcherIDX<subSearchers.length;searcherIDX++) {
subSearchers[searcherIDX] = new ShardSearcher(subReaders[searcherIDX]);
}
final List<SortField> sortFields = new ArrayList
Other Lucene examples (source code examples)Here is a short list of links related to this Lucene TestTopDocsMerge.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.