001: package org.apache.lucene.search.spans;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.util.LuceneTestCase;
023:
024: import org.apache.lucene.analysis.standard.StandardAnalyzer;
025: import org.apache.lucene.document.Document;
026: import org.apache.lucene.document.Field;
027: import org.apache.lucene.index.IndexWriter;
028: import org.apache.lucene.index.Term;
029: import org.apache.lucene.search.*;
030: import org.apache.lucene.store.Directory;
031: import org.apache.lucene.store.RAMDirectory;
032:
033: /*******************************************************************************
034: * Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
035: * work correctly in a BooleanQuery.
036: *
037: * @author Reece Wilton
038: */
039: public class TestSpansAdvanced extends LuceneTestCase {
040:
041: // location to the index
042: protected Directory mDirectory;;
043:
044: protected IndexSearcher searcher;
045:
046: // field names in the index
047: private final static String FIELD_ID = "ID";
048: protected final static String FIELD_TEXT = "TEXT";
049:
050: /**
051: * Initializes the tests by adding 4 identical documents to the index.
052: */
053: protected void setUp() throws Exception {
054: super .setUp();
055: super .setUp();
056:
057: // create test index
058: mDirectory = new RAMDirectory();
059: final IndexWriter writer = new IndexWriter(mDirectory,
060: new StandardAnalyzer(), true);
061: addDocument(writer, "1", "I think it should work.");
062: addDocument(writer, "2", "I think it should work.");
063: addDocument(writer, "3", "I think it should work.");
064: addDocument(writer, "4", "I think it should work.");
065: writer.close();
066: searcher = new IndexSearcher(mDirectory);
067: }
068:
069: protected void tearDown() throws Exception {
070: super .tearDown();
071: searcher.close();
072: mDirectory.close();
073: mDirectory = null;
074: }
075:
076: /**
077: * Adds the document to the index.
078: *
079: * @param writer the Lucene index writer
080: * @param id the unique id of the document
081: * @param text the text of the document
082: * @throws IOException
083: */
084: protected void addDocument(final IndexWriter writer,
085: final String id, final String text) throws IOException {
086:
087: final Document document = new Document();
088: document.add(new Field(FIELD_ID, id, Field.Store.YES,
089: Field.Index.UN_TOKENIZED));
090: document.add(new Field(FIELD_TEXT, text, Field.Store.YES,
091: Field.Index.TOKENIZED));
092: writer.addDocument(document);
093: }
094:
095: /**
096: * Tests two span queries.
097: *
098: * @throws IOException
099: */
100: public void testBooleanQueryWithSpanQueries() throws IOException {
101:
102: doTestBooleanQueryWithSpanQueries(searcher, 0.3884282f);
103: }
104:
105: /**
106: * Tests two span queries.
107: *
108: * @throws IOException
109: */
110: protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s,
111: final float expectedScore) throws IOException {
112:
113: final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT,
114: "work"));
115: final BooleanQuery query = new BooleanQuery();
116: query.add(spanQuery, BooleanClause.Occur.MUST);
117: query.add(spanQuery, BooleanClause.Occur.MUST);
118: final String[] expectedIds = new String[] { "1", "2", "3", "4" };
119: final float[] expectedScores = new float[] { expectedScore,
120: expectedScore, expectedScore, expectedScore };
121: assertHits(s, query, "two span queries", expectedIds,
122: expectedScores);
123: }
124:
125: /**
126: * Checks to see if the hits are what we expected.
127: *
128: * @param query the query to execute
129: * @param description the description of the search
130: * @param expectedIds the expected document ids of the hits
131: * @param expectedScores the expected scores of the hits
132: *
133: * @throws IOException
134: */
135: protected static void assertHits(Searcher s, Query query,
136: final String description, final String[] expectedIds,
137: final float[] expectedScores) throws IOException {
138: QueryUtils.check(query, s);
139:
140: final float tolerance = 1e-5f;
141:
142: // Hits hits = searcher.search(query);
143: // hits normalizes and throws things off if one score is greater than 1.0
144: TopDocs topdocs = s.search(query, null, 10000);
145:
146: /*****
147: // display the hits
148: System.out.println(hits.length() + " hits for search: \"" + description + '\"');
149: for (int i = 0; i < hits.length(); i++) {
150: System.out.println(" " + FIELD_ID + ':' + hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')');
151: }
152: *****/
153:
154: // did we get the hits we expected
155: assertEquals(expectedIds.length, topdocs.totalHits);
156: for (int i = 0; i < topdocs.totalHits; i++) {
157: //System.out.println(i + " exp: " + expectedIds[i]);
158: //System.out.println(i + " field: " + hits.doc(i).get(FIELD_ID));
159:
160: int id = topdocs.scoreDocs[i].doc;
161: float score = topdocs.scoreDocs[i].score;
162: Document doc = s.doc(id);
163: assertEquals(expectedIds[i], doc.get(FIELD_ID));
164: boolean scoreEq = Math.abs(expectedScores[i] - score) < tolerance;
165: if (!scoreEq) {
166: System.out.println(i + " warning, expected score: "
167: + expectedScores[i] + ", actual " + score);
168: System.out.println(s.explain(query, id));
169: }
170: assertEquals(expectedScores[i], score, tolerance);
171: assertEquals(s.explain(query, id).getValue(), score,
172: tolerance);
173: }
174: }
175:
176: }
|