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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/BoostQParserPlugin.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.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;
28
 
 
29
 
/**
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.
35
 
 *
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}
38
 
 */
39
 
public class BoostQParserPlugin extends QParserPlugin {
40
 
  public static String NAME = "boost";
41
 
  public static String BOOSTFUNC = "b";
42
 
 
43
 
  public void init(NamedList args) {
44
 
  }
45
 
 
46
 
  @Override
47
 
  public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
48
 
    return new QParser(qstr, localParams, params, req) {
49
 
      QParser baseParser;
50
 
      ValueSource vs;
51
 
      String b;
52
 
 
53
 
      @Override
54
 
      public Query parse() throws ParseException {
55
 
        b = localParams.get(BOOSTFUNC);
56
 
        baseParser = subQuery(localParams.get(QueryParsing.V), null);
57
 
        Query q = baseParser.getQuery();
58
 
 
59
 
        if (b == null) return q;
60
 
        Query bq = subQuery(b, FunctionQParserPlugin.NAME).getQuery();
61
 
        if (bq instanceof FunctionQuery) {
62
 
          vs = ((FunctionQuery)bq).getValueSource();
63
 
        } else {
64
 
          vs = new QueryValueSource(bq, 0.0f);
65
 
        }
66
 
        return new BoostedQuery(q, vs);
67
 
      }
68
 
 
69
 
 
70
 
      @Override
71
 
      public String[] getDefaultHighlightFields() {
72
 
        return baseParser.getDefaultHighlightFields();
73
 
      }
74
 
                                           
75
 
      @Override
76
 
      public Query getHighlightQuery() throws ParseException {
77
 
        return baseParser.getHighlightQuery();
78
 
      }
79
 
 
80
 
      @Override
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);
86
 
      }
87
 
    };
88
 
  }
89
 
 
90
 
}