1 package org.apache.lucene.queryParser.surround.query;
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 SpanNearClauseFactory:
24 - create for a field name and an indexreader.
27 this should add a corresponding SpanTermQuery, or
28 increase the weight of an existing one.
30 - add a weighted subquery SpanNearQuery
32 - create a clause for SpanNearQuery from the things added above.
33 For this, create an array of SpanQuery's from the added ones.
34 The clause normally is a SpanOrQuery over the added subquery SpanNearQuery
35 the SpanTermQuery's for the added Term's
38 /* When it is necessary to suppress double subqueries as much as possible:
39 hashCode() and equals() on unweighted SpanQuery are needed (possibly via getTerms(),
40 the terms are individually hashable).
41 Idem SpanNearQuery: hash on the subqueries and the slop.
42 Evt. merge SpanNearQuery's by adding the weights of the corresponding subqueries.
46 Are SpanQuery weights handled correctly during search by Lucene?
47 Should the resulting SpanOrQuery be sorted?
48 Could other SpanQueries be added for use in this factory:
49 - SpanOrQuery: in principle yes, but it only has access to it's terms
50 via getTerms(); are the corresponding weights available?
51 - SpanFirstQuery: treat similar to subquery SpanNearQuery. (ok?)
52 - SpanNotQuery: treat similar to subquery SpanNearQuery. (ok?)
55 import java.io.IOException;
56 import java.util.HashMap;
57 import java.util.Iterator;
59 import org.apache.lucene.index.IndexReader;
60 import org.apache.lucene.index.Term;
61 import org.apache.lucene.index.TermEnum;
62 import org.apache.lucene.search.Query;
63 import org.apache.lucene.search.spans.SpanNearQuery;
64 import org.apache.lucene.search.spans.SpanOrQuery;
65 import org.apache.lucene.search.spans.SpanQuery;
66 import org.apache.lucene.search.spans.SpanTermQuery;
69 public class SpanNearClauseFactory {
70 public SpanNearClauseFactory(IndexReader reader, String fieldName, BasicQueryFactory qf) {
72 this.fieldName = fieldName;
73 this.weightBySpanQuery = new HashMap<SpanQuery, Float>();
76 private IndexReader reader;
77 private String fieldName;
78 private HashMap<SpanQuery, Float> weightBySpanQuery;
79 private BasicQueryFactory qf;
81 public IndexReader getIndexReader() {return reader;}
83 public String getFieldName() {return fieldName;}
85 public BasicQueryFactory getBasicQueryFactory() {return qf;}
87 public TermEnum getTermEnum(String termText) throws IOException {
88 return getIndexReader().terms(new Term(getFieldName(), termText));
91 public int size() {return weightBySpanQuery.size();}
93 public void clear() {weightBySpanQuery.clear();}
95 protected void addSpanQueryWeighted(SpanQuery sq, float weight) {
96 Float w = weightBySpanQuery.get(sq);
98 w = Float.valueOf(w.floatValue() + weight);
100 w = Float.valueOf(weight);
101 weightBySpanQuery.put(sq, w);
104 public void addTermWeighted(Term t, float weight) throws IOException {
105 SpanTermQuery stq = qf.newSpanTermQuery(t);
106 /* CHECKME: wrap in Hashable...? */
107 addSpanQueryWeighted(stq, weight);
110 public void addSpanNearQuery(Query q) {
111 if (q == SrndQuery.theEmptyLcnQuery)
113 if (! (q instanceof SpanNearQuery))
114 throw new AssertionError("Expected SpanNearQuery: " + q.toString(getFieldName()));
115 /* CHECKME: wrap in Hashable...? */
116 addSpanQueryWeighted((SpanNearQuery)q, q.getBoost());
119 public SpanQuery makeSpanNearClause() {
120 SpanQuery [] spanQueries = new SpanQuery[size()];
121 Iterator<SpanQuery> sqi = weightBySpanQuery.keySet().iterator();
123 while (sqi.hasNext()) {
124 SpanQuery sq = sqi.next();
125 sq.setBoost(weightBySpanQuery.get(sq).floatValue());
126 spanQueries[i++] = sq;
129 if (spanQueries.length == 1)
130 return spanQueries[0];
132 return new SpanOrQuery(spanQueries);