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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/FunctionRangeQParserPlugin.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
 
package org.apache.solr.search;
18
 
 
19
 
import org.apache.lucene.index.IndexReader;
20
 
import org.apache.lucene.queryParser.ParseException;
21
 
import org.apache.lucene.search.*;
22
 
import org.apache.solr.common.params.SolrParams;
23
 
import org.apache.solr.common.util.NamedList;
24
 
import org.apache.solr.core.SolrConfig;
25
 
import org.apache.solr.request.SolrQueryRequest;
26
 
import org.apache.solr.search.function.*;
27
 
 
28
 
import java.io.IOException;
29
 
import java.util.Map;
30
 
 
31
 
/**
32
 
 * Create a range query over a function.
33
 
 * <br>Other parameters:
34
 
 * <br><code>l</code>, the lower bound, optional)
35
 
 * <br><code>u</code>, the upper bound, optional)
36
 
 * <br><code>incl</code>, include the lower bound: true/false, optional, default=true
37
 
 * <br><code>incu</code>, include the upper bound: true/false, optional, default=true
38
 
 * <br>Example: <code>{!frange l=1000 u=50000}myfield</code>
39
 
 * <br>Filter query example: <code>fq={!frange l=0 u=2.2}sum(user_ranking,editor_ranking)</code> 
40
 
 */
41
 
public class FunctionRangeQParserPlugin extends QParserPlugin {
42
 
  public static String NAME = "frange";
43
 
 
44
 
  public void init(NamedList args) {
45
 
  }
46
 
 
47
 
  @Override
48
 
  public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
49
 
    return new QParser(qstr, localParams, params, req) {
50
 
      ValueSource vs;
51
 
      String funcStr;
52
 
 
53
 
      @Override
54
 
      public Query parse() throws ParseException {
55
 
        funcStr = localParams.get(QueryParsing.V, null);
56
 
        Query funcQ = subQuery(funcStr, FunctionQParserPlugin.NAME).getQuery();
57
 
        if (funcQ instanceof FunctionQuery) {
58
 
          vs = ((FunctionQuery)funcQ).getValueSource();
59
 
        } else {
60
 
          vs = new QueryValueSource(funcQ, 0.0f);
61
 
        }
62
 
 
63
 
        String l = localParams.get("l");
64
 
        String u = localParams.get("u");
65
 
        boolean includeLower = localParams.getBool("incl",true);
66
 
        boolean includeUpper = localParams.getBool("incu",true);
67
 
 
68
 
        // TODO: add a score=val option to allow score to be the value
69
 
        ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, l, u, includeLower, includeUpper);
70
 
        FunctionRangeQuery frq = new FunctionRangeQuery(rf);
71
 
        return frq;
72
 
      }
73
 
    };
74
 
  }
75
 
 
76
 
}
77
 
 
78
 
// This class works as either a normal constant score query, or as a PostFilter using a collector
79
 
class FunctionRangeQuery extends SolrConstantScoreQuery implements PostFilter {
80
 
  final ValueSourceRangeFilter rangeFilt;
81
 
 
82
 
  public FunctionRangeQuery(ValueSourceRangeFilter filter) {
83
 
    super(filter);
84
 
    this.rangeFilt = filter;
85
 
  }
86
 
 
87
 
  public DelegatingCollector getFilterCollector(IndexSearcher searcher) {
88
 
    Map fcontext = ValueSource.newContext();
89
 
    return new FunctionRangeCollector(fcontext);
90
 
  }
91
 
 
92
 
  class FunctionRangeCollector extends DelegatingCollector {
93
 
    final Map fcontext;
94
 
    ValueSourceScorer scorer;
95
 
    int maxdoc;
96
 
 
97
 
    public FunctionRangeCollector(Map fcontext) {
98
 
      this.fcontext = fcontext;
99
 
    }
100
 
 
101
 
    @Override
102
 
    public void collect(int doc) throws IOException {
103
 
      if (doc<maxdoc && scorer.matches(doc)) {
104
 
        delegate.collect(doc);
105
 
      }
106
 
    }
107
 
 
108
 
    @Override
109
 
    public void setNextReader(IndexReader reader, int docBase) throws IOException {
110
 
      maxdoc = reader.maxDoc();
111
 
      DocValues dv = rangeFilt.getValueSource().getValues(fcontext, reader);
112
 
      scorer = dv.getRangeScorer(reader, rangeFilt.getLowerVal(), rangeFilt.getUpperVal(), rangeFilt.isIncludeLower(), rangeFilt.isIncludeUpper());
113
 
      super.setNextReader(reader, docBase);
114
 
    }
115
 
  }
116
 
}