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.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;
30
import java.util.List;
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>
37
* <br>Example: <code>{!lucene q.op=AND df=text sort='price asc'}myfield:foo +bar -baz</code>
39
public class LuceneQParserPlugin extends QParserPlugin {
40
public static String NAME = "lucene";
42
public void init(NamedList args) {
46
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
47
return new LuceneQParser(qstr, localParams, params, req);
51
class LuceneQParser extends QParser {
53
SolrQueryParser lparser;
55
public LuceneQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
56
super(qstr, localParams, params, req);
61
public Query parse() throws ParseException {
62
String qstr = getString();
64
String defaultField = getParam(CommonParams.DF);
65
if (defaultField==null) {
66
defaultField = getReq().getSchema().getDefaultSearchFieldName();
68
lparser = new SolrQueryParser(this, defaultField);
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);
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);
80
return lparser.parse(qstr);
85
public String[] getDefaultHighlightFields() {
86
return new String[]{lparser.getField()};
92
class OldLuceneQParser extends LuceneQParser {
95
public OldLuceneQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
96
super(qstr, localParams, params, req);
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);
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.");
122
return super.parse();
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 ) {