2014-02-14 10:39:38 -07:00
|
|
|
/*
|
2014-02-14 11:55:49 -07:00
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch licenses this file to you under
|
|
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2014-02-14 10:39:38 -07:00
|
|
|
*
|
2014-02-14 11:55:49 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-02-14 10:39:38 -07:00
|
|
|
*
|
2014-02-14 11:55:49 -07:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-02-14 10:39:38 -07:00
|
|
|
*/
|
|
|
|
package org.elasticsearch.index.query;
|
|
|
|
|
|
|
|
import org.apache.lucene.analysis.Analyzer;
|
|
|
|
import org.apache.lucene.queryparser.XSimpleQueryParser;
|
|
|
|
import org.apache.lucene.search.Query;
|
|
|
|
import org.elasticsearch.ElasticsearchIllegalStateException;
|
|
|
|
import org.elasticsearch.index.mapper.FieldMapper;
|
|
|
|
import org.elasticsearch.index.mapper.MapperService;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameQuery;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class for Lucene's SimpleQueryParser that allows us to redefine
|
|
|
|
* different types of queries.
|
|
|
|
*/
|
|
|
|
public class SimpleQueryParser extends XSimpleQueryParser {
|
|
|
|
|
|
|
|
private final boolean lowercaseExpandedTerms;
|
2014-02-14 12:34:16 -07:00
|
|
|
private final Locale locale;
|
2014-02-14 10:39:38 -07:00
|
|
|
|
|
|
|
/** Creates a new parser with custom flags used to enable/disable certain features. */
|
2014-02-14 12:34:16 -07:00
|
|
|
public SimpleQueryParser(Analyzer analyzer, Map<String, Float> weights, int flags, Locale locale, boolean lowercaseExpandedTerms) {
|
2014-02-14 10:39:38 -07:00
|
|
|
super(analyzer, weights, flags);
|
|
|
|
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
|
2014-02-14 12:34:16 -07:00
|
|
|
this.locale = locale;
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally
|
|
|
|
* lowercasing the term first
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Query newFuzzyQuery(String text, int fuzziness) {
|
|
|
|
if (lowercaseExpandedTerms) {
|
2014-02-14 12:34:16 -07:00
|
|
|
text = text.toLowerCase(locale);
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
|
|
|
return super.newFuzzyQuery(text, fuzziness);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches to Lucene's SimpleQueryParser's newPrefixQuery, optionally
|
|
|
|
* lowercasing the term first
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Query newPrefixQuery(String text) {
|
|
|
|
if (lowercaseExpandedTerms) {
|
2014-02-14 12:34:16 -07:00
|
|
|
text = text.toLowerCase(locale);
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
|
|
|
return super.newPrefixQuery(text);
|
|
|
|
}
|
|
|
|
}
|