Skip to content

Commit

Permalink
Search case insensitivity - complete the version dance now the back p…
Browse files Browse the repository at this point in the history
…ort is complete (#62764)

Change the versions used in serialization now the back port is complete.
Re-enbaled BWC tests.

Relates to #61546
  • Loading branch information
markharwood authored Sep 23, 2020
1 parent c622bd6 commit ceeada2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 58 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ tasks.register("verifyVersions") {
* after the backport of the backcompat code is complete.
*/

boolean bwc_tests_enabled = false
final String bwc_tests_disabled_issue = "https://github.com/elastic/elasticsearch/pull/62764" /* place a PR link here when committing bwc changes */
boolean bwc_tests_enabled = true
final String bwc_tests_disabled_issue = "" /* place a PR link here when committing bwc changes */
if (bwc_tests_enabled == false) {
if (bwc_tests_disabled_issue.isEmpty()) {
throw new GradleException("bwc_tests_disabled_issue must be set when bwc_tests_enabled == false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public PrefixQueryBuilder(StreamInput in) throws IOException {
fieldName = in.readString();
value = in.readString();
rewrite = in.readOptionalString();
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
if (in.getVersion().onOrAfter(Version.V_7_10_0)) {
caseInsensitive = in.readBoolean();
}
}
Expand All @@ -94,7 +94,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeString(value);
out.writeOptionalString(rewrite);
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
if (out.getVersion().onOrAfter(Version.V_7_10_0)) {
out.writeBoolean(caseInsensitive);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ public boolean caseInsensitive() {
*/
public TermQueryBuilder(StreamInput in) throws IOException {
super(in);
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
if (in.getVersion().onOrAfter(Version.V_7_10_0)) {
caseInsensitive = in.readBoolean();
}
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
super.doWriteTo(out);
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
if (out.getVersion().onOrAfter(Version.V_7_10_0)) {
out.writeBoolean(caseInsensitive);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public WildcardQueryBuilder(StreamInput in) throws IOException {
fieldName = in.readString();
value = in.readString();
rewrite = in.readOptionalString();
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
if (in.getVersion().onOrAfter(Version.V_7_10_0)) {
caseInsensitive = in.readBoolean();
}
}
Expand All @@ -104,7 +104,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeString(value);
out.writeOptionalString(rewrite);
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
if (out.getVersion().onOrAfter(Version.V_7_10_0)) {
out.writeBoolean(caseInsensitive);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.query;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.AutomatonQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
Expand All @@ -44,13 +45,6 @@ protected PrefixQueryBuilder doCreateTestQueryBuilder() {
if (randomBoolean()) {
query.rewrite(getRandomRewriteMethod());
}
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// if (randomBoolean()) {
// query.caseInsensitive(true);
// }
return query;
}

Expand All @@ -77,8 +71,10 @@ private static PrefixQueryBuilder randomPrefixQuery() {

@Override
protected void doAssertLuceneQuery(PrefixQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
assertThat(query, Matchers.anyOf(instanceOf(PrefixQuery.class), instanceOf(MatchNoDocsQuery.class)));
if (context.fieldMapper(queryBuilder.fieldName()) != null) { // The field is mapped
assertThat(query, Matchers.anyOf(instanceOf(PrefixQuery.class), instanceOf(MatchNoDocsQuery.class),
instanceOf(AutomatonQuery.class)));
if (context.fieldMapper(queryBuilder.fieldName()) != null
&& queryBuilder.caseInsensitive() == false) { // The field is mapped and case sensitive
PrefixQuery prefixQuery = (PrefixQuery) query;

String expectedFieldName = expectedFieldName(queryBuilder.fieldName());
Expand Down Expand Up @@ -108,13 +104,9 @@ public void testBlendedRewriteMethod() throws IOException {

public void testFromJson() throws IOException {
String json =
"{ \"prefix\" : { \"user\" : { \"value\" : \"ki\", \"boost\" : 2.0 "
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// " \"case_insensitive\" : true\n" +

"{ \"prefix\" : { \"user\" : { \"value\" : \"ki\",\n"
+ " \"case_insensitive\" : true,\n"
+ " \"boost\" : 2.0"
+ "} }}";

PrefixQueryBuilder parsed = (PrefixQueryBuilder) parseQuery(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ protected TermQueryBuilder doCreateTestQueryBuilder() {
@Override
protected TermQueryBuilder createQueryBuilder(String fieldName, Object value) {
TermQueryBuilder result = new TermQueryBuilder(fieldName, value);
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// if (randomBoolean()) {
// result.caseInsensitive(true);
// }
return result;
}

Expand Down Expand Up @@ -142,12 +135,8 @@ public void testFromJson() throws IOException {
" \"term\" : {\n" +
" \"exact_value\" : {\n" +
" \"value\" : \"Quick Foxes!\",\n" +
" \"case_insensitive\" : true,\n" +
" \"boost\" : 1.0\n" +
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// " \"case_insensitive\" : true\n" +
" }\n" +
" }\n" +
"}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.query;

import org.apache.lucene.search.AutomatonQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.WildcardQuery;
Expand All @@ -41,13 +42,6 @@ protected WildcardQueryBuilder doCreateTestQueryBuilder() {
if (randomBoolean()) {
query.rewrite(randomFrom(getRandomRewriteMethod()));
}
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// if (randomBoolean()) {
// query.caseInsensitive(true);
// }
return query;
}

Expand Down Expand Up @@ -78,14 +72,18 @@ protected void doAssertLuceneQuery(WildcardQueryBuilder queryBuilder, Query quer
String expectedFieldName = expectedFieldName(queryBuilder.fieldName());

if (expectedFieldName.equals(TEXT_FIELD_NAME)) {
assertThat(query, instanceOf(WildcardQuery.class));
WildcardQuery wildcardQuery = (WildcardQuery) query;

assertThat(wildcardQuery.getField(), equalTo(expectedFieldName));
assertThat(wildcardQuery.getTerm().field(), equalTo(expectedFieldName));
// wildcard queries get normalized
String text = wildcardQuery.getTerm().text().toLowerCase(Locale.ROOT);
assertThat(text, equalTo(text));
if (queryBuilder.caseInsensitive()) {
assertThat(query, instanceOf(AutomatonQuery.class));
} else {
assertThat(query, instanceOf(WildcardQuery.class));
WildcardQuery wildcardQuery = (WildcardQuery) query;

assertThat(wildcardQuery.getField(), equalTo(expectedFieldName));
assertThat(wildcardQuery.getTerm().field(), equalTo(expectedFieldName));
// wildcard queries get normalized
String text = wildcardQuery.getTerm().text().toLowerCase(Locale.ROOT);
assertThat(text, equalTo(text));
}
} else {
Query expected = new MatchNoDocsQuery("unknown field [" + expectedFieldName + "]");
assertEquals(expected, query);
Expand All @@ -110,13 +108,9 @@ public void testEmptyValue() throws IOException {
}

public void testFromJson() throws IOException {
String json = "{ \"wildcard\" : { \"user\" : { \"wildcard\" : \"ki*y\", \"boost\" : 2.0"
//TODO code below is commented out while we do the Version dance for PR 61596. Steps are
// 1) Commit PR 61596 with this code commented out in master
// 2) Backport PR 61596 to 7.x, uncommented
// 3) New PR on master to uncomment this code now that 7.x has support for case insensitive flag.
// " \"case_insensitive\" : true\n" +

String json = "{ \"wildcard\" : { \"user\" : { \"wildcard\" : \"ki*y\","
+ " \"case_insensitive\" : true,\n"
+ " \"boost\" : 2.0"
+ " } }}";
WildcardQueryBuilder parsed = (WildcardQueryBuilder) parseQuery(json);
checkGeneratedJson(json, parsed);
Expand Down

0 comments on commit ceeada2

Please sign in to comment.