1 package org.apache.lucene.search.vectorhighlight;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.search.BooleanQuery;
21 import org.apache.lucene.search.BooleanClause.Occur;
22 import org.apache.lucene.search.WildcardQuery;
24 public class FieldTermStackTest extends AbstractTestCase {
26 public void test1Term() throws Exception {
29 FieldQuery fq = new FieldQuery( tq( "a" ), true, true );
30 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
31 assertEquals( 6, stack.termList.size() );
32 assertEquals( "a(0,1,0)", stack.pop().toString() );
33 assertEquals( "a(2,3,1)", stack.pop().toString() );
34 assertEquals( "a(4,5,2)", stack.pop().toString() );
35 assertEquals( "a(12,13,6)", stack.pop().toString() );
36 assertEquals( "a(28,29,14)", stack.pop().toString() );
37 assertEquals( "a(32,33,16)", stack.pop().toString() );
40 public void test2Terms() throws Exception {
43 BooleanQuery query = new BooleanQuery();
44 query.add( tq( "b" ), Occur.SHOULD );
45 query.add( tq( "c" ), Occur.SHOULD );
46 FieldQuery fq = new FieldQuery( query, true, true );
47 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
48 assertEquals( 8, stack.termList.size() );
49 assertEquals( "b(6,7,3)", stack.pop().toString() );
50 assertEquals( "b(8,9,4)", stack.pop().toString() );
51 assertEquals( "c(10,11,5)", stack.pop().toString() );
52 assertEquals( "b(14,15,7)", stack.pop().toString() );
53 assertEquals( "b(16,17,8)", stack.pop().toString() );
54 assertEquals( "c(18,19,9)", stack.pop().toString() );
55 assertEquals( "b(26,27,13)", stack.pop().toString() );
56 assertEquals( "b(30,31,15)", stack.pop().toString() );
59 public void test1Phrase() throws Exception {
62 FieldQuery fq = new FieldQuery( pqF( "c", "d" ), true, true );
63 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
64 assertEquals( 3, stack.termList.size() );
65 assertEquals( "c(10,11,5)", stack.pop().toString() );
66 assertEquals( "c(18,19,9)", stack.pop().toString() );
67 assertEquals( "d(20,21,10)", stack.pop().toString() );
70 private void makeIndex() throws Exception {
72 // 0123456789012345678901234 (offsets)
73 // a a a b b c a b b c d e f
74 // 0 1 2 3 4 5 6 7 8 9101112 (position)
75 String value1 = "a a a b b c a b b c d e f";
77 // 678901234 (offsets)
79 //1314151617 (position)
80 String value2 = "b a b a f";
82 make1dmfIndex( value1, value2 );
85 public void test1TermB() throws Exception {
88 FieldQuery fq = new FieldQuery( tq( "ab" ), true, true );
89 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
90 assertEquals( 2, stack.termList.size() );
91 assertEquals( "ab(2,4,2)", stack.pop().toString() );
92 assertEquals( "ab(6,8,6)", stack.pop().toString() );
95 public void test2TermsB() throws Exception {
98 BooleanQuery query = new BooleanQuery();
99 query.add( tq( "bc" ), Occur.SHOULD );
100 query.add( tq( "ef" ), Occur.SHOULD );
101 FieldQuery fq = new FieldQuery( query, true, true );
102 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
103 assertEquals( 3, stack.termList.size() );
104 assertEquals( "bc(4,6,4)", stack.pop().toString() );
105 assertEquals( "bc(8,10,8)", stack.pop().toString() );
106 assertEquals( "ef(11,13,11)", stack.pop().toString() );
109 public void test1PhraseB() throws Exception {
112 FieldQuery fq = new FieldQuery( pqF( "ab", "bb" ), true, true );
113 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
114 assertEquals( 4, stack.termList.size() );
115 assertEquals( "ab(2,4,2)", stack.pop().toString() );
116 assertEquals( "bb(3,5,3)", stack.pop().toString() );
117 assertEquals( "ab(6,8,6)", stack.pop().toString() );
118 assertEquals( "bb(7,9,7)", stack.pop().toString() );
121 private void makeIndexB() throws Exception {
123 // 01 12 23 34 45 56 67 78 89 90 01 12 (offsets)
124 // aa|aa|ab|bb|bc|ca|ab|bb|bc|cd|de|ef
125 // 0 1 2 3 4 5 6 7 8 9 10 11 (position)
126 String value = "aaabbcabbcdef";
128 make1dmfIndexB( value );
131 public void test1PhraseShortMV() throws Exception {
134 FieldQuery fq = new FieldQuery( tq( "d" ), true, true );
135 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
136 assertEquals( 1, stack.termList.size() );
137 assertEquals( "d(9,10,3)", stack.pop().toString() );
140 public void test1PhraseLongMV() throws Exception {
143 FieldQuery fq = new FieldQuery( pqF( "search", "engines" ), true, true );
144 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
145 assertEquals( 4, stack.termList.size() );
146 assertEquals( "search(102,108,14)", stack.pop().toString() );
147 assertEquals( "engines(109,116,15)", stack.pop().toString() );
148 assertEquals( "search(157,163,24)", stack.pop().toString() );
149 assertEquals( "engines(164,171,25)", stack.pop().toString() );
152 public void test1PhraseMVB() throws Exception {
155 FieldQuery fq = new FieldQuery( pqF( "sp", "pe", "ee", "ed" ), true, true ); // "speed" -(2gram)-> "sp","pe","ee","ed"
156 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
157 assertEquals( 4, stack.termList.size() );
158 assertEquals( "sp(88,90,61)", stack.pop().toString() );
159 assertEquals( "pe(89,91,62)", stack.pop().toString() );
160 assertEquals( "ee(90,92,63)", stack.pop().toString() );
161 assertEquals( "ed(91,93,64)", stack.pop().toString() );
165 public void testWildcard() throws Exception {
167 FieldQuery fq = new FieldQuery( new WildcardQuery (new Term(F, "th*e")), reader, true, true );
168 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
169 assertEquals (4, stack.termList.size());
170 assertEquals ("the(15,18,2)", stack.pop().toString());
171 assertEquals ("these(133,138,20)", stack.pop().toString());
172 assertEquals ("the(153,156,23)", stack.pop().toString());
173 assertEquals ("the(195,198,31)", stack.pop().toString());