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.search.BooleanQuery;
20 import org.apache.lucene.search.BooleanClause.Occur;
22 public class FieldTermStackTest extends AbstractTestCase {
24 public void test1Term() throws Exception {
27 FieldQuery fq = new FieldQuery( tq( "a" ), true, true );
28 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
29 assertEquals( 6, stack.termList.size() );
30 assertEquals( "a(0,1,0)", stack.pop().toString() );
31 assertEquals( "a(2,3,1)", stack.pop().toString() );
32 assertEquals( "a(4,5,2)", stack.pop().toString() );
33 assertEquals( "a(12,13,6)", stack.pop().toString() );
34 assertEquals( "a(28,29,14)", stack.pop().toString() );
35 assertEquals( "a(32,33,16)", stack.pop().toString() );
38 public void test2Terms() throws Exception {
41 BooleanQuery query = new BooleanQuery();
42 query.add( tq( "b" ), Occur.SHOULD );
43 query.add( tq( "c" ), Occur.SHOULD );
44 FieldQuery fq = new FieldQuery( query, true, true );
45 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
46 assertEquals( 8, stack.termList.size() );
47 assertEquals( "b(6,7,3)", stack.pop().toString() );
48 assertEquals( "b(8,9,4)", stack.pop().toString() );
49 assertEquals( "c(10,11,5)", stack.pop().toString() );
50 assertEquals( "b(14,15,7)", stack.pop().toString() );
51 assertEquals( "b(16,17,8)", stack.pop().toString() );
52 assertEquals( "c(18,19,9)", stack.pop().toString() );
53 assertEquals( "b(26,27,13)", stack.pop().toString() );
54 assertEquals( "b(30,31,15)", stack.pop().toString() );
57 public void test1Phrase() throws Exception {
60 FieldQuery fq = new FieldQuery( pqF( "c", "d" ), true, true );
61 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
62 assertEquals( 3, stack.termList.size() );
63 assertEquals( "c(10,11,5)", stack.pop().toString() );
64 assertEquals( "c(18,19,9)", stack.pop().toString() );
65 assertEquals( "d(20,21,10)", stack.pop().toString() );
68 private void makeIndex() throws Exception {
70 // 0123456789012345678901234 (offsets)
71 // a a a b b c a b b c d e f
72 // 0 1 2 3 4 5 6 7 8 9101112 (position)
73 String value1 = "a a a b b c a b b c d e f";
75 // 678901234 (offsets)
77 //1314151617 (position)
78 String value2 = "b a b a f";
80 make1dmfIndex( value1, value2 );
83 public void test1TermB() throws Exception {
86 FieldQuery fq = new FieldQuery( tq( "ab" ), true, true );
87 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
88 assertEquals( 2, stack.termList.size() );
89 assertEquals( "ab(2,4,2)", stack.pop().toString() );
90 assertEquals( "ab(6,8,6)", stack.pop().toString() );
93 public void test2TermsB() throws Exception {
96 BooleanQuery query = new BooleanQuery();
97 query.add( tq( "bc" ), Occur.SHOULD );
98 query.add( tq( "ef" ), Occur.SHOULD );
99 FieldQuery fq = new FieldQuery( query, true, true );
100 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
101 assertEquals( 3, stack.termList.size() );
102 assertEquals( "bc(4,6,4)", stack.pop().toString() );
103 assertEquals( "bc(8,10,8)", stack.pop().toString() );
104 assertEquals( "ef(11,13,11)", stack.pop().toString() );
107 public void test1PhraseB() throws Exception {
110 FieldQuery fq = new FieldQuery( pqF( "ab", "bb" ), true, true );
111 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
112 assertEquals( 4, stack.termList.size() );
113 assertEquals( "ab(2,4,2)", stack.pop().toString() );
114 assertEquals( "bb(3,5,3)", stack.pop().toString() );
115 assertEquals( "ab(6,8,6)", stack.pop().toString() );
116 assertEquals( "bb(7,9,7)", stack.pop().toString() );
119 private void makeIndexB() throws Exception {
121 // 01 12 23 34 45 56 67 78 89 90 01 12 (offsets)
122 // aa|aa|ab|bb|bc|ca|ab|bb|bc|cd|de|ef
123 // 0 1 2 3 4 5 6 7 8 9 10 11 (position)
124 String value = "aaabbcabbcdef";
126 make1dmfIndexB( value );
129 public void test1PhraseShortMV() throws Exception {
132 FieldQuery fq = new FieldQuery( tq( "d" ), true, true );
133 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
134 assertEquals( 1, stack.termList.size() );
135 assertEquals( "d(9,10,3)", stack.pop().toString() );
138 public void test1PhraseLongMV() throws Exception {
141 FieldQuery fq = new FieldQuery( pqF( "search", "engines" ), true, true );
142 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
143 assertEquals( 4, stack.termList.size() );
144 assertEquals( "search(102,108,14)", stack.pop().toString() );
145 assertEquals( "engines(109,116,15)", stack.pop().toString() );
146 assertEquals( "search(157,163,24)", stack.pop().toString() );
147 assertEquals( "engines(164,171,25)", stack.pop().toString() );
150 public void test1PhraseMVB() throws Exception {
153 FieldQuery fq = new FieldQuery( pqF( "sp", "pe", "ee", "ed" ), true, true ); // "speed" -(2gram)-> "sp","pe","ee","ed"
154 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
155 assertEquals( 4, stack.termList.size() );
156 assertEquals( "sp(88,90,61)", stack.pop().toString() );
157 assertEquals( "pe(89,91,62)", stack.pop().toString() );
158 assertEquals( "ee(90,92,63)", stack.pop().toString() );
159 assertEquals( "ed(91,93,64)", stack.pop().toString() );