1 package org.apache.lucene.search;
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 org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.analysis.MockTokenizer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.RandomIndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.search.IndexSearcher;
29 import org.apache.lucene.search.PhraseQuery;
30 import org.apache.lucene.store.Directory;
32 public class TestSloppyPhraseQuery extends LuceneTestCase {
34 private static final String S_1 = "A A A";
35 private static final String S_2 = "A 1 2 3 A 4 5 6 A";
37 private static final Document DOC_1 = makeDocument("X " + S_1 + " Y");
38 private static final Document DOC_2 = makeDocument("X " + S_2 + " Y");
39 private static final Document DOC_3 = makeDocument("X " + S_1 + " A Y");
40 private static final Document DOC_1_B = makeDocument("X " + S_1 + " Y N N N N " + S_1 + " Z");
41 private static final Document DOC_2_B = makeDocument("X " + S_2 + " Y N N N N " + S_2 + " Z");
42 private static final Document DOC_3_B = makeDocument("X " + S_1 + " A Y N N N N " + S_1 + " A Y");
43 private static final Document DOC_4 = makeDocument("A A X A X B A X B B A A X B A A");
45 private static final PhraseQuery QUERY_1 = makePhraseQuery( S_1 );
46 private static final PhraseQuery QUERY_2 = makePhraseQuery( S_2 );
47 private static final PhraseQuery QUERY_4 = makePhraseQuery( "X A A");
50 * Test DOC_4 and QUERY_4.
51 * QUERY_4 has a fuzzy (len=1) match to DOC_4, so all slop values > 0 should succeed.
52 * But only the 3rd sequence of A's in DOC_4 will do.
54 public void testDoc4_Query4_All_Slops_Should_match() throws Exception {
55 for (int slop=0; slop<30; slop++) {
56 int numResultsExpected = slop<1 ? 0 : 1;
57 checkPhraseQuery(DOC_4, QUERY_4, slop, numResultsExpected);
62 * Test DOC_1 and QUERY_1.
63 * QUERY_1 has an exact match to DOC_1, so all slop values should succeed.
64 * Before LUCENE-1310, a slop value of 1 did not succeed.
66 public void testDoc1_Query1_All_Slops_Should_match() throws Exception {
67 for (int slop=0; slop<30; slop++) {
68 float score1 = checkPhraseQuery(DOC_1, QUERY_1, slop, 1);
69 float score2 = checkPhraseQuery(DOC_1_B, QUERY_1, slop, 1);
70 assertTrue("slop="+slop+" score2="+score2+" should be greater than score1 "+score1, score2>score1);
75 * Test DOC_2 and QUERY_1.
76 * 6 should be the minimum slop to make QUERY_1 match DOC_2.
77 * Before LUCENE-1310, 7 was the minimum.
79 public void testDoc2_Query1_Slop_6_or_more_Should_match() throws Exception {
80 for (int slop=0; slop<30; slop++) {
81 int numResultsExpected = slop<6 ? 0 : 1;
82 float score1 = checkPhraseQuery(DOC_2, QUERY_1, slop, numResultsExpected);
83 if (numResultsExpected>0) {
84 float score2 = checkPhraseQuery(DOC_2_B, QUERY_1, slop, 1);
85 assertTrue("slop="+slop+" score2="+score2+" should be greater than score1 "+score1, score2>score1);
91 * Test DOC_2 and QUERY_2.
92 * QUERY_2 has an exact match to DOC_2, so all slop values should succeed.
93 * Before LUCENE-1310, 0 succeeds, 1 through 7 fail, and 8 or greater succeeds.
95 public void testDoc2_Query2_All_Slops_Should_match() throws Exception {
96 for (int slop=0; slop<30; slop++) {
97 float score1 = checkPhraseQuery(DOC_2, QUERY_2, slop, 1);
98 float score2 = checkPhraseQuery(DOC_2_B, QUERY_2, slop, 1);
99 assertTrue("slop="+slop+" score2="+score2+" should be greater than score1 "+score1, score2>score1);
104 * Test DOC_3 and QUERY_1.
105 * QUERY_1 has an exact match to DOC_3, so all slop values should succeed.
107 public void testDoc3_Query1_All_Slops_Should_match() throws Exception {
108 for (int slop=0; slop<30; slop++) {
109 float score1 = checkPhraseQuery(DOC_3, QUERY_1, slop, 1);
110 float score2 = checkPhraseQuery(DOC_3_B, QUERY_1, slop, 1);
111 assertTrue("slop="+slop+" score2="+score2+" should be greater than score1 "+score1, score2>score1);
115 private float checkPhraseQuery(Document doc, PhraseQuery query, int slop, int expectedNumResults) throws Exception {
118 Directory ramDir = newDirectory();
119 RandomIndexWriter writer = new RandomIndexWriter(random, ramDir, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false));
120 writer.addDocument(doc);
122 IndexReader reader = writer.getReader();
124 IndexSearcher searcher = newSearcher(reader);
125 TopDocs td = searcher.search(query,null,10);
126 //System.out.println("slop: "+slop+" query: "+query+" doc: "+doc+" Expecting number of hits: "+expectedNumResults+" maxScore="+td.getMaxScore());
127 assertEquals("slop: "+slop+" query: "+query+" doc: "+doc+" Wrong number of hits", expectedNumResults, td.totalHits);
129 //QueryUtils.check(query,searcher);
135 return td.getMaxScore();
138 private static Document makeDocument(String docText) {
139 Document doc = new Document();
140 Field f = new Field("f", docText, Field.Store.NO, Field.Index.ANALYZED);
141 f.setOmitNorms(true);
146 private static PhraseQuery makePhraseQuery(String terms) {
147 PhraseQuery query = new PhraseQuery();
148 String[] t = terms.split(" +");
149 for (int i=0; i<t.length; i++) {
150 query.add(new Term("f", t[i]));