FileDocCategorySizeDatePackage
TestSpansAdvanced.javaAPI DocApache Lucene 2.0.05954Fri May 26 09:54:10 BST 2006org.apache.lucene.search.spans

TestSpansAdvanced

public class TestSpansAdvanced extends TestCase
Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't work correctly in a BooleanQuery.
author
Reece Wilton

Fields Summary
protected Directory
mDirectory
protected IndexSearcher
searcher
private static final String
FIELD_ID
protected static final String
FIELD_TEXT
Constructors Summary
Methods Summary
protected voidaddDocument(org.apache.lucene.index.IndexWriter writer, java.lang.String id, java.lang.String text)
Adds the document to the index.

param
writer the Lucene index writer
param
id the unique id of the document
param
text the text of the document
throws
IOException


        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 static voidassertHits(org.apache.lucene.search.Searcher s, org.apache.lucene.search.Query query, java.lang.String description, java.lang.String[] expectedIds, float[] expectedScores)
Checks to see if the hits are what we expected.

param
query the query to execute
param
description the description of the search
param
expectedIds the expected document ids of the hits
param
expectedScores the expected scores of the hits
throws
IOException

        final float tolerance = 1e-5f;

        // Hits hits = searcher.search(query);
        // hits normalizes and throws things off if one score is greater than 1.0
        TopDocs topdocs = s.search(query,null,10000);

        /*****
        // 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, topdocs.totalHits);
        for (int i = 0; i < topdocs.totalHits; i++) {
            //System.out.println(i + " exp: " + expectedIds[i]);
            //System.out.println(i + " field: " + hits.doc(i).get(FIELD_ID));

            int id = topdocs.scoreDocs[i].doc;
            float score = topdocs.scoreDocs[i].score;
            Document doc = s.doc(id);
            assertEquals(expectedIds[i], doc.get(FIELD_ID));
            boolean scoreEq = Math.abs(expectedScores[i] - score) < tolerance;
            if (!scoreEq) {
              System.out.println(i + " warning, expected score: " + expectedScores[i] + ", actual " + score);
              System.out.println(s.explain(query,id));
            }
            assertEquals(expectedScores[i], score, tolerance);
            assertEquals(s.explain(query,id).getValue(), score, tolerance);
        }
    
protected voiddoTestBooleanQueryWithSpanQueries(org.apache.lucene.search.IndexSearcher s, float expectedScore)
Tests two span queries.

throws
IOException


        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 String[] expectedIds = new String[] { "1", "2", "3", "4" };
        final float[] expectedScores = new float[] { expectedScore, expectedScore, expectedScore, expectedScore };
        assertHits(s, query, "two span queries", expectedIds, expectedScores);
    
protected voidsetUp()
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();
        searcher = new IndexSearcher(mDirectory);
    
protected voidtearDown()

        searcher.close();
        mDirectory.close();
        mDirectory = null;
    
public voidtestBooleanQueryWithSpanQueries()
Tests two span queries.

throws
IOException


        doTestBooleanQueryWithSpanQueries(searcher,0.3884282f);