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;
|
2014-10-20 12:28:28 +02:00
|
|
|
import org.apache.lucene.index.Term;
|
|
|
|
import org.apache.lucene.search.*;
|
2014-02-14 10:39:38 -07:00
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class for Lucene's SimpleQueryParser that allows us to redefine
|
|
|
|
* different types of queries.
|
|
|
|
*/
|
2014-02-18 21:45:16 +01:00
|
|
|
public class SimpleQueryParser extends org.apache.lucene.queryparser.simple.SimpleQueryParser {
|
2014-02-14 10:39:38 -07:00
|
|
|
|
2014-02-18 15:46:25 -07:00
|
|
|
private final Settings settings;
|
2014-02-14 10:39:38 -07:00
|
|
|
|
|
|
|
/** Creates a new parser with custom flags used to enable/disable certain features. */
|
2014-02-18 15:46:25 -07:00
|
|
|
public SimpleQueryParser(Analyzer analyzer, Map<String, Float> weights, int flags, Settings settings) {
|
2014-02-14 10:39:38 -07:00
|
|
|
super(analyzer, weights, flags);
|
2014-02-18 15:46:25 -07:00
|
|
|
this.settings = settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rethrow the runtime exception, unless the lenient flag has been set, returns null
|
|
|
|
*/
|
|
|
|
private Query rethrowUnlessLenient(RuntimeException e) {
|
|
|
|
if (settings.lenient()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Query newDefaultQuery(String text) {
|
2014-10-20 12:28:28 +02:00
|
|
|
BooleanQuery bq = new BooleanQuery(true);
|
|
|
|
for (Map.Entry<String,Float> entry : weights.entrySet()) {
|
|
|
|
try {
|
|
|
|
Query q = createBooleanQuery(entry.getKey(), text, super.getDefaultOperator());
|
|
|
|
if (q != null) {
|
|
|
|
q.setBoost(entry.getValue());
|
|
|
|
bq.add(q, BooleanClause.Occur.SHOULD);
|
|
|
|
}
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
rethrowUnlessLenient(e);
|
|
|
|
}
|
2014-02-18 15:46:25 -07:00
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
return super.simplify(bq);
|
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) {
|
2014-02-18 15:46:25 -07:00
|
|
|
if (settings.lowercaseExpandedTerms()) {
|
|
|
|
text = text.toLowerCase(settings.locale());
|
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
BooleanQuery bq = new BooleanQuery(true);
|
|
|
|
for (Map.Entry<String,Float> entry : weights.entrySet()) {
|
|
|
|
try {
|
|
|
|
Query q = new FuzzyQuery(new Term(entry.getKey(), text), fuzziness);
|
|
|
|
if (q != null) {
|
|
|
|
q.setBoost(entry.getValue());
|
|
|
|
bq.add(q, BooleanClause.Occur.SHOULD);
|
|
|
|
}
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
rethrowUnlessLenient(e);
|
|
|
|
}
|
2014-02-18 15:46:25 -07:00
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
return super.simplify(bq);
|
2014-02-18 15:46:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Query newPhraseQuery(String text, int slop) {
|
2014-10-20 12:28:28 +02:00
|
|
|
BooleanQuery bq = new BooleanQuery(true);
|
|
|
|
for (Map.Entry<String,Float> entry : weights.entrySet()) {
|
|
|
|
try {
|
|
|
|
Query q = createPhraseQuery(entry.getKey(), text, slop);
|
|
|
|
if (q != null) {
|
|
|
|
q.setBoost(entry.getValue());
|
|
|
|
bq.add(q, BooleanClause.Occur.SHOULD);
|
|
|
|
}
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
rethrowUnlessLenient(e);
|
|
|
|
}
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
return super.simplify(bq);
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches to Lucene's SimpleQueryParser's newPrefixQuery, optionally
|
|
|
|
* lowercasing the term first
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Query newPrefixQuery(String text) {
|
2014-02-18 15:46:25 -07:00
|
|
|
if (settings.lowercaseExpandedTerms()) {
|
|
|
|
text = text.toLowerCase(settings.locale());
|
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
BooleanQuery bq = new BooleanQuery(true);
|
|
|
|
for (Map.Entry<String,Float> entry : weights.entrySet()) {
|
|
|
|
try {
|
|
|
|
PrefixQuery prefix = new PrefixQuery(new Term(entry.getKey(), text));
|
|
|
|
prefix.setBoost(entry.getValue());
|
|
|
|
bq.add(prefix, BooleanClause.Occur.SHOULD);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
return rethrowUnlessLenient(e);
|
|
|
|
}
|
2014-02-18 15:46:25 -07:00
|
|
|
}
|
2014-10-20 12:28:28 +02:00
|
|
|
return super.simplify(bq);
|
2014-02-18 15:46:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class encapsulating the settings for the SimpleQueryString query, with
|
|
|
|
* their default values
|
|
|
|
*/
|
|
|
|
public static class Settings {
|
|
|
|
private Locale locale = Locale.ROOT;
|
|
|
|
private boolean lowercaseExpandedTerms = true;
|
|
|
|
private boolean lenient = false;
|
|
|
|
|
|
|
|
public Settings() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void locale(Locale locale) {
|
|
|
|
this.locale = locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Locale locale() {
|
|
|
|
return this.locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void lowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
|
|
|
|
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean lowercaseExpandedTerms() {
|
|
|
|
return this.lowercaseExpandedTerms;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void lenient(boolean lenient) {
|
|
|
|
this.lenient = lenient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean lenient() {
|
|
|
|
return this.lenient;
|
2014-02-14 10:39:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|