pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / spans / TestSpansAdvanced2.java
1 package org.apache.lucene.search.spans;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.analysis.standard.StandardAnalyzer;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.RandomIndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
27 import org.apache.lucene.search.*;
28
29 /*******************************************************************************
30  * Some expanded tests to make sure my patch doesn't break other SpanTermQuery
31  * functionality.
32  * 
33  */
34 public class TestSpansAdvanced2 extends TestSpansAdvanced {
35   IndexSearcher searcher2;
36   IndexReader reader2;
37   
38   /**
39    * Initializes the tests by adding documents to the index.
40    */
41   @Override
42   public void setUp() throws Exception {
43     super.setUp();
44     
45     // create test index
46     final RandomIndexWriter writer = new RandomIndexWriter(random, mDirectory,
47         newIndexWriterConfig(TEST_VERSION_CURRENT, new StandardAnalyzer(TEST_VERSION_CURRENT))
48                                                            .setOpenMode(OpenMode.APPEND).setMergePolicy(newLogMergePolicy()));
49     addDocument(writer, "A", "Should we, could we, would we?");
50     addDocument(writer, "B", "It should.  Should it?");
51     addDocument(writer, "C", "It shouldn't.");
52     addDocument(writer, "D", "Should we, should we, should we.");
53     reader2 = writer.getReader();
54     writer.close();
55     
56     // re-open the searcher since we added more docs
57     searcher2 = newSearcher(reader2);
58   }
59   
60   @Override
61   public void tearDown() throws Exception {
62     searcher2.close();
63     reader2.close();
64     super.tearDown();
65   }
66   
67   /**
68    * Verifies that the index has the correct number of documents.
69    * 
70    * @throws Exception
71    */
72   public void testVerifyIndex() throws Exception {
73     final IndexReader reader = IndexReader.open(mDirectory, true);
74     assertEquals(8, reader.numDocs());
75     reader.close();
76   }
77   
78   /**
79    * Tests a single span query that matches multiple documents.
80    * 
81    * @throws IOException
82    */
83   public void testSingleSpanQuery() throws IOException {
84     
85     final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
86     final String[] expectedIds = new String[] {"B", "D", "1", "2", "3", "4",
87         "A"};
88     final float[] expectedScores = new float[] {0.625f, 0.45927936f,
89         0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.26516503f,};
90     assertHits(searcher2, spanQuery, "single span query", expectedIds,
91         expectedScores);
92   }
93   
94   /**
95    * Tests a single span query that matches multiple documents.
96    * 
97    * @throws IOException
98    */
99   public void testMultipleDifferentSpanQueries() throws IOException {
100     
101     final Query spanQuery1 = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
102     final Query spanQuery2 = new SpanTermQuery(new Term(FIELD_TEXT, "we"));
103     final BooleanQuery query = new BooleanQuery();
104     query.add(spanQuery1, BooleanClause.Occur.MUST);
105     query.add(spanQuery2, BooleanClause.Occur.MUST);
106     final String[] expectedIds = new String[] {"D", "A"};
107     // these values were pre LUCENE-413
108     // final float[] expectedScores = new float[] { 0.93163157f, 0.20698164f };
109     final float[] expectedScores = new float[] {1.0191123f, 0.93163157f};
110     assertHits(searcher2, query, "multiple different span queries",
111         expectedIds, expectedScores);
112   }
113   
114   /**
115    * Tests two span queries.
116    * 
117    * @throws IOException
118    */
119   @Override
120   public void testBooleanQueryWithSpanQueries() throws IOException {
121     
122     doTestBooleanQueryWithSpanQueries(searcher2, 0.73500174f);
123   }
124 }