pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / spans / TestSpansAdvanced.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.util.LuceneTestCase;
23
24 import org.apache.lucene.analysis.standard.StandardAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.index.RandomIndexWriter;
29 import org.apache.lucene.index.Term;
30 import org.apache.lucene.search.*;
31 import org.apache.lucene.store.Directory;
32
33 /*******************************************************************************
34  * Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
35  * work correctly in a BooleanQuery.
36  * 
37  */
38 public class TestSpansAdvanced extends LuceneTestCase {
39   
40   // location to the index
41   protected Directory mDirectory;
42   protected IndexReader reader;
43   protected IndexSearcher searcher;
44   
45   // field names in the index
46   private final static String FIELD_ID = "ID";
47   protected final static String FIELD_TEXT = "TEXT";
48   
49   /**
50    * Initializes the tests by adding 4 identical documents to the index.
51    */
52   @Override
53   public void setUp() throws Exception {
54     super.setUp();
55     // create test index
56     mDirectory = newDirectory();
57     final RandomIndexWriter writer = new RandomIndexWriter(random,
58         mDirectory, newIndexWriterConfig(TEST_VERSION_CURRENT,
59                                          new StandardAnalyzer(TEST_VERSION_CURRENT)).setMergePolicy(newLogMergePolicy()));
60
61     addDocument(writer, "1", "I think it should work.");
62     addDocument(writer, "2", "I think it should work.");
63     addDocument(writer, "3", "I think it should work.");
64     addDocument(writer, "4", "I think it should work.");
65     reader = writer.getReader();
66     writer.close();
67     searcher = newSearcher(reader);
68   }
69   
70   @Override
71   public void tearDown() throws Exception {
72     searcher.close();
73     reader.close();
74     mDirectory.close();
75     mDirectory = null;
76     super.tearDown();
77   }
78   
79   /**
80    * Adds the document to the index.
81    * 
82    * @param writer the Lucene index writer
83    * @param id the unique id of the document
84    * @param text the text of the document
85    * @throws IOException
86    */
87   protected void addDocument(final RandomIndexWriter writer, final String id,
88       final String text) throws IOException {
89     
90     final Document document = new Document();
91     document.add(newField(FIELD_ID, id, Field.Store.YES,
92         Field.Index.NOT_ANALYZED));
93     document.add(newField(FIELD_TEXT, text, Field.Store.YES,
94         Field.Index.ANALYZED));
95     writer.addDocument(document);
96   }
97   
98   /**
99    * Tests two span queries.
100    * 
101    * @throws IOException
102    */
103   public void testBooleanQueryWithSpanQueries() throws IOException {
104     
105     doTestBooleanQueryWithSpanQueries(searcher, 0.3884282f);
106   }
107   
108   /**
109    * Tests two span queries.
110    * 
111    * @throws IOException
112    */
113   protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s,
114       final float expectedScore) throws IOException {
115     
116     final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "work"));
117     final BooleanQuery query = new BooleanQuery();
118     query.add(spanQuery, BooleanClause.Occur.MUST);
119     query.add(spanQuery, BooleanClause.Occur.MUST);
120     final String[] expectedIds = new String[] {"1", "2", "3", "4"};
121     final float[] expectedScores = new float[] {expectedScore, expectedScore,
122         expectedScore, expectedScore};
123     assertHits(s, query, "two span queries", expectedIds, expectedScores);
124   }
125   
126   /**
127    * Checks to see if the hits are what we expected.
128    * 
129    * @param query the query to execute
130    * @param description the description of the search
131    * @param expectedIds the expected document ids of the hits
132    * @param expectedScores the expected scores of the hits
133    * 
134    * @throws IOException
135    */
136   protected static void assertHits(Searcher s, Query query,
137       final String description, final String[] expectedIds,
138       final float[] expectedScores) throws IOException {
139     QueryUtils.check(random, query, s);
140     
141     final float tolerance = 1e-5f;
142     
143     // Hits hits = searcher.search(query);
144     // hits normalizes and throws things off if one score is greater than 1.0
145     TopDocs topdocs = s.search(query, null, 10000);
146     
147     /*****
148      * // display the hits System.out.println(hits.length() +
149      * " hits for search: \"" + description + '\"'); for (int i = 0; i <
150      * hits.length(); i++) { System.out.println("  " + FIELD_ID + ':' +
151      * hits.doc(i).get(FIELD_ID) + " (score:" + hits.score(i) + ')'); }
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: " + expectedScores[i]
167             + ", 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, tolerance);
172     }
173   }
174   
175 }