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.analysis.standard.StandardAnalyzer;
023: import org.apache.lucene.index.IndexReader;
024: import org.apache.lucene.index.IndexWriter;
025: import org.apache.lucene.index.Term;
026: import org.apache.lucene.search.*;
027:
028: /*******************************************************************************
029: * Some expanded tests to make sure my patch doesn't break other SpanTermQuery
030: * functionality.
031: *
032: * @author Reece Wilton
033: */
034: public class TestSpansAdvanced2 extends TestSpansAdvanced {
035: IndexSearcher searcher2;
036:
037: /**
038: * Initializes the tests by adding documents to the index.
039: */
040: protected void setUp() throws Exception {
041: super .setUp();
042:
043: // create test index
044: final IndexWriter writer = new IndexWriter(mDirectory,
045: new StandardAnalyzer(), false);
046: addDocument(writer, "A", "Should we, could we, would we?");
047: addDocument(writer, "B", "It should. Should it?");
048: addDocument(writer, "C", "It shouldn't.");
049: addDocument(writer, "D", "Should we, should we, should we.");
050: writer.close();
051:
052: // re-open the searcher since we added more docs
053: searcher2 = new IndexSearcher(mDirectory);
054: }
055:
056: /**
057: * Verifies that the index has the correct number of documents.
058: *
059: * @throws Exception
060: */
061: public void testVerifyIndex() throws Exception {
062: final IndexReader reader = IndexReader.open(mDirectory);
063: assertEquals(8, reader.numDocs());
064: reader.close();
065: }
066:
067: /**
068: * Tests a single span query that matches multiple documents.
069: *
070: * @throws IOException
071: */
072: public void testSingleSpanQuery() throws IOException {
073:
074: final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT,
075: "should"));
076: final String[] expectedIds = new String[] { "B", "D", "1", "2",
077: "3", "4", "A" };
078: final float[] expectedScores = new float[] { 0.625f,
079: 0.45927936f, 0.35355338f, 0.35355338f, 0.35355338f,
080: 0.35355338f, 0.26516503f, };
081: assertHits(searcher2, spanQuery, "single span query",
082: expectedIds, expectedScores);
083: }
084:
085: /**
086: * Tests a single span query that matches multiple documents.
087: *
088: * @throws IOException
089: */
090: public void testMultipleDifferentSpanQueries() throws IOException {
091:
092: final Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT,
093: "should"));
094: final Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT,
095: "we"));
096: final BooleanQuery query = new BooleanQuery();
097: query.add(spanQuery1, BooleanClause.Occur.MUST);
098: query.add(spanQuery2, BooleanClause.Occur.MUST);
099: final String[] expectedIds = new String[] { "D", "A" };
100: // these values were pre LUCENE-413
101: // final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
102: final float[] expectedScores = new float[] { 1.0191123f,
103: 0.93163157f };
104: assertHits(searcher2, query, "multiple different span queries",
105: expectedIds, expectedScores);
106: }
107:
108: /**
109: * Tests two span queries.
110: *
111: * @throws IOException
112: */
113: public void testBooleanQueryWithSpanQueries() throws IOException {
114:
115: doTestBooleanQueryWithSpanQueries(searcher2, 0.73500174f);
116: }
117: }
|