add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / highlighter / src / test / org / apache / lucene / search / vectorhighlight / FieldTermStackTest.java
1 package org.apache.lucene.search.vectorhighlight;
2 /**
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import org.apache.lucene.search.BooleanQuery;
20 import org.apache.lucene.search.BooleanClause.Occur;
21
22 public class FieldTermStackTest extends AbstractTestCase {
23   
24   public void test1Term() throws Exception {
25     makeIndex();
26     
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() );
36   }
37   
38   public void test2Terms() throws Exception {
39     makeIndex();
40     
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() );
55   }
56   
57   public void test1Phrase() throws Exception {
58     makeIndex();
59     
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() );
66   }
67   
68   private void makeIndex() throws Exception {
69     //           111111111122222
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";
74     // 222233333
75     // 678901234 (offsets)
76     // b a b a f
77     //1314151617 (position)
78     String value2 = "b a b a f";
79     
80     make1dmfIndex( value1, value2 );
81   }
82   
83   public void test1TermB() throws Exception {
84     makeIndexB();
85     
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() );
91   }
92   
93   public void test2TermsB() throws Exception {
94     makeIndexB();
95     
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() );
105   }
106   
107   public void test1PhraseB() throws Exception {
108     makeIndexB();
109     
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() );
117   }
118   
119   private void makeIndexB() throws Exception {
120     //                             1 11 11
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";
125     
126     make1dmfIndexB( value );
127   }
128   
129   public void test1PhraseShortMV() throws Exception {
130     makeIndexShortMV();
131     
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() );
136   }
137   
138   public void test1PhraseLongMV() throws Exception {
139     makeIndexLongMV();
140     
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() );
148   }
149
150   public void test1PhraseMVB() throws Exception {
151     makeIndexLongMVB();
152     
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() );
160   }
161 }