TestSpansAdvancedpublic class TestSpansAdvanced extends TestCase Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
work correctly in a BooleanQuery. |
Fields Summary |
---|
protected Directory | mDirectory | private static final String | FIELD_ID | protected static final String | FIELD_TEXT |
Methods Summary |
---|
protected void | addDocument(org.apache.lucene.index.IndexWriter writer, java.lang.String id, java.lang.String text)Adds the document to the index.
final Document document = new Document();
document.add(new Field(FIELD_ID, id, Field.Store.YES, Field.Index.UN_TOKENIZED));
document.add(new Field(FIELD_TEXT, text, Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(document);
| protected void | assertHits(org.apache.lucene.search.Hits hits, java.lang.String description, java.lang.String[] expectedIds, float[] expectedScores)Checks to see if the hits are what we expected.
// display the hits
/*System.out.println(hits.length() + " hits for search: \"" + description + '\"');
for (int i = 0; i < hits.length(); i++) {
System.out.println(" " + FIELD_ID + ':' + hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')');
}*/
// did we get the hits we expected
assertEquals(expectedIds.length, hits.length());
for (int i = 0; i < hits.length(); i++) {
assertTrue(expectedIds[i].equals(hits.doc(i).get(FIELD_ID)));
assertEquals(expectedScores[i], hits.score(i), 0);
}
| protected void | doTestBooleanQueryWithSpanQueries(float expectedScore)Tests two span queries.
final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
final BooleanQuery query = new BooleanQuery();
query.add(spanQuery, BooleanClause.Occur.MUST);
query.add(spanQuery, BooleanClause.Occur.MUST);
final Hits hits = executeQuery(query);
final String[] expectedIds = new String[] { "1", "2", "3", "4" };
final float[] expectedScores = new float[] { expectedScore, expectedScore, expectedScore, expectedScore };
assertHits(hits, "two span queries", expectedIds, expectedScores);
| protected org.apache.lucene.search.Hits | executeQuery(org.apache.lucene.search.Query query)Executes the query and throws an assertion if the results don't match the
expectedHits.
final IndexSearcher searcher = new IndexSearcher(mDirectory);
final Hits hits = searcher.search(query);
searcher.close();
return hits;
| protected void | setUp()Initializes the tests by adding 4 identical documents to the index.
super.setUp();
// create test index
mDirectory = new RAMDirectory();
final IndexWriter writer = new IndexWriter(mDirectory, new StandardAnalyzer(), true);
addDocument(writer, "1", "I think it should work.");
addDocument(writer, "2", "I think it should work.");
addDocument(writer, "3", "I think it should work.");
addDocument(writer, "4", "I think it should work.");
writer.close();
| protected void | tearDown()
mDirectory.close();
mDirectory = null;
| public void | testBooleanQueryWithSpanQueries()Tests two span queries.
doTestBooleanQueryWithSpanQueries(0.3884282f);
|
|