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.queryParser.ParseException;
20
import org.apache.lucene.search.Query;
21
import org.apache.solr.common.params.SolrParams;
22
import org.apache.solr.common.util.NamedList;
23
import org.apache.solr.request.SolrQueryRequest;
24
import org.apache.solr.search.function.BoostedQuery;
25
import org.apache.solr.search.function.FunctionQuery;
26
import org.apache.solr.search.function.QueryValueSource;
27
import org.apache.solr.search.function.ValueSource;
30
* Create a boosted query from the input value. The main value is the query to be boosted.
31
* <br>Other parameters: <code>b</code>, the function query to use as the boost.
32
* <p>Example: <code>{!boost b=log(popularity)}foo</code> creates a query "foo"
33
* which is boosted (scores are multiplied) by the function query <code>log(popularity)</code>.
34
* The query to be boosted may be of any type.
36
* <p>Example: <code>{!boost b=recip(ms(NOW,mydatefield),3.16e-11,1,1)}foo</code> creates a query "foo"
37
* which is boosted by the date boosting function referenced in {@link org.apache.solr.search.function.ReciprocalFloatFunction}
39
public class BoostQParserPlugin extends QParserPlugin {
40
public static String NAME = "boost";
41
public static String BOOSTFUNC = "b";
43
public void init(NamedList args) {
47
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
48
return new QParser(qstr, localParams, params, req) {
54
public Query parse() throws ParseException {
55
b = localParams.get(BOOSTFUNC);
56
baseParser = subQuery(localParams.get(QueryParsing.V), null);
57
Query q = baseParser.getQuery();
59
if (b == null) return q;
60
Query bq = subQuery(b, FunctionQParserPlugin.NAME).getQuery();
61
if (bq instanceof FunctionQuery) {
62
vs = ((FunctionQuery)bq).getValueSource();
64
vs = new QueryValueSource(bq, 0.0f);
66
return new BoostedQuery(q, vs);
71
public String[] getDefaultHighlightFields() {
72
return baseParser.getDefaultHighlightFields();
76
public Query getHighlightQuery() throws ParseException {
77
return baseParser.getHighlightQuery();
81
public void addDebugInfo(NamedList<Object> debugInfo) {
82
// encapsulate base debug info in a sub-list?
83
baseParser.addDebugInfo(debugInfo);
84
debugInfo.add("boost_str",b);
85
debugInfo.add("boost_parsed",vs);