1 package org.apache.lucene.search.vectorhighlight;
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.search.Query;
22 public class SimpleFragListBuilderTest extends AbstractTestCase {
24 public void testNullFieldFragList() throws Exception {
25 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
26 FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "b c d" ), 100 );
27 assertEquals( 0, ffl.getFragInfos().size() );
30 public void testTooSmallFragSize() throws Exception {
32 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
33 sflb.createFieldFragList( fpl( "a", "b c d" ), sflb.minFragCharSize - 1 );
34 fail( "IllegalArgumentException must be thrown" );
36 catch ( IllegalArgumentException expected ) {
40 public void testSmallerFragSizeThanTermQuery() throws Exception {
41 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
42 FieldFragList ffl = sflb.createFieldFragList( fpl( "abcdefghijklmnopqrs", "abcdefghijklmnopqrs" ), sflb.minFragCharSize );
43 assertEquals( 1, ffl.getFragInfos().size() );
44 assertEquals( "subInfos=(abcdefghijklmnopqrs((0,19)))/1.0(0,19)", ffl.getFragInfos().get( 0 ).toString() );
47 public void testSmallerFragSizeThanPhraseQuery() throws Exception {
48 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
49 FieldFragList ffl = sflb.createFieldFragList( fpl( "\"abcdefgh jklmnopqrs\"", "abcdefgh jklmnopqrs" ), sflb.minFragCharSize );
50 assertEquals( 1, ffl.getFragInfos().size() );
51 if (VERBOSE) System.out.println( ffl.getFragInfos().get( 0 ).toString() );
52 assertEquals( "subInfos=(abcdefghjklmnopqrs((0,21)))/1.0(0,21)", ffl.getFragInfos().get( 0 ).toString() );
55 public void test1TermIndex() throws Exception {
56 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
57 FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a" ), 100 );
58 assertEquals( 1, ffl.getFragInfos().size() );
59 assertEquals( "subInfos=(a((0,1)))/1.0(0,100)", ffl.getFragInfos().get( 0 ).toString() );
62 public void test2TermsIndex1Frag() throws Exception {
63 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
64 FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a a" ), 100 );
65 assertEquals( 1, ffl.getFragInfos().size() );
66 assertEquals( "subInfos=(a((0,1))a((2,3)))/2.0(0,100)", ffl.getFragInfos().get( 0 ).toString() );
68 ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b a" ), 20 );
69 assertEquals( 1, ffl.getFragInfos().size() );
70 assertEquals( "subInfos=(a((0,1))a((18,19)))/2.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
72 ffl = sflb.createFieldFragList( fpl( "a", "b b b b a b b b b a" ), 20 );
73 assertEquals( 1, ffl.getFragInfos().size() );
74 assertEquals( "subInfos=(a((8,9))a((18,19)))/2.0(2,22)", ffl.getFragInfos().get( 0 ).toString() );
77 public void test2TermsIndex2Frags() throws Exception {
78 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
79 FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b b b b b a" ), 20 );
80 assertEquals( 2, ffl.getFragInfos().size() );
81 assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
82 assertEquals( "subInfos=(a((28,29)))/1.0(22,42)", ffl.getFragInfos().get( 1 ).toString() );
84 ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b b b b a" ), 20 );
85 assertEquals( 2, ffl.getFragInfos().size() );
86 assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
87 assertEquals( "subInfos=(a((26,27)))/1.0(20,40)", ffl.getFragInfos().get( 1 ).toString() );
89 ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b a" ), 20 );
90 assertEquals( 2, ffl.getFragInfos().size() );
91 assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
92 assertEquals( "subInfos=(a((20,21)))/1.0(20,40)", ffl.getFragInfos().get( 1 ).toString() );
95 public void test2TermsQuery() throws Exception {
96 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
97 FieldFragList ffl = sflb.createFieldFragList( fpl( "a b", "c d e" ), 20 );
98 assertEquals( 0, ffl.getFragInfos().size() );
100 ffl = sflb.createFieldFragList( fpl( "a b", "d b c" ), 20 );
101 assertEquals( 1, ffl.getFragInfos().size() );
102 assertEquals( "subInfos=(b((2,3)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
104 ffl = sflb.createFieldFragList( fpl( "a b", "a b c" ), 20 );
105 assertEquals( 1, ffl.getFragInfos().size() );
106 assertEquals( "subInfos=(a((0,1))b((2,3)))/2.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
109 public void testPhraseQuery() throws Exception {
110 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
111 FieldFragList ffl = sflb.createFieldFragList( fpl( "\"a b\"", "c d e" ), 20 );
112 assertEquals( 0, ffl.getFragInfos().size() );
114 ffl = sflb.createFieldFragList( fpl( "\"a b\"", "a c b" ), 20 );
115 assertEquals( 0, ffl.getFragInfos().size() );
117 ffl = sflb.createFieldFragList( fpl( "\"a b\"", "a b c" ), 20 );
118 assertEquals( 1, ffl.getFragInfos().size() );
119 assertEquals( "subInfos=(ab((0,3)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
122 public void testPhraseQuerySlop() throws Exception {
123 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
124 FieldFragList ffl = sflb.createFieldFragList( fpl( "\"a b\"~1", "a c b" ), 20 );
125 assertEquals( 1, ffl.getFragInfos().size() );
126 assertEquals( "subInfos=(ab((0,1)(4,5)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
129 private FieldPhraseList fpl( String queryValue, String indexValue ) throws Exception {
130 make1d1fIndex( indexValue );
131 Query query = paW.parse( queryValue );
132 FieldQuery fq = new FieldQuery( query, true, true );
133 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
134 return new FieldPhraseList( stack, fq );
137 public void test1PhraseShortMV() throws Exception {
140 FieldQuery fq = new FieldQuery( tq( "d" ), true, true );
141 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
142 FieldPhraseList fpl = new FieldPhraseList( stack, fq );
143 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
144 FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
145 assertEquals( 1, ffl.getFragInfos().size() );
146 assertEquals( "subInfos=(d((9,10)))/1.0(3,103)", ffl.getFragInfos().get( 0 ).toString() );
149 public void test1PhraseLongMV() throws Exception {
152 FieldQuery fq = new FieldQuery( pqF( "search", "engines" ), true, true );
153 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
154 FieldPhraseList fpl = new FieldPhraseList( stack, fq );
155 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
156 FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
157 assertEquals( 1, ffl.getFragInfos().size() );
158 assertEquals( "subInfos=(searchengines((102,116))searchengines((157,171)))/2.0(96,196)", ffl.getFragInfos().get( 0 ).toString() );
161 public void test1PhraseLongMVB() throws Exception {
164 FieldQuery fq = new FieldQuery( pqF( "sp", "pe", "ee", "ed" ), true, true ); // "speed" -(2gram)-> "sp","pe","ee","ed"
165 FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
166 FieldPhraseList fpl = new FieldPhraseList( stack, fq );
167 SimpleFragListBuilder sflb = new SimpleFragListBuilder();
168 FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
169 assertEquals( 1, ffl.getFragInfos().size() );
170 assertEquals( "subInfos=(sppeeeed((88,93)))/1.0(82,182)", ffl.getFragInfos().get( 0 ).toString() );