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.index.IndexReader;
21 import org.apache.lucene.index.RandomIndexWriter;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.store.Directory;
24 import org.apache.lucene.util.LuceneTestCase;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
28 public class TestNGramPhraseQuery extends LuceneTestCase {
30 private static IndexReader reader;
31 private static Directory directory;
34 public static void beforeClass() throws Exception {
35 directory = newDirectory();
36 RandomIndexWriter writer = new RandomIndexWriter(random, directory);
38 reader = IndexReader.open(directory);
42 public static void afterClass() throws Exception {
49 public void testRewrite() throws Exception {
50 // bi-gram test ABC => AB/BC => AB/BC
51 PhraseQuery pq1 = new NGramPhraseQuery(2);
52 pq1.add(new Term("f", "AB"));
53 pq1.add(new Term("f", "BC"));
55 Query q = pq1.rewrite(reader);
56 assertTrue(q instanceof NGramPhraseQuery);
58 pq1 = (NGramPhraseQuery)q;
59 assertArrayEquals(new Term[]{new Term("f", "AB"), new Term("f", "BC")}, pq1.getTerms());
60 assertArrayEquals(new int[]{0, 1}, pq1.getPositions());
62 // bi-gram test ABCD => AB/BC/CD => AB//CD
63 PhraseQuery pq2 = new NGramPhraseQuery(2);
64 pq2.add(new Term("f", "AB"));
65 pq2.add(new Term("f", "BC"));
66 pq2.add(new Term("f", "CD"));
68 q = pq2.rewrite(reader);
69 assertTrue(q instanceof PhraseQuery);
70 assertNotSame(pq2, q);
72 assertArrayEquals(new Term[]{new Term("f", "AB"), new Term("f", "CD")}, pq2.getTerms());
73 assertArrayEquals(new int[]{0, 2}, pq2.getPositions());
75 // tri-gram test ABCDEFGH => ABC/BCD/CDE/DEF/EFG/FGH => ABC///DEF//FGH
76 PhraseQuery pq3 = new NGramPhraseQuery(3);
77 pq3.add(new Term("f", "ABC"));
78 pq3.add(new Term("f", "BCD"));
79 pq3.add(new Term("f", "CDE"));
80 pq3.add(new Term("f", "DEF"));
81 pq3.add(new Term("f", "EFG"));
82 pq3.add(new Term("f", "FGH"));
84 q = pq3.rewrite(reader);
85 assertTrue(q instanceof PhraseQuery);
86 assertNotSame(pq3, q);
88 assertArrayEquals(new Term[]{new Term("f", "ABC"), new Term("f", "DEF"), new Term("f", "FGH")}, pq3.getTerms());
89 assertArrayEquals(new int[]{0, 3, 5}, pq3.getPositions());