Skip to content

Commit ce686b1

Browse files
committed
DATAES-637 - Change branch 4.0. to use Elasticsearch 7.3.
Original PR: spring-projects#302
1 parent 9cb9c72 commit ce686b1

File tree

41 files changed

+159
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+159
-96
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<properties>
2121
<commonslang>2.6</commonslang>
22-
<elasticsearch>6.8.1</elasticsearch>
22+
<elasticsearch>7.3.0</elasticsearch>
2323
<log4j>2.9.1</log4j>
2424
<springdata.commons>2.3.0.BUILD-SNAPSHOT</springdata.commons>
2525
<netty>4.1.39.Final</netty>

src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.lang.annotation.Target;
2323

2424
import org.elasticsearch.index.VersionType;
25-
2625
import org.springframework.data.annotation.Persistent;
2726

2827
/**
@@ -33,6 +32,7 @@
3332
* @author Mason Chan
3433
* @author Ivan Greene
3534
* @author Mark Paluch
35+
* @author Peter-Josef Meisch
3636
*/
3737
@Persistent
3838
@Inherited
@@ -54,8 +54,14 @@
5454
String indexName();
5555

5656
/**
57-
* Mapping type name.
57+
* Mapping type name. <br/>
58+
* deprecated as Elasticsearch does not support this anymore
59+
* (@see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.3/removal-of-types.html">Elastisearch removal of types documentation</a>) and will remove it in
60+
* Elasticsearch 8.
61+
*
62+
* @deprecated since 4.0
5863
*/
64+
@Deprecated
5965
String type() default "";
6066

6167
/**
@@ -64,9 +70,11 @@
6470
boolean useServerConfiguration() default false;
6571

6672
/**
67-
* Number of shards for the index {@link #indexName()}. Used for index creation.
73+
* Number of shards for the index {@link #indexName()}. Used for index creation. <br/>
74+
* With version 4.0, the default value is changed from 5 to 1 to reflect the change in the default settings of
75+
* Elasticsearch which changed to 1 as well in Elasticsearch 7.0.
6876
*/
69-
short shards() default 5;
77+
short shards() default 1;
7078

7179
/**
7280
* Number of replicas for the index {@link #indexName()}. Used for index creation.

src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.util.Collection;
23+
import java.util.Collections;
2324

2425
import org.elasticsearch.client.Client;
2526
import org.elasticsearch.client.node.NodeClient;
@@ -42,6 +43,7 @@
4243
* @author Rizwan Idrees
4344
* @author Mohsin Husen
4445
* @author Ilkang Na
46+
* @author Peter-Josef Meisch
4547
*/
4648
public class NodeClientFactoryBean implements FactoryBean<Client>, InitializingBean, DisposableBean {
4749

@@ -56,14 +58,17 @@ public class NodeClientFactoryBean implements FactoryBean<Client>, InitializingB
5658

5759
public static class TestNode extends Node {
5860

61+
private static final String DEFAULT_NODE_NAME = "spring-data-elasticsearch-test-node";
62+
5963
public TestNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins) {
6064

61-
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), classpathPlugins, false);
65+
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(), null,
66+
() -> DEFAULT_NODE_NAME), classpathPlugins, false);
6267
}
6368

6469
protected void registerDerivedNodeNameWithLogger(String nodeName) {
6570
try {
66-
LogConfigurator.setNodeName(nodeName);
71+
LogConfigurator.setNodeName(nodeName);
6772
} catch (Exception e) {
6873
// nagh - just forget about it
6974
}

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultReactiveElasticsearchClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
*
123123
* @author Christoph Strobl
124124
* @author Mark Paluch
125+
* @author Peter-Josef Meisch
125126
* @since 3.2
126127
* @see ClientConfiguration
127128
* @see ReactiveRestClients
@@ -548,7 +549,7 @@ private static GetResult getResponseToGetResult(GetResponse response) {
548549

549550
return new GetResult(response.getIndex(), response.getType(), response.getId(), response.getSeqNo(),
550551
response.getPrimaryTerm(), response.getVersion(), response.isExists(), response.getSourceAsBytesRef(),
551-
response.getFields());
552+
response.getFields(), null);
552553
}
553554

554555
// -->

src/main/java/org/springframework/data/elasticsearch/client/util/RequestConverters.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
3434
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
3535
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
36-
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
3736
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
3837
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3938
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -60,6 +59,7 @@
6059
import org.elasticsearch.client.Request;
6160
import org.elasticsearch.client.Requests;
6261
import org.elasticsearch.client.RethrottleRequest;
62+
import org.elasticsearch.client.indices.AnalyzeRequest;
6363
import org.elasticsearch.cluster.health.ClusterHealthStatus;
6464
import org.elasticsearch.common.Priority;
6565
import org.elasticsearch.common.Strings;
@@ -98,6 +98,8 @@
9898
* <p>
9999
* Only intended for internal use.
100100
*
101+
* @author Christoph Strobl
102+
* @author Peter-Josef Meisch
101103
* @since 3.2
102104
*/
103105
public class RequestConverters {
@@ -718,10 +720,10 @@ public static Request putMapping(PutMappingRequest putMappingRequest) {
718720
Request request = new Request(HttpMethod.PUT.name(),
719721
RequestConverters.endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type()));
720722

721-
RequestConverters.Params parameters = new RequestConverters.Params(request);
722-
parameters.withTimeout(putMappingRequest.timeout());
723-
parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout());
724-
723+
RequestConverters.Params parameters = new RequestConverters.Params(request) //
724+
.withTimeout(putMappingRequest.timeout()) //
725+
.withMasterTimeout(putMappingRequest.masterNodeTimeout()) //
726+
.withIncludeTypeName(true);
725727
request.setEntity(RequestConverters.createEntity(putMappingRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
726728
return request;
727729
}
@@ -942,7 +944,10 @@ Params withWaitForActiveShards(ActiveShardCount activeShardCount) {
942944

943945
Params withWaitForActiveShards(ActiveShardCount activeShardCount, ActiveShardCount defaultActiveShardCount) {
944946
if (activeShardCount != null && activeShardCount != defaultActiveShardCount) {
945-
return putParam("wait_for_active_shards", activeShardCount.toString().toLowerCase(Locale.ROOT));
947+
// in Elasticsearch 7, "default" cannot be sent anymore, so it needs to be mapped to the default value of 1
948+
String value = activeShardCount == ActiveShardCount.DEFAULT ? "1"
949+
: activeShardCount.toString().toLowerCase(Locale.ROOT);
950+
return putParam("wait_for_active_shards", value);
946951
}
947952
return this;
948953
}
@@ -1082,6 +1087,18 @@ Params withWaitForEvents(Priority waitForEvents) {
10821087
}
10831088
return this;
10841089
}
1090+
1091+
/**
1092+
* sets the include_type_name parameter. Needed for Elasticsearch 7 to be used with the mapping types still
1093+
* available. Will be removed again when Elasticsearch drops the support for this parameter in Elasticsearch 8.
1094+
*
1095+
* @deprecated since 4.0
1096+
* @since 4.0
1097+
*/
1098+
@Deprecated
1099+
Params withIncludeTypeName(boolean includeTypeName) {
1100+
return putParam("include_type_name", String.valueOf(includeTypeName));
1101+
}
10851102
}
10861103

10871104
/**

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.action.search.SearchResponse;
2929
import org.elasticsearch.common.document.DocumentField;
3030
import org.elasticsearch.search.SearchHit;
31-
3231
import org.springframework.core.convert.ConversionService;
3332
import org.springframework.core.convert.support.DefaultConversionService;
3433
import org.springframework.data.domain.Pageable;
@@ -40,6 +39,7 @@
4039
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
4140
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
4241
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
42+
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
4343
import org.springframework.data.mapping.PersistentPropertyAccessor;
4444
import org.springframework.data.mapping.context.MappingContext;
4545
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
@@ -99,7 +99,7 @@ private static EntityMapper initEntityMapper(
9999
@Override
100100
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
101101

102-
long totalHits = response.getHits().getTotalHits();
102+
long totalHits = SearchHitsUtil.getTotalCount(response.getHits());
103103
float maxScore = response.getHits().getMaxScore();
104104

105105
List<T> results = new ArrayList<>();

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.elasticsearch.action.search.SearchScrollRequest;
5757
import org.elasticsearch.action.update.UpdateRequest;
5858
import org.elasticsearch.action.update.UpdateResponse;
59+
import org.elasticsearch.client.Request;
5960
import org.elasticsearch.client.RequestOptions;
6061
import org.elasticsearch.client.Requests;
6162
import org.elasticsearch.client.Response;
@@ -104,6 +105,7 @@
104105
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
105106
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
106107
import org.springframework.data.elasticsearch.core.query.*;
108+
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
107109
import org.springframework.data.util.CloseableIterator;
108110
import org.springframework.lang.Nullable;
109111
import org.springframework.util.Assert;
@@ -302,7 +304,8 @@ public Map<String, Object> getMapping(String indexName, String type) {
302304
Map<String, Object> mappings = null;
303305
RestClient restClient = client.getLowLevelClient();
304306
try {
305-
Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type);
307+
Request request = new Request("GET", '/' + indexName + "/_mapping/" + type + "?include_type_name=true");
308+
Response response = restClient.performRequest(request);
306309
mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()), type);
307310
} catch (Exception e) {
308311
throw new ElasticsearchException(
@@ -601,7 +604,7 @@ private long doCount(SearchRequest countRequest, QueryBuilder elasticsearchQuery
601604
countRequest.source(sourceBuilder);
602605

603606
try {
604-
return client.search(countRequest, RequestOptions.DEFAULT).getHits().getTotalHits();
607+
return SearchHitsUtil.getTotalCount(client.search(countRequest, RequestOptions.DEFAULT).getHits());
605608
} catch (IOException e) {
606609
throw new ElasticsearchException("Error while searching for request: " + countRequest.toString(), e);
607610
}
@@ -622,7 +625,7 @@ private long doCount(SearchRequest searchRequest, QueryBuilder elasticsearchQuer
622625
} catch (IOException e) {
623626
throw new ElasticsearchException("Error for search request: " + searchRequest.toString(), e);
624627
}
625-
return response.getHits().getTotalHits();
628+
return SearchHitsUtil.getTotalCount(response.getHits());
626629
}
627630

628631
private <T> SearchRequest prepareCount(Query query, Class<T> clazz) {
@@ -844,7 +847,7 @@ public boolean indexExists(String indexName) {
844847
public boolean typeExists(String index, String type) {
845848
RestClient restClient = client.getLowLevelClient();
846849
try {
847-
Response response = restClient.performRequest("HEAD", index + "/_mapping/" + type);
850+
Response response = restClient.performRequest(new Request("HEAD", index + "/_mapping/" + type));
848851
return (response.getStatusLine().getStatusCode() == 200);
849852
} catch (Exception e) {
850853
throw new ElasticsearchException("Error while checking type exists for index: " + index + " type : " + type + " ",
@@ -1272,7 +1275,7 @@ public Map getSetting(String indexName) {
12721275
Map settings = null;
12731276
RestClient restClient = client.getLowLevelClient();
12741277
try {
1275-
Response response = restClient.performRequest("GET", "/" + indexName + "/_settings");
1278+
Response response = restClient.performRequest(new Request("GET", "/" + indexName + "/_settings"));
12761279
settings = convertSettingResponse(EntityUtils.toString(response.getEntity()), indexName);
12771280

12781281
} catch (Exception e) {
@@ -1432,10 +1435,6 @@ private IndexRequest prepareIndex(IndexQuery query) {
14321435
indexRequest.versionType(versionType);
14331436
}
14341437

1435-
if (query.getParentId() != null) {
1436-
indexRequest.parent(query.getParentId());
1437-
}
1438-
14391438
return indexRequest;
14401439
} catch (IOException e) {
14411440
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);
@@ -1466,7 +1465,7 @@ public List<AliasMetaData> queryForAlias(String indexName) {
14661465
String aliasResponse;
14671466

14681467
try {
1469-
response = restClient.performRequest("GET", "/" + indexName + "/_alias/*");
1468+
response = restClient.performRequest(new Request("GET", "/" + indexName + "/_alias/*"));
14701469
aliasResponse = EntityUtils.toString(response.getEntity());
14711470
} catch (Exception e) {
14721471
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName, e);

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
9393
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
9494
import org.springframework.data.elasticsearch.core.query.*;
95+
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
9596
import org.springframework.data.util.CloseableIterator;
9697
import org.springframework.lang.Nullable;
9798
import org.springframework.util.Assert;
@@ -508,7 +509,7 @@ private long doCount(SearchRequestBuilder countRequestBuilder, QueryBuilder elas
508509
if (elasticsearchQuery != null) {
509510
countRequestBuilder.setQuery(elasticsearchQuery);
510511
}
511-
return countRequestBuilder.execute().actionGet().getHits().getTotalHits();
512+
return SearchHitsUtil.getTotalCount(countRequestBuilder.execute().actionGet().getHits());
512513
}
513514

514515
private long doCount(SearchRequestBuilder searchRequestBuilder, QueryBuilder elasticsearchQuery,
@@ -521,7 +522,7 @@ private long doCount(SearchRequestBuilder searchRequestBuilder, QueryBuilder ela
521522
if (elasticsearchFilter != null) {
522523
searchRequestBuilder.setPostFilter(elasticsearchFilter);
523524
}
524-
return searchRequestBuilder.execute().actionGet().getHits().getTotalHits();
525+
return SearchHitsUtil.getTotalCount(searchRequestBuilder.execute().actionGet().getHits());
525526
}
526527

527528
private <T> SearchRequestBuilder prepareCount(Query query, Class<T> clazz) {
@@ -1202,10 +1203,6 @@ private IndexRequestBuilder prepareIndex(IndexQuery query) {
12021203
indexRequestBuilder.setVersionType(versionType);
12031204
}
12041205

1205-
if (query.getParentId() != null) {
1206-
indexRequestBuilder.setParent(query.getParentId());
1207-
}
1208-
12091206
return indexRequestBuilder;
12101207
} catch (IOException e) {
12111208
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);

0 commit comments

Comments
 (0)