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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.solr.search;
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.*;
28
import java.io.IOException;
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>
41
public class FunctionRangeQParserPlugin extends QParserPlugin {
42
public static String NAME = "frange";
44
public void init(NamedList args) {
48
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
49
return new QParser(qstr, localParams, params, req) {
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();
60
vs = new QueryValueSource(funcQ, 0.0f);
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);
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);
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;
82
public FunctionRangeQuery(ValueSourceRangeFilter filter) {
84
this.rangeFilt = filter;
87
public DelegatingCollector getFilterCollector(IndexSearcher searcher) {
88
Map fcontext = ValueSource.newContext();
89
return new FunctionRangeCollector(fcontext);
92
class FunctionRangeCollector extends DelegatingCollector {
94
ValueSourceScorer scorer;
97
public FunctionRangeCollector(Map fcontext) {
98
this.fcontext = fcontext;
102
public void collect(int doc) throws IOException {
103
if (doc<maxdoc && scorer.matches(doc)) {
104
delegate.collect(doc);
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);