1 package org.apache.lucene.search.spans;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.util.LuceneTestCase;
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;
33 /*******************************************************************************
34 * Tests the span query bug in Lucene. It demonstrates that SpanTermQuerys don't
35 * work correctly in a BooleanQuery.
38 public class TestSpansAdvanced extends LuceneTestCase {
40 // location to the index
41 protected Directory mDirectory;
42 protected IndexReader reader;
43 protected IndexSearcher searcher;
45 // field names in the index
46 private final static String FIELD_ID = "ID";
47 protected final static String FIELD_TEXT = "TEXT";
50 * Initializes the tests by adding 4 identical documents to the index.
53 public void setUp() throws Exception {
56 mDirectory = newDirectory();
57 final RandomIndexWriter writer = new RandomIndexWriter(random,
58 mDirectory, newIndexWriterConfig(TEST_VERSION_CURRENT,
59 new StandardAnalyzer(TEST_VERSION_CURRENT)).setMergePolicy(newLogMergePolicy()));
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();
67 searcher = newSearcher(reader);
71 public void tearDown() throws Exception {
80 * Adds the document to the index.
82 * @param writer the Lucene index writer
83 * @param id the unique id of the document
84 * @param text the text of the document
87 protected void addDocument(final RandomIndexWriter writer, final String id,
88 final String text) throws IOException {
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);
99 * Tests two span queries.
101 * @throws IOException
103 public void testBooleanQueryWithSpanQueries() throws IOException {
105 doTestBooleanQueryWithSpanQueries(searcher, 0.3884282f);
109 * Tests two span queries.
111 * @throws IOException
113 protected void doTestBooleanQueryWithSpanQueries(IndexSearcher s,
114 final float expectedScore) throws IOException {
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);
127 * Checks to see if the hits are what we expected.
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
134 * @throws IOException
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);
141 final float tolerance = 1e-5f;
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);
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) + ')'); }
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));
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;
166 System.out.println(i + " warning, expected score: " + expectedScores[i]
167 + ", actual " + score);
168 System.out.println(s.explain(query, id));
170 assertEquals(expectedScores[i], score, tolerance);
171 assertEquals(s.explain(query, id).getValue(), score, tolerance);