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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.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.queryParser.QueryParser;
21
 
import org.apache.lucene.search.Query;
22
 
import org.apache.lucene.search.Sort;
23
 
import org.apache.solr.common.SolrException;
24
 
import org.apache.solr.common.params.CommonParams;
25
 
import org.apache.solr.common.params.SolrParams;
26
 
import org.apache.solr.common.util.NamedList;
27
 
import org.apache.solr.common.util.StrUtils;
28
 
import org.apache.solr.request.SolrQueryRequest;
29
 
 
30
 
import java.util.List;
31
 
/**
32
 
 * Parse Solr's variant on the Lucene QueryParser syntax.
33
 
 * <br>Other parameters:<ul>
34
 
 * <li>q.op - the default operator "OR" or "AND"</li>
35
 
 * <li>df - the default field name</li>
36
 
 * </ul>
37
 
 * <br>Example: <code>{!lucene q.op=AND df=text sort='price asc'}myfield:foo +bar -baz</code>
38
 
 */
39
 
public class LuceneQParserPlugin extends QParserPlugin {
40
 
  public static String NAME = "lucene";
41
 
 
42
 
  public void init(NamedList args) {
43
 
  }
44
 
 
45
 
  @Override
46
 
  public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
47
 
    return new LuceneQParser(qstr, localParams, params, req);
48
 
  }
49
 
}
50
 
 
51
 
class LuceneQParser extends QParser {
52
 
  String sortStr;
53
 
  SolrQueryParser lparser;
54
 
 
55
 
  public LuceneQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
56
 
    super(qstr, localParams, params, req);
57
 
  }
58
 
 
59
 
 
60
 
  @Override
61
 
  public Query parse() throws ParseException {
62
 
    String qstr = getString();
63
 
 
64
 
    String defaultField = getParam(CommonParams.DF);
65
 
    if (defaultField==null) {
66
 
      defaultField = getReq().getSchema().getDefaultSearchFieldName();
67
 
    }
68
 
    lparser = new SolrQueryParser(this, defaultField);
69
 
 
70
 
    // these could either be checked & set here, or in the SolrQueryParser constructor
71
 
    String opParam = getParam(QueryParsing.OP);
72
 
    if (opParam != null) {
73
 
      lparser.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR);
74
 
    } else {
75
 
      // try to get default operator from schema
76
 
      QueryParser.Operator operator = getReq().getSchema().getSolrQueryParser(null).getDefaultOperator();
77
 
      lparser.setDefaultOperator(null == operator ? QueryParser.Operator.OR : operator);
78
 
    }
79
 
 
80
 
    return lparser.parse(qstr);
81
 
  }
82
 
 
83
 
 
84
 
  @Override
85
 
  public String[] getDefaultHighlightFields() {
86
 
    return new String[]{lparser.getField()};
87
 
  }
88
 
  
89
 
}
90
 
 
91
 
 
92
 
class OldLuceneQParser extends LuceneQParser {
93
 
  String sortStr;
94
 
 
95
 
  public OldLuceneQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
96
 
    super(qstr, localParams, params, req);
97
 
  }
98
 
 
99
 
  @Override
100
 
  public Query parse() throws ParseException {
101
 
    // handle legacy "query;sort" syntax
102
 
    if (getLocalParams() == null) {
103
 
      String qstr = getString();
104
 
      sortStr = getParams().get(CommonParams.SORT);
105
 
      if (sortStr == null) {
106
 
        // sort may be legacy form, included in the query string
107
 
        List<String> commands = StrUtils.splitSmart(qstr,';');
108
 
        if (commands.size() == 2) {
109
 
          qstr = commands.get(0);
110
 
          sortStr = commands.get(1);
111
 
        } else if (commands.size() == 1) {
112
 
          // This is need to support the case where someone sends: "q=query;"
113
 
          qstr = commands.get(0);
114
 
        }
115
 
        else if (commands.size() > 2) {
116
 
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "If you want to use multiple ';' in the query, use the 'sort' param.");
117
 
        }
118
 
      }
119
 
      setString(qstr);
120
 
    }
121
 
 
122
 
    return super.parse();
123
 
  }
124
 
 
125
 
  @Override
126
 
  public SortSpec getSort(boolean useGlobal) throws ParseException {
127
 
    SortSpec sort = super.getSort(useGlobal);
128
 
    if (sortStr != null && sortStr.length()>0 && sort.getSort()==null) {
129
 
      Sort oldSort = QueryParsing.parseSort(sortStr, getReq());
130
 
      if( oldSort != null ) {
131
 
        sort.sort = oldSort;
132
 
      }
133
 
    }
134
 
    return sort;
135
 
  }
136
 
 
137
 
}
138