|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MultiFieldQueryParser | Line # 35 | 71 | 34 | 91.3% |
0.9126984
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (9) | |||
| Result | |||
|
0.515873
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testSimple
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testSimple
|
1 PASS | |
|
0.42063493
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testAnalyzerReturningNull
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testAnalyzerReturningNull
|
1 PASS | |
|
0.26984128
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testBoostsSimple
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testBoostsSimple
|
1 PASS | |
|
0.22222222
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStopWordSearching
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStopWordSearching
|
1 PASS | |
|
0.12698413
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod2Old
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod2Old
|
1 PASS | |
|
0.103174604
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod3
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod3
|
1 PASS | |
|
0.103174604
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod1
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod1
|
1 PASS | |
|
0.103174604
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod2
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod2
|
1 PASS | |
|
0.103174604
|
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod3Old
org.apache.lucene.queryParser.TestMultiFieldQueryParser.testStaticMethod3Old
|
1 PASS | |
| 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 org.apache.lucene.analysis.Analyzer; | |
| 20 | import org.apache.lucene.search.BooleanClause; | |
| 21 | import org.apache.lucene.search.BooleanQuery; | |
| 22 | import org.apache.lucene.search.MultiPhraseQuery; | |
| 23 | import org.apache.lucene.search.PhraseQuery; | |
| 24 | import org.apache.lucene.search.Query; | |
| 25 | ||
| 26 | import java.util.Vector; | |
| 27 | import java.util.Map; | |
| 28 | ||
| 29 | /** | |
| 30 | * A QueryParser which constructs queries to search multiple fields. | |
| 31 | * | |
| 32 | * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>, Daniel Naber | |
| 33 | * @version $Revision: 465081 $ | |
| 34 | */ | |
| 35 | public class MultiFieldQueryParser extends QueryParser | |
| 36 | { | |
| 37 | ||
| 38 | private String[] fields; | |
| 39 | private Map boosts; | |
| 40 | ||
| 41 | /** | |
| 42 | * Creates a MultiFieldQueryParser. | |
| 43 | * Allows passing of a map with term to Boost, and the boost to apply to each term. | |
| 44 | * | |
| 45 | * <p>It will, when parse(String query) | |
| 46 | * is called, construct a query like this (assuming the query consists of | |
| 47 | * two terms and you specify the two fields <code>title</code> and <code>body</code>):</p> | |
| 48 | * | |
| 49 | * <code> | |
| 50 | * (title:term1 body:term1) (title:term2 body:term2) | |
| 51 | * </code> | |
| 52 | * | |
| 53 | * <p>When setDefaultOperator(AND_OPERATOR) is set, the result will be:</p> | |
| 54 | * | |
| 55 | * <code> | |
| 56 | * +(title:term1 body:term1) +(title:term2 body:term2) | |
| 57 | * </code> | |
| 58 | * | |
| 59 | * <p>When you pass a boost (title=>5 body=>10) you can get </p> | |
| 60 | * | |
| 61 | * <code> | |
| 62 | * +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0) | |
| 63 | * </code> | |
| 64 | * | |
| 65 | * <p>In other words, all the query's terms must appear, but it doesn't matter in | |
| 66 | * what fields they appear.</p> | |
| 67 | */ | |
| 68 | 1 |
public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map boosts) { |
| 69 | 1 | this(fields,analyzer); |
| 70 | 1 | this.boosts = boosts; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Creates a MultiFieldQueryParser. | |
| 75 | * | |
| 76 | * <p>It will, when parse(String query) | |
| 77 | * is called, construct a query like this (assuming the query consists of | |
| 78 | * two terms and you specify the two fields <code>title</code> and <code>body</code>):</p> | |
| 79 | * | |
| 80 | * <code> | |
| 81 | * (title:term1 body:term1) (title:term2 body:term2) | |
| 82 | * </code> | |
| 83 | * | |
| 84 | * <p>When setDefaultOperator(AND_OPERATOR) is set, the result will be:</p> | |
| 85 | * | |
| 86 | * <code> | |
| 87 | * +(title:term1 body:term1) +(title:term2 body:term2) | |
| 88 | * </code> | |
| 89 | * | |
| 90 | * <p>In other words, all the query's terms must appear, but it doesn't matter in | |
| 91 | * what fields they appear.</p> | |
| 92 | */ | |
| 93 | 5 |
public MultiFieldQueryParser(String[] fields, Analyzer analyzer) { |
| 94 | 5 | super(null, analyzer); |
| 95 | 5 | this.fields = fields; |
| 96 | } | |
| 97 | ||
| 98 | 37 |
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException { |
| 99 | 37 | if (field == null) { |
| 100 | 35 | Vector clauses = new Vector(); |
| 101 | 105 | for (int i = 0; i < fields.length; i++) { |
| 102 | 70 | Query q = super.getFieldQuery(fields[i], queryText); |
| 103 | 70 | if (q != null) { |
| 104 | //If the user passes a map of boosts | |
| 105 | 67 | if (boosts != null) { |
| 106 | //Get the boost from the map and apply them | |
| 107 | 18 | Float boost = (Float)boosts.get(fields[i]); |
| 108 | 18 | if (boost != null) { |
| 109 | 18 | q.setBoost(boost.floatValue()); |
| 110 | } | |
| 111 | } | |
| 112 | 67 | if (q instanceof PhraseQuery) { |
| 113 | 12 | ((PhraseQuery) q).setSlop(slop); |
| 114 | } | |
| 115 | 67 | if (q instanceof MultiPhraseQuery) { |
| 116 | 0 | ((MultiPhraseQuery) q).setSlop(slop); |
| 117 | } | |
| 118 | 67 | clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD)); |
| 119 | } | |
| 120 | } | |
| 121 | 35 | if (clauses.size() == 0) // happens for stopwords |
| 122 | 1 | return null; |
| 123 | 34 | return getBooleanQuery(clauses, true); |
| 124 | } | |
| 125 | 2 | return super.getFieldQuery(field, queryText); |
| 126 | } | |
| 127 | ||
| 128 | ||
| 129 | 31 |
protected Query getFieldQuery(String field, String queryText) throws ParseException { |
| 130 | 31 | return getFieldQuery(field, queryText, 0); |
| 131 | } | |
| 132 | ||
| 133 | ||
| 134 | 3 |
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException |
| 135 | { | |
| 136 | 3 | if (field == null) { |
| 137 | 3 | Vector clauses = new Vector(); |
| 138 | 10 | for (int i = 0; i < fields.length; i++) { |
| 139 | 7 | clauses.add(new BooleanClause(super.getFuzzyQuery(fields[i], termStr, minSimilarity), |
| 140 | BooleanClause.Occur.SHOULD)); | |
| 141 | } | |
| 142 | 3 | return getBooleanQuery(clauses, true); |
| 143 | } | |
| 144 | 0 | return super.getFuzzyQuery(field, termStr, minSimilarity); |
| 145 | } | |
| 146 | ||
| 147 | 3 |
protected Query getPrefixQuery(String field, String termStr) throws ParseException |
| 148 | { | |
| 149 | 3 | if (field == null) { |
| 150 | 3 | Vector clauses = new Vector(); |
| 151 | 10 | for (int i = 0; i < fields.length; i++) { |
| 152 | 7 | clauses.add(new BooleanClause(super.getPrefixQuery(fields[i], termStr), |
| 153 | BooleanClause.Occur.SHOULD)); | |
| 154 | } | |
| 155 | ||