Elasticsearch: Fix support for V6 versions earlier than 6.7.*

Change I02fb4b445 added the include_type_name parameter on all requests
to Elasticsearch V6, but it turns out that the parameter is not
recognized on earlier V6 versions (e.g. 6.6.* and 6.5.*) and causes
index creation to fail with "bad request".

Fix the query adapter to only put the parameter on requests for version
6.7 and later minor ones.

Bug: Issue 10905
Change-Id: Ic35e38f1deb95905e70f547ca6fe6c3a99aba30d
This commit is contained in:
David Pursehouse
2019-05-27 12:14:36 +09:00
committed by Marco Miller
parent 732b3d03e5
commit 6f26d4a589
3 changed files with 27 additions and 4 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.elasticsearch; package com.google.gerrit.elasticsearch;
import static com.google.gerrit.elasticsearch.ElasticVersion.V6_7;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class ElasticQueryAdapter { public class ElasticQueryAdapter {
@@ -42,11 +44,12 @@ public class ElasticQueryAdapter {
this.omitType = version.isV7OrLater(); this.omitType = version.isV7OrLater();
this.versionDiscoveryUrl = version.isV6OrLater() ? "/%s*" : "/%s*/_aliases"; this.versionDiscoveryUrl = version.isV6OrLater() ? "/%s*" : "/%s*/_aliases";
this.searchFilteringName = "_source"; this.searchFilteringName = "_source";
this.indicesExistParams = version.isV6() ? INDICES + "&" + INCLUDE_TYPE : INDICES; this.indicesExistParams =
version.isAtLeastMinorVersion(V6_7) ? INDICES + "&" + INCLUDE_TYPE : INDICES;
this.exactFieldType = "keyword"; this.exactFieldType = "keyword";
this.stringFieldType = "text"; this.stringFieldType = "text";
this.indexProperty = "true"; this.indexProperty = "true";
this.includeTypeNameParam = version.isV6() ? "?" + INCLUDE_TYPE : ""; this.includeTypeNameParam = version.isAtLeastMinorVersion(V6_7) ? "?" + INCLUDE_TYPE : "";
} }
void setIgnoreUnmapped(JsonObject properties) { void setIgnoreUnmapped(JsonObject properties) {

View File

@@ -71,14 +71,22 @@ public enum ElasticVersion {
return isAtLeastVersion(7); return isAtLeastVersion(7);
} }
private boolean isAtLeastVersion(int v) { private boolean isAtLeastVersion(int major) {
return getMajor() >= v; return getMajor() >= major;
}
public boolean isAtLeastMinorVersion(ElasticVersion version) {
return getMajor().equals(version.getMajor()) && getMinor() >= version.getMinor();
} }
private Integer getMajor() { private Integer getMajor() {
return Integer.valueOf(version.split("\\.")[0]); return Integer.valueOf(version.split("\\.")[0]);
} }
private Integer getMinor() {
return Integer.valueOf(version.split("\\.")[1]);
}
@Override @Override
public String toString() { public String toString() {
return version; return version;

View File

@@ -70,6 +70,18 @@ public class ElasticVersionTest {
assertThat(ElasticVersion.V7_0.isV6OrLater()).isTrue(); assertThat(ElasticVersion.V7_0.isV6OrLater()).isTrue();
} }
@Test
public void atLeastMinorVersion() throws Exception {
assertThat(ElasticVersion.V5_6.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_2.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_3.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_4.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_5.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_6.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
assertThat(ElasticVersion.V6_7.isAtLeastMinorVersion(ElasticVersion.V6_7)).isTrue();
assertThat(ElasticVersion.V7_0.isAtLeastMinorVersion(ElasticVersion.V6_7)).isFalse();
}
@Test @Test
public void version7() throws Exception { public void version7() throws Exception {
assertThat(ElasticVersion.V5_6.isV7OrLater()).isFalse(); assertThat(ElasticVersion.V5_6.isV7OrLater()).isFalse();