Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
134   284   18   11.17
2   197   0.13   4
12     1.5  
3    
 
  TestMultiFieldQueryParser       Line # 42 130 14 96.4% 0.9640288
  TestMultiFieldQueryParser.AnalyzerReturningNull       Line # 263 3 3 100% 1.0
  TestMultiFieldQueryParser.AnalyzerReturningNull.EmptyTokenStream       Line # 277 1 1 100% 1.0
 
  (9)
 
1    package org.apache.lucene.queryParser;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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 junit.framework.TestCase;
20    import org.apache.lucene.analysis.Analyzer;
21    import org.apache.lucene.analysis.Token;
22    import org.apache.lucene.analysis.TokenStream;
23    import org.apache.lucene.analysis.standard.StandardAnalyzer;
24    import org.apache.lucene.document.Document;
25    import org.apache.lucene.document.Field;
26    import org.apache.lucene.index.IndexWriter;
27    import org.apache.lucene.search.BooleanClause;
28    import org.apache.lucene.search.Hits;
29    import org.apache.lucene.search.IndexSearcher;
30    import org.apache.lucene.search.Query;
31    import org.apache.lucene.store.Directory;
32    import org.apache.lucene.store.RAMDirectory;
33   
34    import java.io.Reader;
35    import java.util.HashMap;
36    import java.util.Map;
37   
38    /**
39    * Tests QueryParser.
40    * @author Daniel Naber
41    */
 
42    public class TestMultiFieldQueryParser extends TestCase {
43   
 
44  1 toggle public void testSimple() throws Exception {
45  1 String[] fields = {"b", "t"};
46  1 MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new StandardAnalyzer());
47   
48  1 Query q = mfqp.parse("one");
49  1 assertEquals("b:one t:one", q.toString());
50   
51  1 q = mfqp.parse("one two");
52  1 assertEquals("(b:one t:one) (b:two t:two)", q.toString());
53   
54  1 q = mfqp.parse("+one +two");
55  1 assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
56   
57  1 q = mfqp.parse("+one -two -three)");
58  1 assertEquals("+(b:one t:one) -(b:two t:two) -(b:three t:three)", q.toString());
59   
60  1 q = mfqp.parse("one^2 two");
61  1 assertEquals("((b:one t:one)^2.0) (b:two t:two)", q.toString());
62   
63  1 q = mfqp.parse("one~ two");
64  1 assertEquals("(b:one~0.5 t:one~0.5) (b:two t:two)", q.toString());
65   
66  1 q = mfqp.parse("one~0.8 two^2");
67  1 assertEquals("(b:one~0.8 t:one~0.8) ((b:two t:two)^2.0)", q.toString());
68   
69  1 q = mfqp.parse("one* two*");
70  1 assertEquals("(b:one* t:one*) (b:two* t:two*)", q.toString());
71   
72  1 q = mfqp.parse("[a TO c] two");
73  1 assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString());
74   
75  1 q = mfqp.parse("w?ldcard");
76  1 assertEquals("b:w?ldcard t:w?ldcard", q.toString());
77   
78  1 q = mfqp.parse("\"foo bar\"");
79  1 assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString());
80   
81  1 q = mfqp.parse("\"aa bb cc\" \"dd ee\"");
82  1 assertEquals("(b:\"aa bb cc\" t:\"aa bb cc\") (b:\"dd ee\" t:\"dd ee\")", q.toString());
83   
84  1 q = mfqp.parse("\"foo bar\"~4");
85  1 assertEquals("b:\"foo bar\"~4 t:\"foo bar\"~4", q.toString());
86   
87    // make sure that terms which have a field are not touched:
88  1 q = mfqp.parse("one f:two");
89  1 assertEquals("(b:one t:one) f:two", q.toString());
90   
91    // AND mode:
92  1 mfqp.setDefaultOperator(QueryParser.AND_OPERATOR);
93  1 q = mfqp.parse("one two");
94  1 assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
95  1 q = mfqp.parse("\"aa bb cc\" \"dd ee\"");
96  1 assertEquals("+(b:\"aa bb cc\" t:\"aa bb cc\") +(b:\"dd ee\" t:\"dd ee\")", q.toString());
97   
98    }
99   
 
100  1 toggle public void testBoostsSimple() throws Exception {
101  1 Map boosts = new HashMap();
102  1 boosts.put("b", new Float(5));
103  1 boosts.put("t", new Float(10));
104  1 String[] fields = {"b", "t"};
105  1 MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new StandardAnalyzer(), boosts);
106   
107   
108    //Check for simple
109  1 Query q = mfqp.parse("one");
110  1 assertEquals("b:one^5.0 t:one^10.0", q.toString());
111   
112    //Check for AND
113  1 q = mfqp.parse("one AND two");
114  1 assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0)", q.toString());
115   
116    //Check for OR
117  1 q = mfqp.parse("one OR two");
118  1 assertEquals("(b:one^5.0 t:one^10.0) (b:two^5.0 t:two^10.0)", q.toString());
119   
120    //Check for AND and a field
121  1 q = mfqp.parse("one AND two AND foo:test");
122  1 assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0) +foo:test", q.toString());
123   
124  1 q = mfqp.parse("one^3 AND two^4");
125  1 assertEquals("+((b:one^5.0 t:one^10.0)^3.0) +((b:two^5.0 t:two^10.0)^4.0)", q.toString());
126    }
127   
 
128  1 toggle public void testStaticMethod1() throws ParseException {
129  1 String[] fields = {"b", "t"};
130  1 String[] queries = {"one", "two"};
131  1 Query q = MultiFieldQueryParser.parse(queries, fields, new StandardAnalyzer());
132  1 assertEquals("b:one t:two", q.toString());
133   
134  1 String[] queries2 = {"+one", "+two"};
135  1 q = MultiFieldQueryParser.parse(queries2, fields, new StandardAnalyzer());
136  1 assertEquals("(+b:one) (+t:two)", q.toString());
137   
138  1 String[] queries3 = {"one", "+two"};
139  1 q =