From 471801b484bc4374448a42f7a25e2a1554cb27a1 Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 12 Mar 2010 16:28:37 +0200 Subject: [PATCH] query/filter json parsing to allow passing numbers/booleans as strings and not native json types --- .idea/dictionaries/kimchy.xml | 1 + .../http/netty/NettyHttpRequest.java | 7 +- .../index/query/json/BoolJsonQueryParser.java | 21 +++-- .../ConstantScoreQueryJsonQueryParser.java | 6 +- .../query/json/DisMaxJsonQueryParser.java | 18 ++++- .../query/json/FieldJsonQueryParser.java | 79 +++++++++++++------ .../json/FilteredQueryJsonQueryParser.java | 6 +- .../index/query/json/JsonFilterParser.java | 2 +- .../index/query/json/JsonQueryParser.java | 2 +- .../query/json/MatchAllJsonQueryParser.java | 8 +- .../MoreLikeThisFieldJsonQueryParser.java | 19 +++++ .../json/MoreLikeThisJsonQueryParser.java | 19 +++++ .../query/json/PrefixJsonQueryParser.java | 12 ++- .../json/QueryStringJsonQueryParser.java | 21 +++++ .../query/json/RangeJsonFilterParser.java | 5 ++ .../query/json/RangeJsonQueryParser.java | 13 ++- .../query/json/SpanFirstJsonQueryParser.java | 12 ++- .../query/json/SpanNearJsonQueryParser.java | 17 +++- .../query/json/SpanNotJsonQueryParser.java | 8 +- .../query/json/SpanOrJsonQueryParser.java | 6 +- .../query/json/SpanTermJsonQueryParser.java | 6 +- .../index/query/json/TermJsonQueryParser.java | 6 +- .../query/json/WildcardJsonQueryParser.java | 6 +- .../java/org/elasticsearch/util/Booleans.java | 40 ++++++++++ .../util/settings/ImmutableSettings.java | 10 +-- 25 files changed, 283 insertions(+), 67 deletions(-) create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/util/Booleans.java diff --git a/.idea/dictionaries/kimchy.xml b/.idea/dictionaries/kimchy.xml index 3498386bb8f..956dfca47f7 100644 --- a/.idea/dictionaries/kimchy.xml +++ b/.idea/dictionaries/kimchy.xml @@ -7,6 +7,7 @@ banon birthdate bool + booleans checksum commitable committable diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java index fca52f95d99..69632b3a608 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java @@ -22,6 +22,7 @@ package org.elasticsearch.http.netty; import org.apache.lucene.util.UnicodeUtil; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.http.HttpRequest; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.SizeValue; import org.elasticsearch.util.TimeValue; import org.jboss.netty.buffer.ChannelBuffer; @@ -144,11 +145,7 @@ public class NettyHttpRequest implements HttpRequest { } @Override public boolean paramAsBoolean(String key, boolean defaultValue) { - String sValue = param(key); - if (sValue == null) { - return defaultValue; - } - return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off")); + return Booleans.parseBoolean(param(key), defaultValue); } @Override public Boolean paramAsBoolean(String key, Boolean defaultValue) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BoolJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BoolJsonQueryParser.java index b1db2746e5e..5b385f0dde3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BoolJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BoolJsonQueryParser.java @@ -29,6 +29,7 @@ import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import java.io.IOException; @@ -38,7 +39,7 @@ import static com.google.common.collect.Lists.*; import static org.elasticsearch.util.lucene.search.Queries.*; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class BoolJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -93,12 +94,22 @@ public class BoolJsonQueryParser extends AbstractIndexComponent implements JsonQ } else if (token == JsonToken.VALUE_NUMBER_INT) { if ("disableCoord".equals(currentFieldName)) { disableCoord = jp.getIntValue() != 0; - } - } else { - if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); } else if ("minimumNumberShouldMatch".equals(currentFieldName)) { minimumNumberShouldMatch = jp.getIntValue(); + } else if ("boost".equals(currentFieldName)) { + boost = jp.getIntValue(); + } + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { + if ("boost".equals(currentFieldName)) { + boost = jp.getFloatValue(); + } + } else if (token == JsonToken.VALUE_STRING) { + if ("disableCoord".equals(currentFieldName)) { + disableCoord = Booleans.parseBoolean(jp.getText(), false); + } else if ("minimumNumberShouldMatch".equals(currentFieldName)) { + minimumNumberShouldMatch = Integer.parseInt(jp.getText()); + } else if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/ConstantScoreQueryJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/ConstantScoreQueryJsonQueryParser.java index 9eb9c5a40ed..6c4786f8fe8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/ConstantScoreQueryJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/ConstantScoreQueryJsonQueryParser.java @@ -34,7 +34,7 @@ import org.elasticsearch.util.settings.Settings; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class ConstantScoreQueryJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -67,6 +67,10 @@ public class ConstantScoreQueryJsonQueryParser extends AbstractIndexComponent im if ("boost".equals(currentFieldName)) { boost = jp.getFloatValue(); } + } else if (token == JsonToken.VALUE_STRING) { + if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } } } if (filter == null) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/DisMaxJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/DisMaxJsonQueryParser.java index 5d3d483c3b2..74b2a5aeb86 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/DisMaxJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/DisMaxJsonQueryParser.java @@ -74,11 +74,23 @@ public class DisMaxJsonQueryParser extends AbstractIndexComponent implements Jso } } else { if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } else if ("tieBreakerMultiplier".equals(currentFieldName)) { - tieBreakerMultiplier = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + tieBreakerMultiplier = Float.parseFloat(jp.getText()); + } else { + tieBreakerMultiplier = jp.getFloatValue(); + } } else if ("tieBreaker".equals(currentFieldName)) { - tieBreakerMultiplier = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + tieBreakerMultiplier = Float.parseFloat(jp.getText()); + } else { + tieBreakerMultiplier = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FieldJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FieldJsonQueryParser.java index 8c592df41b1..ec6b1ffab91 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FieldJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FieldJsonQueryParser.java @@ -33,6 +33,7 @@ import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.query.support.MapperQueryParser; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import java.io.IOException; @@ -80,29 +81,17 @@ public class FieldJsonQueryParser extends AbstractIndexComponent implements Json while ((token = jp.nextToken()) != JsonToken.END_OBJECT) { if (token == JsonToken.FIELD_NAME) { currentFieldName = jp.getCurrentName(); - } else { + } else if (token == JsonToken.VALUE_STRING) { if ("query".equals(currentFieldName)) { queryString = jp.getText(); } else if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + boost = Float.parseFloat(jp.getText()); } else if ("enablePositionIncrements".equals(currentFieldName)) { - if (token == JsonToken.VALUE_TRUE) { - enablePositionIncrements = true; - } else if (token == JsonToken.VALUE_FALSE) { - enablePositionIncrements = false; - } else { - enablePositionIncrements = jp.getIntValue() != 0; - } + enablePositionIncrements = Booleans.parseBoolean(jp.getText(), true); } else if ("lowercaseExpandedTerms".equals(currentFieldName)) { - if (token == JsonToken.VALUE_TRUE) { - lowercaseExpandedTerms = true; - } else if (token == JsonToken.VALUE_FALSE) { - lowercaseExpandedTerms = false; - } else { - lowercaseExpandedTerms = jp.getIntValue() != 0; - } + lowercaseExpandedTerms = Booleans.parseBoolean(jp.getText(), true); } else if ("phraseSlop".equals(currentFieldName)) { - phraseSlop = jp.getIntValue(); + phraseSlop = Integer.parseInt(jp.getText()); } else if ("analyzer".equals(currentFieldName)) { analyzer = analysisService.analyzer(jp.getText()); } else if ("defaultOperator".equals(currentFieldName)) { @@ -115,17 +104,57 @@ public class FieldJsonQueryParser extends AbstractIndexComponent implements Json throw new QueryParsingException(index, "Query default operator [" + op + "] is not allowed"); } } else if ("fuzzyMinSim".equals(currentFieldName)) { - fuzzyMinSim = jp.getFloatValue(); + fuzzyMinSim = Float.parseFloat(jp.getText()); + } else if ("fuzzyPrefixLength".equals(currentFieldName)) { + fuzzyPrefixLength = Integer.parseInt(jp.getText()); + } else if ("escape".equals(currentFieldName)) { + escape = Booleans.parseBoolean(jp.getText(), false); + } + } else if (token == JsonToken.VALUE_NUMBER_INT) { + if ("query".equals(currentFieldName)) { + queryString = jp.getText(); + } else if ("boost".equals(currentFieldName)) { + boost = jp.getIntValue(); + } else if ("enablePositionIncrements".equals(currentFieldName)) { + enablePositionIncrements = jp.getIntValue() != 0; + } else if ("lowercaseExpandedTerms".equals(currentFieldName)) { + lowercaseExpandedTerms = jp.getIntValue() != 0; + } else if ("phraseSlop".equals(currentFieldName)) { + phraseSlop = jp.getIntValue(); + } else if ("fuzzyMinSim".equals(currentFieldName)) { + fuzzyMinSim = jp.getIntValue(); } else if ("fuzzyPrefixLength".equals(currentFieldName)) { fuzzyPrefixLength = jp.getIntValue(); } else if ("escape".equals(currentFieldName)) { - if (token == JsonToken.VALUE_TRUE) { - escape = true; - } else if (token == JsonToken.VALUE_FALSE) { - escape = false; - } else { - escape = jp.getIntValue() != 0; - } + escape = jp.getIntValue() != 0; + } + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { + if ("query".equals(currentFieldName)) { + queryString = jp.getText(); + } else if ("boost".equals(currentFieldName)) { + boost = jp.getFloatValue(); + } else if ("fuzzyPrefixLength".equals(currentFieldName)) { + fuzzyPrefixLength = jp.getIntValue(); + } + } else if (token == JsonToken.VALUE_TRUE) { + if ("query".equals(currentFieldName)) { + queryString = jp.getText(); + } else if ("enablePositionIncrements".equals(currentFieldName)) { + enablePositionIncrements = true; + } else if ("lowercaseExpandedTerms".equals(currentFieldName)) { + lowercaseExpandedTerms = true; + } else if ("escape".equals(currentFieldName)) { + escape = true; + } + } else if (token == JsonToken.VALUE_FALSE) { + if ("query".equals(currentFieldName)) { + queryString = jp.getText(); + } else if ("enablePositionIncrements".equals(currentFieldName)) { + enablePositionIncrements = false; + } else if ("lowercaseExpandedTerms".equals(currentFieldName)) { + lowercaseExpandedTerms = false; + } else if ("escape".equals(currentFieldName)) { + escape = false; } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FilteredQueryJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FilteredQueryJsonQueryParser.java index 174b67d2f0f..86d17ca6796 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FilteredQueryJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/FilteredQueryJsonQueryParser.java @@ -34,7 +34,7 @@ import org.elasticsearch.util.settings.Settings; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class FilteredQueryJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -66,6 +66,10 @@ public class FilteredQueryJsonQueryParser extends AbstractIndexComponent impleme } else if ("filter".equals(currentFieldName)) { filter = parseContext.parseInnerFilter(); } + } else if (token == JsonToken.VALUE_STRING) { + if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } } else if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) { if ("boost".equals(currentFieldName)) { boost = jp.getFloatValue(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonFilterParser.java index 68c0ae19910..2cd2cb7e1b4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonFilterParser.java @@ -26,7 +26,7 @@ import org.elasticsearch.index.query.QueryParsingException; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public interface JsonFilterParser extends IndexComponent { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParser.java index 05eabbcf2ca..735ed3726fd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParser.java @@ -26,7 +26,7 @@ import org.elasticsearch.index.query.QueryParsingException; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public interface JsonQueryParser extends IndexComponent { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MatchAllJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MatchAllJsonQueryParser.java index 665af671276..78fcb256ecf 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MatchAllJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MatchAllJsonQueryParser.java @@ -58,11 +58,15 @@ public class MatchAllJsonQueryParser extends AbstractIndexComponent implements J while ((token = jp.nextToken()) != JsonToken.END_OBJECT) { if (token == JsonToken.FIELD_NAME) { currentFieldName = jp.getCurrentName(); + } else if (token == JsonToken.VALUE_STRING) { + if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } else if ("normsField".equals(currentFieldName)) { + normsField = parseContext.indexName(jp.getText()); + } } else { if ("boost".equals(currentFieldName)) { boost = jp.getFloatValue(); - } else if ("normsField".equals(currentFieldName)) { - normsField = parseContext.indexName(jp.getText()); } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java index c11a0ea56e9..930622ecccb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java @@ -28,6 +28,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.lucene.search.MoreLikeThisQuery; import org.elasticsearch.util.settings.Settings; @@ -72,6 +73,24 @@ public class MoreLikeThisFieldJsonQueryParser extends AbstractIndexComponent imp } else if (token == JsonToken.VALUE_STRING) { if ("likeText".equals(currentFieldName)) { mltQuery.setLikeText(jp.getText()); + } else if ("minTermFrequency".equals(currentFieldName)) { + mltQuery.setMinTermFrequency(Integer.parseInt(jp.getText())); + } else if ("maxQueryTerms".equals(currentFieldName)) { + mltQuery.setMaxQueryTerms(Integer.parseInt(jp.getText())); + } else if ("minDocFreq".equals(currentFieldName)) { + mltQuery.setMinDocFreq(Integer.parseInt(jp.getText())); + } else if ("maxDocFreq".equals(currentFieldName)) { + mltQuery.setMaxDocFreq(Integer.parseInt(jp.getText())); + } else if ("minWordLen".equals(currentFieldName)) { + mltQuery.setMinWordLen(Integer.parseInt(jp.getText())); + } else if ("maxWordLen".equals(currentFieldName)) { + mltQuery.setMaxWordLen(Integer.parseInt(jp.getText())); + } else if ("boostTerms".equals(currentFieldName)) { + mltQuery.setBoostTerms(Booleans.parseBoolean(jp.getText(), false)); + } else if ("boostTermsFactor".equals(currentFieldName)) { + mltQuery.setBoostTermsFactor(Float.parseFloat(jp.getText())); + } else if ("percentTermsToMatch".equals(currentFieldName)) { + mltQuery.setPercentTermsToMatch(Float.parseFloat(jp.getText())); } } else if (token == JsonToken.VALUE_NUMBER_INT) { if ("minTermFrequency".equals(currentFieldName)) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java index 2842678c3df..c8953f4e242 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java @@ -27,6 +27,7 @@ import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.lucene.search.MoreLikeThisQuery; import org.elasticsearch.util.settings.Settings; @@ -64,6 +65,24 @@ public class MoreLikeThisJsonQueryParser extends AbstractIndexComponent implemen } else if (token == JsonToken.VALUE_STRING) { if ("likeText".equals(currentFieldName)) { mltQuery.setLikeText(jp.getText()); + } else if ("minTermFrequency".equals(currentFieldName)) { + mltQuery.setMinTermFrequency(Integer.parseInt(jp.getText())); + } else if ("maxQueryTerms".equals(currentFieldName)) { + mltQuery.setMaxQueryTerms(Integer.parseInt(jp.getText())); + } else if ("minDocFreq".equals(currentFieldName)) { + mltQuery.setMinDocFreq(Integer.parseInt(jp.getText())); + } else if ("maxDocFreq".equals(currentFieldName)) { + mltQuery.setMaxDocFreq(Integer.parseInt(jp.getText())); + } else if ("minWordLen".equals(currentFieldName)) { + mltQuery.setMinWordLen(Integer.parseInt(jp.getText())); + } else if ("maxWordLen".equals(currentFieldName)) { + mltQuery.setMaxWordLen(Integer.parseInt(jp.getText())); + } else if ("boostTerms".equals(currentFieldName)) { + mltQuery.setBoostTerms(Booleans.parseBoolean(jp.getText(), false)); + } else if ("boostTermsFactor".equals(currentFieldName)) { + mltQuery.setBoostTermsFactor(Float.parseFloat(jp.getText())); + } else if ("percentTermsToMatch".equals(currentFieldName)) { + mltQuery.setPercentTermsToMatch(Float.parseFloat(jp.getText())); } } else if (token == JsonToken.VALUE_NUMBER_INT) { if ("minTermFrequency".equals(currentFieldName)) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/PrefixJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/PrefixJsonQueryParser.java index 21b254aac28..7061a88e8e2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/PrefixJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/PrefixJsonQueryParser.java @@ -38,7 +38,7 @@ import java.io.IOException; import static org.elasticsearch.index.query.support.QueryParsers.*; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class PrefixJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -68,10 +68,18 @@ public class PrefixJsonQueryParser extends AbstractIndexComponent implements Jso while ((token = jp.nextToken()) != JsonToken.END_OBJECT) { if (token == JsonToken.FIELD_NAME) { currentFieldName = jp.getCurrentName(); - } else { + } else if (token == JsonToken.VALUE_STRING) { if (NAME.equals(currentFieldName)) { value = jp.getText(); } else if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } + } else if (token == JsonToken.VALUE_NUMBER_INT) { + if ("boost".equals(currentFieldName)) { + boost = jp.getIntValue(); + } + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { + if ("boost".equals(currentFieldName)) { boost = jp.getFloatValue(); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/QueryStringJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/QueryStringJsonQueryParser.java index b27a93d9ee9..3ec0ff3e3c4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/QueryStringJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/QueryStringJsonQueryParser.java @@ -35,6 +35,7 @@ import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.query.support.MapperQueryParser; import org.elasticsearch.index.query.support.MultiFieldMapperQueryParser; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import org.elasticsearch.util.trove.ExtTObjectFloatHashMap; @@ -134,6 +135,26 @@ public class QueryStringJsonQueryParser extends AbstractIndexComponent implement } } else if ("analyzer".equals(currentFieldName)) { analyzer = analysisService.analyzer(jp.getText()); + } else if ("allowLeadingWildcard".equals(currentFieldName)) { + allowLeadingWildcard = Booleans.parseBoolean(jp.getText(), false); + } else if ("lowercaseExpandedTerms".equals(currentFieldName)) { + lowercaseExpandedTerms = Booleans.parseBoolean(jp.getText(), false); + } else if ("enablePositionIncrements".equals(currentFieldName)) { + enablePositionIncrements = Booleans.parseBoolean(jp.getText(), false); + } else if ("escape".equals(currentFieldName)) { + escape = Booleans.parseBoolean(jp.getText(), false); + } else if ("useDisMax".equals(currentFieldName)) { + useDisMax = Booleans.parseBoolean(jp.getText(), false); + } else if ("fuzzyPrefixLength".equals(currentFieldName)) { + fuzzyPrefixLength = Integer.parseInt(jp.getText()); + } else if ("phraseSlop".equals(currentFieldName)) { + phraseSlop = Integer.parseInt(jp.getText()); + } else if ("fuzzyMinSim".equals(currentFieldName)) { + fuzzyMinSim = Float.parseFloat(jp.getText()); + } else if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } else if ("tieBreaker".equals(currentFieldName)) { + tieBreaker = Float.parseFloat(jp.getText()); } } else if (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE) { if ("allowLeadingWildcard".equals(currentFieldName)) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java index 01f72a28116..743e57ae7ef 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java @@ -29,6 +29,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import java.io.IOException; @@ -86,12 +87,16 @@ public class RangeJsonFilterParser extends AbstractIndexComponent implements Jso } else if ("includeLower".equals(currentFieldName)) { if (token == JsonToken.VALUE_NUMBER_INT) { includeLower = jp.getIntValue() != 0; + } else if (token == JsonToken.VALUE_STRING) { + includeLower = Booleans.parseBoolean(jp.getText(), includeLower); } else { includeLower = token == JsonToken.VALUE_TRUE; } } else if ("includeUpper".equals(currentFieldName)) { if (token == JsonToken.VALUE_NUMBER_INT) { includeUpper = jp.getIntValue() != 0; + } else if (token == JsonToken.VALUE_STRING) { + includeUpper = Booleans.parseBoolean(jp.getText(), includeUpper); } else { includeUpper = token == JsonToken.VALUE_TRUE; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java index 0a6374c8d21..67bfc377afa 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java @@ -29,6 +29,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import java.io.IOException; @@ -36,7 +37,7 @@ import java.io.IOException; import static org.elasticsearch.index.query.support.QueryParsers.*; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class RangeJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -83,17 +84,25 @@ public class RangeJsonQueryParser extends AbstractIndexComponent implements Json } else if ("includeLower".equals(currentFieldName)) { if (token == JsonToken.VALUE_NUMBER_INT) { includeLower = jp.getIntValue() != 0; + } else if (token == JsonToken.VALUE_STRING) { + includeLower = Booleans.parseBoolean(jp.getText(), includeLower); } else { includeLower = token == JsonToken.VALUE_TRUE; } } else if ("includeUpper".equals(currentFieldName)) { if (token == JsonToken.VALUE_NUMBER_INT) { includeUpper = jp.getIntValue() != 0; + } else if (token == JsonToken.VALUE_STRING) { + includeUpper = Booleans.parseBoolean(jp.getText(), includeUpper); } else { includeUpper = token == JsonToken.VALUE_TRUE; } } else if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanFirstJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanFirstJsonQueryParser.java index 1a8b333b43f..971e2935384 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanFirstJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanFirstJsonQueryParser.java @@ -71,9 +71,17 @@ public class SpanFirstJsonQueryParser extends AbstractIndexComponent implements } } else { if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } else if ("end".equals(currentFieldName)) { - end = jp.getIntValue(); + if (token == JsonToken.VALUE_STRING) { + end = Integer.parseInt(jp.getText()); + } else { + end = jp.getIntValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNearJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNearJsonQueryParser.java index 7a67d46195b..feb1363ff69 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNearJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNearJsonQueryParser.java @@ -29,6 +29,7 @@ import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.util.Booleans; import org.elasticsearch.util.settings.Settings; import java.io.IOException; @@ -37,7 +38,7 @@ import java.util.List; import static com.google.common.collect.Lists.*; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class SpanNearJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -76,6 +77,16 @@ public class SpanNearJsonQueryParser extends AbstractIndexComponent implements J clauses.add((SpanQuery) query); } } + } else if (token == JsonToken.VALUE_STRING) { + if ("inOrder".equals(currentFieldName)) { + inOrder = Booleans.parseBoolean(jp.getText(), inOrder); + } else if ("collectPayloads".equals(currentFieldName)) { + collectPayloads = Booleans.parseBoolean(jp.getText(), collectPayloads); + } else if ("slop".equals(currentFieldName)) { + slop = Integer.parseInt(jp.getText()); + } else if ("boost".equals(currentFieldName)) { + boost = Float.parseFloat(jp.getText()); + } } else if (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE) { if ("inOrder".equals(currentFieldName)) { inOrder = token == JsonToken.VALUE_TRUE; @@ -89,8 +100,10 @@ public class SpanNearJsonQueryParser extends AbstractIndexComponent implements J collectPayloads = jp.getIntValue() != 0; } else if ("slop".equals(currentFieldName)) { slop = jp.getIntValue(); + } else if ("boost".equals(currentFieldName)) { + boost = jp.getIntValue(); } - } else { + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { if ("boost".equals(currentFieldName)) { boost = jp.getFloatValue(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNotJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNotJsonQueryParser.java index b9153d32742..3be899fb0b9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNotJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanNotJsonQueryParser.java @@ -34,7 +34,7 @@ import org.elasticsearch.util.settings.Settings; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class SpanNotJsonQueryParser extends AbstractIndexComponent implements JsonQueryParser { @@ -77,7 +77,11 @@ public class SpanNotJsonQueryParser extends AbstractIndexComponent implements Js } } else { if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanOrJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanOrJsonQueryParser.java index bdb4ee57886..5b463a2a017 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanOrJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanOrJsonQueryParser.java @@ -75,7 +75,11 @@ public class SpanOrJsonQueryParser extends AbstractIndexComponent implements Jso } } else { if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanTermJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanTermJsonQueryParser.java index 8b97770e686..342c6195ec0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanTermJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/SpanTermJsonQueryParser.java @@ -74,7 +74,11 @@ public class SpanTermJsonQueryParser extends AbstractIndexComponent implements J if ("value".equals(currentFieldName)) { value = jp.getText(); } else if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java index 5e72282b0a7..10189c5e262 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java @@ -70,7 +70,11 @@ public class TermJsonQueryParser extends AbstractIndexComponent implements JsonQ if ("value".equals(currentFieldName)) { value = jp.getText(); } else if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/WildcardJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/WildcardJsonQueryParser.java index b90519282e4..52bc7c93f60 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/WildcardJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/WildcardJsonQueryParser.java @@ -72,7 +72,11 @@ public class WildcardJsonQueryParser extends AbstractIndexComponent implements J if ("wildcard".equals(currentFieldName)) { value = jp.getText(); } else if ("boost".equals(currentFieldName)) { - boost = jp.getFloatValue(); + if (token == JsonToken.VALUE_STRING) { + boost = Float.parseFloat(jp.getText()); + } else { + boost = jp.getFloatValue(); + } } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/Booleans.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/Booleans.java new file mode 100644 index 00000000000..96ef7be69ee --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/Booleans.java @@ -0,0 +1,40 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.elasticsearch.util; + +/** + * @author kimchy (shay.banon) + */ +public class Booleans { + + public static boolean parseBoolean(String value, boolean defaultValue) { + if (value == null) { + return defaultValue; + } + return !(value.equals("false") || value.equals("0") || value.equals("off")); + } + + public static Boolean parseBoolean(String value, Boolean defaultValue) { + if (value == null) { + return defaultValue; + } + return !(value.equals("false") || value.equals("0") || value.equals("off")); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/ImmutableSettings.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/ImmutableSettings.java index 08f2361d876..32b69370e2b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/ImmutableSettings.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/ImmutableSettings.java @@ -152,15 +152,7 @@ public class ImmutableSettings implements Settings { } @Override public Boolean getAsBoolean(String setting, Boolean defaultValue) { - String sValue = get(setting); - if (sValue == null) { - return defaultValue; - } - try { - return sValue.equals("true") || sValue.equals("1"); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse boolean setting [" + setting + "] with value [" + sValue + "]", e); - } + return Booleans.parseBoolean(get(setting), defaultValue); } @Override public TimeValue getAsTime(String setting, TimeValue defaultValue) {