Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart9.png 37% of files have more coverage
121   326   46   6.05
54   209   0.38   10
20     2.3  
2    
 
  MultiPhraseQuery       Line # 40 61 30 88% 0.8796296
  MultiPhraseQuery.MultiPhraseWeight       Line # 127 60 16 94.3% 0.9425287
 
  (20)
 
1    package org.apache.lucene.search;
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 java.io.IOException;
20    import java.util.*;
21   
22    import org.apache.lucene.index.IndexReader;
23    import org.apache.lucene.index.MultipleTermPositions;
24    import org.apache.lucene.index.Term;
25    import org.apache.lucene.index.TermPositions;
26    import org.apache.lucene.search.Query;
27    import org.apache.lucene.util.ToStringUtils;
28   
29    /**
30    * MultiPhraseQuery is a generalized version of PhraseQuery, with an added
31    * method {@link #add(Term[])}.
32    * To use this class, to search for the phrase "Microsoft app*" first use
33    * add(Term) on the term "Microsoft", then find all terms that have "app" as
34    * prefix using IndexReader.terms(Term), and use MultiPhraseQuery.add(Term[]
35    * terms) to add them to the query.
36    *
37    * @author Anders Nielsen
38    * @version 1.0
39    */
 
40    public class MultiPhraseQuery extends Query {
41    private String field;
42    private ArrayList termArrays = new ArrayList();
43    private Vector positions = new Vector();
44   
45    private int slop = 0;
46   
47    /** Sets the phrase slop for this query.
48    * @see PhraseQuery#setSlop(int)
49    */
 
50  27 toggle public void setSlop(int s) { slop = s; }
51   
52    /** Sets the phrase slop for this query.
53    * @see PhraseQuery#getSlop()
54    */
 
55  0 toggle public int getSlop() { return slop; }
56   
57    /** Add a single term at the next position in the phrase.
58    * @see PhraseQuery#add(Term)
59    */
 
60  8 toggle public void add(Term term) { add(new Term[]{term}); }
61   
62    /** Add multiple terms at the next position in the phrase. Any of the terms
63    * may match.
64    *
65    * @see PhraseQuery#add(Term)
66    */
 
67  67 toggle public void add(Term[] terms) {
68  67 int position = 0;
69  67 if (positions.size() > 0)
70  34 position = ((Integer) positions.lastElement()).intValue() + 1;
71   
72  67 add(terms, position);
73    }
74   
75    /**
76    * Allows to specify the relative position of terms within the phrase.
77    *
78    * @see PhraseQuery#add(Term, int)
79    * @param terms
80    * @param position
81    */
 
82  67 toggle public void add(Term[] terms, int position) {
83  67 if (termArrays.size() == 0)
84  33 field = terms[0].field();
85   
86  168 for (int i = 0; i < terms.length; i++) {
87  102 if (terms[i].field() != field) {
88  1 throw new IllegalArgumentException(
89    "All phrase terms must be in the same field (" + field + "): "
90    + terms[i]);
91    }
92    }
93   
94  66 termArrays.add(terms);
95  66 positions.addElement(new Integer(position));
96    }
97   
98    /**
99    * Retu