Elasticsearch: Exclude types from V7 which deprecates them

Remove the use of the include_type_name parameter which started from V6.
Before this change, warnings related to the V7 usage of that parameter
showed in consoles.

More in [1] about the include_type_name parameter for Elasticsearch
6.x (warning), 7.x (deprecation), and 8.x (deprecated).

[1] https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html#_schedule_for_removal_of_mapping_types

Bug: Issue 10496
Change-Id: I6fe26e097a618d71a781dbebae621b6515d6822e
This commit is contained in:
Marco Miller
2019-05-16 12:17:00 -04:00
committed by Marco Miller
parent 9294a5d38c
commit 23b2c4a206
4 changed files with 68 additions and 35 deletions

View File

@@ -188,10 +188,15 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
}
protected String getMappingsFor(String type, MappingProperties properties) {
JsonObject mappingType = new JsonObject();
mappingType.add(type, gson.toJsonTree(properties));
JsonObject mappings = new JsonObject();
mappings.add(MAPPINGS, gson.toJsonTree(mappingType));
if (client.adapter().omitType()) {
mappings.add(MAPPINGS, gson.toJsonTree(properties));
} else {
JsonObject mappingType = new JsonObject();
mappingType.add(type, gson.toJsonTree(properties));
mappings.add(MAPPINGS, gson.toJsonTree(mappingType));
}
return gson.toJson(mappings);
}
@@ -229,11 +234,12 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
protected String getURI(String type, String request) throws UnsupportedEncodingException {
String encodedIndexName = URLEncoder.encode(indexName, UTF_8.toString());
if (SEARCH.equals(request) && client.adapter().omitTypeFromSearch()) {
if (SEARCH.equals(request) && client.adapter().omitType()) {
return encodedIndexName + "/" + request;
}
String encodedType = URLEncoder.encode(type, UTF_8.toString());
return encodedIndexName + "/" + encodedType + "/" + request;
String encodedTypeIfAny =
client.adapter().omitType() ? "" : "/" + URLEncoder.encode(type, UTF_8.toString());
return encodedIndexName + encodedTypeIfAny + "/" + request;
}
protected Response postRequest(String uri, Object payload) throws IOException {

View File

@@ -141,7 +141,7 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
BulkRequest bulk =
new IndexRequest(getId(cd), indexName, adapter.getType(insertIndex), adapter)
.add(new UpdateRequest<>(schema, cd));
if (!adapter.usePostV5Type()) {
if (adapter.deleteToReplace()) {
bulk.add(new DeleteRequest(cd.getId().toString(), indexName, deleteIndex, adapter));
}
@@ -160,17 +160,19 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
throws QueryParseException {
Set<Change.Status> statuses = ChangeIndexRewriter.getPossibleStatus(p);
List<String> indexes = Lists.newArrayListWithCapacity(2);
if (client.adapter().usePostV5Type()) {
if (!Sets.intersection(statuses, OPEN_STATUSES).isEmpty()
|| !Sets.intersection(statuses, CLOSED_STATUSES).isEmpty()) {
indexes.add(ElasticQueryAdapter.POST_V5_TYPE);
}
} else {
if (!Sets.intersection(statuses, OPEN_STATUSES).isEmpty()) {
indexes.add(OPEN_CHANGES);
}
if (!Sets.intersection(statuses, CLOSED_STATUSES).isEmpty()) {
indexes.add(CLOSED_CHANGES);
if (!client.adapter().omitType()) {
if (client.adapter().useV6Type()) {
if (!Sets.intersection(statuses, OPEN_STATUSES).isEmpty()
|| !Sets.intersection(statuses, CLOSED_STATUSES).isEmpty()) {
indexes.add(ElasticQueryAdapter.V6_TYPE);
}
} else {
if (!Sets.intersection(statuses, OPEN_STATUSES).isEmpty()) {
indexes.add(OPEN_CHANGES);
}
if (!Sets.intersection(statuses, CLOSED_STATUSES).isEmpty()) {
indexes.add(CLOSED_CHANGES);
}
}
}
return new QuerySource(indexes, p, opts);
@@ -178,16 +180,16 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
@Override
protected String getDeleteActions(Id c) {
if (client.adapter().usePostV5Type()) {
return delete(ElasticQueryAdapter.POST_V5_TYPE, c);
if (!client.adapter().useV5Type()) {
return delete(client.adapter().getType(), c);
}
return delete(OPEN_CHANGES, c) + delete(CLOSED_CHANGES, c);
}
@Override
protected String getMappings() {
if (client.adapter().usePostV5Type()) {
return getMappingsFor(ElasticQueryAdapter.POST_V5_TYPE, mapping.changes);
if (!client.adapter().useV5Type()) {
return getMappingsFor(client.adapter().getType(), mapping.changes);
}
return gson.toJson(ImmutableMap.of(MAPPINGS, mapping));
}

View File

@@ -17,11 +17,12 @@ package com.google.gerrit.elasticsearch;
import com.google.gson.JsonObject;
public class ElasticQueryAdapter {
static final String POST_V5_TYPE = "_doc";
static final String V6_TYPE = "_doc";
private final boolean ignoreUnmapped;
private final boolean usePostV5Type;
private final boolean omitTypeFromSearch;
private final boolean useV5Type;
private final boolean useV6Type;
private final boolean omitType;
private final String searchFilteringName;
private final String indicesExistParam;
@@ -33,15 +34,16 @@ public class ElasticQueryAdapter {
ElasticQueryAdapter(ElasticVersion version) {
this.ignoreUnmapped = false;
this.usePostV5Type = version.isV6OrLater();
this.omitTypeFromSearch = version.isV7OrLater();
this.useV5Type = !version.isV6OrLater();
this.useV6Type = version.isV6();
this.omitType = version.isV7OrLater();
this.versionDiscoveryUrl = version.isV6OrLater() ? "/%s*" : "/%s*/_aliases";
this.searchFilteringName = "_source";
this.indicesExistParam = "?allow_no_indices=false";
this.exactFieldType = "keyword";
this.stringFieldType = "text";
this.indexProperty = "true";
this.includeTypeNameParam = version.isV7OrLater() ? "?include_type_name=true" : "";
this.includeTypeNameParam = version.isV6() ? "?include_type_name=true" : "";
}
void setIgnoreUnmapped(JsonObject properties) {
@@ -51,7 +53,7 @@ public class ElasticQueryAdapter {
}
public void setType(JsonObject properties, String type) {
if (!usePostV5Type) {
if (useV5Type) {
properties.addProperty("_type", type);
}
}
@@ -76,16 +78,31 @@ public class ElasticQueryAdapter {
return indexProperty;
}
boolean usePostV5Type() {
return usePostV5Type;
boolean deleteToReplace() {
return useV5Type;
}
boolean omitTypeFromSearch() {
return omitTypeFromSearch;
boolean useV5Type() {
return useV5Type;
}
boolean useV6Type() {
return useV6Type;
}
boolean omitType() {
return omitType;
}
String getType() {
return getType("");
}
String getType(String type) {
return usePostV5Type() ? POST_V5_TYPE : type;
if (useV6Type()) {
return V6_TYPE;
}
return useV5Type() ? type : "";
}
String getVersionDiscoveryUrl(String name) {

View File

@@ -58,6 +58,10 @@ public enum ElasticVersion {
return Joiner.on(", ").join(ElasticVersion.values());
}
public boolean isV6() {
return getMajor() == 6;
}
public boolean isV6OrLater() {
return isAtLeastVersion(6);
}
@@ -67,7 +71,11 @@ public enum ElasticVersion {
}
private boolean isAtLeastVersion(int v) {
return Integer.valueOf(version.split("\\.")[0]) >= v;
return getMajor() >= v;
}
private Integer getMajor() {
return Integer.valueOf(version.split("\\.")[0]);
}
@Override