Query DSL: deprecate _name and boost in short variants of queries

As discussed in #11744 this is the last step to unify parsing of boost and _name. Those fields are supported only in long version of queries, while we sometimes parse them when wwe shouldn't, inconsistently.

Closes #11744
Closes #12966
This commit is contained in:
javanna 2015-08-18 18:53:53 +02:00 committed by Luca Cavanna
parent 793fcb6998
commit dc807f294e
7 changed files with 22 additions and 11 deletions

View File

@ -943,7 +943,8 @@ public class Strings {
boolean changed = false;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '_') {
//e.g. _name stays as-is, _first_name becomes _firstName
if (c == '_' && i > 0) {
if (!changed) {
if (sb != null) {
sb.setLength(0);

View File

@ -23,6 +23,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
@ -38,6 +39,8 @@ public class PrefixQueryParser implements QueryParser {
public static final String NAME = "prefix";
private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of prefix query");
@Inject
public PrefixQueryParser() {
}
@ -84,7 +87,7 @@ public class PrefixQueryParser implements QueryParser {
}
}
} else {
if ("_name".equals(currentFieldName)) {
if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) {
queryName = parser.text();
} else {
fieldName = currentFieldName;

View File

@ -40,6 +40,7 @@ public class RangeQueryParser implements QueryParser {
public static final String NAME = "range";
private static final ParseField FIELDDATA_FIELD = new ParseField("fielddata").withAllDeprecated("[no replacement]");
private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of range query");
@Inject
public RangeQueryParser() {
@ -109,7 +110,7 @@ public class RangeQueryParser implements QueryParser {
}
}
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) {
queryName = parser.text();
} else if (parseContext.parseFieldMatcher().match(currentFieldName, FIELDDATA_FIELD)) {
// ignore

View File

@ -24,6 +24,7 @@ import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.util.automaton.Operations;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
@ -41,6 +42,8 @@ public class RegexpQueryParser implements QueryParser {
public static final int DEFAULT_FLAGS_VALUE = RegexpFlag.ALL.value();
private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of regexp query");
@Inject
public RegexpQueryParser() {
}
@ -96,7 +99,7 @@ public class RegexpQueryParser implements QueryParser {
}
}
} else {
if ("_name".equals(currentFieldName)) {
if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) {
queryName = parser.text();
} else {
fieldName = currentFieldName;

View File

@ -22,10 +22,10 @@ package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
@ -37,6 +37,9 @@ public class TermQueryParser implements QueryParser {
public static final String NAME = "term";
private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of term query");
private static final ParseField BOOST_FIELD = new ParseField("boost").withAllDeprecated("boost is not supported in short version of term query");
@Inject
public TermQueryParser() {
}
@ -85,9 +88,9 @@ public class TermQueryParser implements QueryParser {
}
}
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) {
queryName = parser.text();
} else if ("boost".equals(currentFieldName)) {
} else if (parseContext.parseFieldMatcher().match(currentFieldName, BOOST_FIELD)) {
boost = parser.floatValue();
} else {
if (fieldName != null) {

View File

@ -29,6 +29,8 @@ public class StringsTests extends ESTestCase {
assertEquals("fooBar", Strings.toCamelCase("foo_bar"));
assertEquals("fooBarFooBar", Strings.toCamelCase("foo_bar_foo_bar"));
assertEquals("fooBar", Strings.toCamelCase("foo_bar_"));
assertEquals("_foo", Strings.toCamelCase("_foo"));
assertEquals("_fooBar", Strings.toCamelCase("_foo_bar_"));
}
public void testSubstring() {

View File

@ -54,9 +54,7 @@ import java.nio.file.Path;
import java.util.Set;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
/**
*
@ -249,7 +247,7 @@ public class AnalysisModuleTests extends ESTestCase {
fail("This should fail with IllegalArgumentException because the analyzers name starts with _");
} catch (ProvisionException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
assertThat(e.getCause().getMessage(), equalTo("analyzer name must not start with '_'. got \"_invalid_name\""));
assertThat(e.getCause().getMessage(), either(equalTo("analyzer name must not start with '_'. got \"_invalid_name\"")).or(equalTo("analyzer name must not start with '_'. got \"_invalidName\"")));
}
}