Methods Summary |
---|
private void | checkHits(org.apache.lucene.search.Hits hits, java.lang.String prefix)Check the hits for duplicates.
if(hits!=null) {
Map idMap = new TreeMap();
for(int docnum=0;docnum<hits.length();++docnum) {
Integer luceneId = null;
try {
luceneId = new Integer(hits.id(docnum));
if(idMap.containsKey(luceneId)) {
StringBuffer message = new StringBuffer(prefix);
message.append("Duplicate key for hit index = ");
message.append(docnum);
message.append(", previous index = ");
message.append(((Integer)idMap.get(luceneId)).toString());
message.append(", Lucene ID = ");
message.append(luceneId);
log(message.toString());
} else {
idMap.put(luceneId, new Integer(docnum));
}
} catch(IOException ioe) {
StringBuffer message = new StringBuffer(prefix);
message.append("Error occurred for hit index = ");
message.append(docnum);
message.append(" (");
message.append(ioe.getMessage());
message.append(")");
log(message.toString());
}
}
}
|
private org.apache.lucene.store.Directory | getIndex()
RAMDirectory indexStore = new RAMDirectory ();
IndexWriter writer = new IndexWriter (indexStore, new StandardAnalyzer(), true);
RandomGen random = new RandomGen();
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 (new Field("publicationDate_", random.getLuceneDate(), Field.Store.YES, Field.Index.UN_TOKENIZED));
}
if((i%7)==0) { // some documents to match the query (see below)
doc.add (new Field("content", "test", Field.Store.YES, Field.Index.TOKENIZED));
}
// every document has a defined 'mandant' field
doc.add(new Field("mandant", Integer.toString(i%3), Field.Store.YES, Field.Index.UN_TOKENIZED));
writer.addDocument (doc);
}
writer.optimize ();
writer.close ();
return indexStore;
|
private void | log(java.lang.String message)
System.out.println(message);
|
public static void | main(java.lang.String[] argv)
TestRunner.run (suite());
|
private void | matchHits(org.apache.lucene.search.Searcher searcher, org.apache.lucene.search.Sort sort)
// make a query without sorting first
Hits hitsByRank = searcher.search(query);
checkHits(hitsByRank, "Sort by rank: "); // check for duplicates
Map resultMap = new TreeMap();
// store hits in TreeMap - TreeMap does not allow duplicates; existing entries are silently overwritten
for(int hitid=0;hitid<hitsByRank.length(); ++hitid) {
resultMap.put(
new Integer(hitsByRank.id(hitid)), // Key: Lucene Document ID
new Integer(hitid)); // Value: Hits-Objekt Index
}
// now make a query using the sort criteria
Hits resultSort = searcher.search (query, sort);
checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates
String lf = System.getProperty("line.separator", "\n");
// besides the sorting both sets of hits must be identical
for(int hitid=0;hitid<resultSort.length(); ++hitid) {
Integer idHitDate = new Integer(resultSort.id(hitid)); // document ID from sorted search
if(!resultMap.containsKey(idHitDate)) {
log("ID "+idHitDate+" not found. Possibliy a duplicate.");
}
assertTrue(resultMap.containsKey(idHitDate)); // same ID must be in the Map from the rank-sorted search
// every hit must appear once in both result sets --> remove it from the Map.
// At the end the Map must be empty!
resultMap.remove(idHitDate);
}
if(resultMap.size()==0) {
// log("All hits matched");
} else {
log("Couldn't match "+resultMap.size()+" hits.");
}
assertEquals(resultMap.size(), 0);
|
public void | setUp()Create index and query for test cases.
index = getIndex();
query = new TermQuery( new Term("content", "test"));
|
public static junit.framework.Test | suite()
return new TestSuite (TestCustomSearcherSort.class);
|
public void | testFieldSortCustomSearcher()Run the test using two CustomSearcher instances.
// log("Run testFieldSortCustomSearcher");
// define the sort criteria
Sort custSort = new Sort(new SortField[] {
new SortField("publicationDate_"),
SortField.FIELD_SCORE
});
Searcher searcher = new CustomSearcher (index, 2);
// search and check hits
matchHits(searcher, custSort);
|
public void | testFieldSortMultiCustomSearcher()Run the test using two CustomSearcher instances.
// log("Run testFieldSortMultiCustomSearcher");
// define the sort criteria
Sort custSort = new Sort(new SortField[] {
new SortField("publicationDate_"),
SortField.FIELD_SCORE
});
Searcher searcher =
new MultiSearcher(new Searchable[] {
new CustomSearcher (index, 0),
new CustomSearcher (index, 2)});
// search and check hits
matchHits(searcher, custSort);
|
public void | testFieldSortSingleSearcher()Run the test using one CustomSearcher wrapped by a MultiSearcher.
// log("Run testFieldSortSingleSearcher");
// define the sort criteria
Sort custSort = new Sort(new SortField[] {
new SortField("publicationDate_"),
SortField.FIELD_SCORE
});
Searcher searcher =
new MultiSearcher(new Searchable[] {
new CustomSearcher (index, 2)});
// search and check hits
matchHits(searcher, custSort);
|