|
Lucene example source code file (TestCustomSearcherSort.java)
The Lucene TestCustomSearcherSort.java source code
package org.apache.lucene.search;
/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed 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.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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;
/** Unit test for sorting code. */
public class TestCustomSearcherSort extends LuceneTestCase implements Serializable {
private Directory index = null;
private IndexReader reader;
private Query query = null;
// reduced from 20000 to 2000 to speed up test...
private final static int INDEX_SIZE = 2000 * RANDOM_MULTIPLIER;
/**
* Create index and query for test cases.
*/
@Override
public void setUp() throws Exception {
super.setUp();
index = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, index);
RandomGen random = new RandomGen(this.random);
for (int i = 0; i < INDEX_SIZE; ++i) { // don't decrease; if to low the
// problem doesn't show up
Document doc = new Document();
if ((i % 5) != 0) { // some documents must not have an entry in the first
// sort field
doc.add(newField("publicationDate_", random.getLuceneDate(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
}
if ((i % 7) == 0) { // some documents to match the query (see below)
doc.add(newField("content", "test", Field.Store.YES,
Field.Index.ANALYZED));
}
// every document has a defined 'mandant' field
doc.add(newField("mandant", Integer.toString(i % 3), Field.Store.YES,
Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
reader = writer.getReader();
writer.close();
query = new TermQuery(new Term("content", "test"));
}
@Override
public void tearDown() throws Exception {
reader.close();
index.close();
super.tearDown();
}
/**
* Run the test using two CustomSearcher instances.
*/
public void testFieldSortCustomSearcher() throws Exception {
// log("Run testFieldSortCustomSearcher");
// define the sort criteria
Sort custSort = new Sort(
new SortField("publicationDate_", SortField.STRING),
SortField.FIELD_SCORE);
Searcher searcher = new CustomSearcher(reader, 2);
// search and check hits
matchHits(searcher, custSort);
}
/**
* Run the test using one CustomSearcher wrapped by a MultiSearcher.
*/
public void testFieldSortSingleSearcher() throws Exception {
// log("Run testFieldSortSingleSearcher");
// define the sort criteria
Sort custSort = new Sort(
new SortField("publicationDate_", SortField.STRING),
SortField.FIELD_SCORE);
Searcher searcher = new MultiSearcher(new Searcher[] {new CustomSearcher(
reader, 2)});
// search and check hits
matchHits(searcher, custSort);
}
/**
* Run the test using two CustomSearcher instances.
*/
public void testFieldSortMultiCustomSearcher() throws Exception {
// log("Run testFieldSortMultiCustomSearcher");
// define the sort criteria
Sort custSort = new Sort(
new SortField("publicationDate_", SortField.STRING),
SortField.FIELD_SCORE);
Searcher searcher = new MultiSearcher(new Searchable[] {
new CustomSearcher(reader, 0), new CustomSearcher(reader, 2)});
// search and check hits
matchHits(searcher, custSort);
}
// make sure the documents returned by the search match the expected list
private void matchHits(Searcher searcher, Sort sort) throws IOException {
// make a query without sorting first
ScoreDoc[] hitsByRank = searcher.search(query, null, Integer.MAX_VALUE).scoreDocs;
checkHits(hitsByRank, "Sort by rank: "); // check for duplicates
Map<Integer,Integer> resultMap = new TreeMap
Other Lucene examples (source code examples)Here is a short list of links related to this Lucene TestCustomSearcherSort.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.