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.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.*;
29 /*******************************************************************************
30 * Some expanded tests to make sure my patch doesn't break other SpanTermQuery
34 public class TestSpansAdvanced2 extends TestSpansAdvanced {
35 IndexSearcher searcher2;
39 * Initializes the tests by adding documents to the index.
42 public void setUp() throws Exception {
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();
56 // re-open the searcher since we added more docs
57 searcher2 = newSearcher(reader2);
61 public void tearDown() throws Exception {
68 * Verifies that the index has the correct number of documents.
72 public void testVerifyIndex() throws Exception {
73 final IndexReader reader = IndexReader.open(mDirectory, true);
74 assertEquals(8, reader.numDocs());
79 * Tests a single span query that matches multiple documents.
83 public void testSingleSpanQuery() throws IOException {
85 final Query spanQuery = new SpanTermQuery(new Term(FIELD_TEXT, "should"));
86 final String[] expectedIds = new String[] {"B", "D", "1", "2", "3", "4",
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,
95 * Tests a single span query that matches multiple documents.
99 public void testMultipleDifferentSpanQueries() throws IOException {
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);
115 * Tests two span queries.
117 * @throws IOException
120 public void testBooleanQueryWithSpanQueries() throws IOException {
122 doTestBooleanQueryWithSpanQueries(searcher2, 0.73500174f);