~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/FunctionQuery.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.search.function;
19
 
 
20
 
import org.apache.lucene.index.IndexReader;
21
 
import org.apache.lucene.search.*;
22
 
import org.apache.solr.search.SolrIndexReader;
23
 
 
24
 
import java.io.IOException;
25
 
import java.util.Set;
26
 
import java.util.Map;
27
 
 
28
 
 
29
 
/**
30
 
 * Returns a score for each document based on a ValueSource,
31
 
 * often some function of the value of a field.
32
 
 *
33
 
 * <b>Note: This API is experimental and may change in non backward-compatible ways in the future</b>
34
 
 *
35
 
 * @version $Id: FunctionQuery.java 1065312 2011-01-30 16:08:25Z rmuir $
36
 
 */
37
 
public class FunctionQuery extends Query {
38
 
  ValueSource func;
39
 
 
40
 
  /**
41
 
   * @param func defines the function to be used for scoring
42
 
   */
43
 
  public FunctionQuery(ValueSource func) {
44
 
    this.func=func;
45
 
  }
46
 
 
47
 
  /** @return The associated ValueSource */
48
 
  public ValueSource getValueSource() {
49
 
    return func;
50
 
  }
51
 
 
52
 
  @Override
53
 
  public Query rewrite(IndexReader reader) throws IOException {
54
 
    return this;
55
 
  }
56
 
 
57
 
  @Override
58
 
  public void extractTerms(Set terms) {}
59
 
 
60
 
  protected class FunctionWeight extends Weight {
61
 
    protected Searcher searcher;
62
 
    protected float queryNorm;
63
 
    protected float queryWeight;
64
 
    protected Map context;
65
 
 
66
 
    public FunctionWeight(Searcher searcher) throws IOException {
67
 
      this.searcher = searcher;
68
 
      this.context = func.newContext();
69
 
      func.createWeight(context, searcher);
70
 
    }
71
 
 
72
 
    @Override
73
 
    public Query getQuery() {
74
 
      return FunctionQuery.this;
75
 
    }
76
 
 
77
 
    @Override
78
 
    public float getValue() {
79
 
      return queryWeight;
80
 
    }
81
 
 
82
 
    @Override
83
 
    public float sumOfSquaredWeights() throws IOException {
84
 
      queryWeight = getBoost();
85
 
      return queryWeight * queryWeight;
86
 
    }
87
 
 
88
 
    @Override
89
 
    public void normalize(float norm) {
90
 
      this.queryNorm = norm;
91
 
      queryWeight *= this.queryNorm;
92
 
    }
93
 
 
94
 
    @Override
95
 
    public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
96
 
      return new AllScorer(getSimilarity(searcher), reader, this);
97
 
    }
98
 
 
99
 
    @Override
100
 
    public Explanation explain(IndexReader reader, int doc) throws IOException {
101
 
      SolrIndexReader topReader = (SolrIndexReader)reader;
102
 
      SolrIndexReader[] subReaders = topReader.getLeafReaders();
103
 
      int[] offsets = topReader.getLeafOffsets();
104
 
      int readerPos = SolrIndexReader.readerIndex(doc, offsets);
105
 
      int readerBase = offsets[readerPos];
106
 
      return ((AllScorer)scorer(subReaders[readerPos], true, true)).explain(doc-readerBase);
107
 
    }
108
 
  }
109
 
 
110
 
  protected class AllScorer extends Scorer {
111
 
    final IndexReader reader;
112
 
    final FunctionWeight weight;
113
 
    final int maxDoc;
114
 
    final float qWeight;
115
 
    int doc=-1;
116
 
    final DocValues vals;
117
 
    final boolean hasDeletions;
118
 
 
119
 
    public AllScorer(Similarity similarity, IndexReader reader, FunctionWeight w) throws IOException {
120
 
      super(similarity, w);
121
 
      this.weight = w;
122
 
      this.qWeight = w.getValue();
123
 
      this.reader = reader;
124
 
      this.maxDoc = reader.maxDoc();
125
 
      this.hasDeletions = reader.hasDeletions();
126
 
      vals = func.getValues(weight.context, reader);
127
 
    }
128
 
 
129
 
    @Override
130
 
    public int docID() {
131
 
      return doc;
132
 
    }
133
 
 
134
 
    @Override
135
 
    // instead of matching all docs, we could also embed a query.
136
 
    // the score could either ignore the subscore, or boost it.
137
 
    // Containment:  floatline(foo:myTerm, "myFloatField", 1.0, 0.0f)
138
 
    // Boost:        foo:myTerm^floatline("myFloatField",1.0,0.0f)
139
 
    public int nextDoc() throws IOException {
140
 
      for(;;) {
141
 
        ++doc;
142
 
        if (doc>=maxDoc) {
143
 
          return doc=NO_MORE_DOCS;
144
 
        }
145
 
        if (hasDeletions && reader.isDeleted(doc)) continue;
146
 
        return doc;
147
 
      }
148
 
    }
149
 
 
150
 
    @Override
151
 
    public int advance(int target) throws IOException {
152
 
      // this will work even if target==NO_MORE_DOCS
153
 
      doc=target-1;
154
 
      return nextDoc();
155
 
    }
156
 
 
157
 
    // instead of matching all docs, we could also embed a query.
158
 
    // the score could either ignore the subscore, or boost it.
159
 
    // Containment:  floatline(foo:myTerm, "myFloatField", 1.0, 0.0f)
160
 
    // Boost:        foo:myTerm^floatline("myFloatField",1.0,0.0f)
161
 
    public boolean next() throws IOException {
162
 
      for(;;) {
163
 
        ++doc;
164
 
        if (doc>=maxDoc) {
165
 
          return false;
166
 
        }
167
 
        if (hasDeletions && reader.isDeleted(doc)) continue;
168
 
        // todo: maybe allow score() to throw a specific exception
169
 
        // and continue on to the next document if it is thrown...
170
 
        // that may be useful, but exceptions aren't really good
171
 
        // for flow control.
172
 
        return true;
173
 
      }
174
 
    }
175
 
 
176
 
    public int doc() {
177
 
      return doc;
178
 
    }
179
 
 
180
 
    @Override
181
 
    public float score() throws IOException {
182
 
      float score = qWeight * vals.floatVal(doc);
183
 
 
184
 
      // Current Lucene priority queues can't handle NaN and -Infinity, so
185
 
      // map to -Float.MAX_VALUE. This conditional handles both -infinity
186
 
      // and NaN since comparisons with NaN are always false.
187
 
      return score>Float.NEGATIVE_INFINITY ? score : -Float.MAX_VALUE;
188
 
    }
189
 
 
190
 
    public boolean skipTo(int target) throws IOException {
191
 
      doc=target-1;
192
 
      return next();
193
 
    }
194
 
 
195
 
    public Explanation explain(int doc) throws IOException {
196
 
      float sc = qWeight * vals.floatVal(doc);
197
 
 
198
 
      Explanation result = new ComplexExplanation
199
 
        (true, sc, "FunctionQuery(" + func + "), product of:");
200
 
 
201
 
      result.addDetail(vals.explain(doc));
202
 
      result.addDetail(new Explanation(getBoost(), "boost"));
203
 
      result.addDetail(new Explanation(weight.queryNorm,"queryNorm"));
204
 
      return result;
205
 
    }
206
 
  }
207
 
 
208
 
 
209
 
  @Override
210
 
  public Weight createWeight(Searcher searcher) throws IOException {
211
 
    return new FunctionQuery.FunctionWeight(searcher);
212
 
  }
213
 
 
214
 
 
215
 
  /** Prints a user-readable version of this query. */
216
 
  @Override
217
 
  public String toString(String field)
218
 
  {
219
 
    float boost = getBoost();
220
 
    return (boost!=1.0?"(":"") + func.toString()
221
 
            + (boost==1.0 ? "" : ")^"+boost);
222
 
  }
223
 
 
224
 
 
225
 
  /** Returns true if <code>o</code> is equal to this. */
226
 
  @Override
227
 
  public boolean equals(Object o) {
228
 
    if (FunctionQuery.class != o.getClass()) return false;
229
 
    FunctionQuery other = (FunctionQuery)o;
230
 
    return this.getBoost() == other.getBoost()
231
 
            && this.func.equals(other.func);
232
 
  }
233
 
 
234
 
  /** Returns a hash code value for this object. */
235
 
  @Override
236
 
  public int hashCode() {
237
 
    return func.hashCode()*31 + Float.floatToIntBits(getBoost());
238
 
  }
239
 
 
240
 
}