pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.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.index.Term;
20 import org.apache.lucene.search.BooleanQuery;
21 import org.apache.lucene.search.BooleanClause.Occur;
22 import org.apache.lucene.search.WildcardQuery;
23
24 public class FieldTermStackTest extends AbstractTestCase {
25   
26   public void test1Term() throws Exception {
27     makeIndex();
28     
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() );
38   }
39   
40   public void test2Terms() throws Exception {
41     makeIndex();
42     
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() );
57   }
58   
59   public void test1Phrase() throws Exception {
60     makeIndex();
61     
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() );
68   }
69   
70   private void makeIndex() throws Exception {
71     //           111111111122222
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";
76     // 222233333
77     // 678901234 (offsets)
78     // b a b a f
79     //1314151617 (position)
80     String value2 = "b a b a f";
81     
82     make1dmfIndex( value1, value2 );
83   }
84   
85   public void test1TermB() throws Exception {
86     makeIndexB();
87     
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() );
93   }
94   
95   public void test2TermsB() throws Exception {
96     makeIndexB();
97     
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() );
107   }
108   
109   public void test1PhraseB() throws Exception {
110     makeIndexB();
111     
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() );
119   }
120   
121   private void makeIndexB() throws Exception {
122     //                             1 11 11
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";
127     
128     make1dmfIndexB( value );
129   }
130   
131   public void test1PhraseShortMV() throws Exception {
132     makeIndexShortMV();
133     
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() );
138   }
139   
140   public void test1PhraseLongMV() throws Exception {
141     makeIndexLongMV();
142     
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() );
150   }
151
152   public void test1PhraseMVB() throws Exception {
153     makeIndexLongMVB();
154     
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() );
162   }
163
164   
165   public void testWildcard() throws Exception {
166     makeIndexLongMV();
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());
174   }
175
176 }