Skip to content

Commit 2e9bef0

Browse files
authored
Configure index settings with @setting annotation.
Original Pull Request #1748 Closes #1719
1 parent 13ab2b9 commit 2e9bef0

File tree

74 files changed

+1292
-635
lines changed

Some content is hidden

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

74 files changed

+1292
-635
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-clients.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Spring Data Elasticsearch operates upon an Elasticsearch client that is connecte
88
[[elasticsearch.clients.transport]]
99
== Transport Client
1010

11-
WARNING: The well known `TransportClient` is deprecated as of Elasticsearch 7 and will be removed in Elasticsearch 8. (https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html[see the Elasticsearch documentation]). Spring Data Elasticsearch will support the `TransportClient` as long as it is available in the used
11+
WARNING: The `TransportClient` is deprecated as of Elasticsearch 7 and will be removed in Elasticsearch 8. (https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html[see the Elasticsearch documentation]). Spring Data Elasticsearch will support the `TransportClient` as long as it is available in the used
1212
Elasticsearch <<elasticsearch.versions,version>> but has deprecated the classes using it since version 4.0.
1313

1414
We strongly recommend to use the <<elasticsearch.clients.rest>> instead of the `TransportClient`.

Diff for: src/main/asciidoc/reference/elasticsearch-migration-guide-4.1-4.2.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This section describes breaking changes from version 4.1.x to 4.2.x and how remo
66
[[elasticsearch-migration-guide-4.1-4.2.deprecations]]
77
== Deprecations
88

9+
=== @Document parameters
10+
11+
The parameters of the `@Document` annotation that are relevant for the index settings (`useServerConfiguration`, `shards`. `replicas`, `refreshIntervall` and `indexStoretype`) have been moved to the `@Setting` annotation. Use in `@Document` is still possible but deprecated.
12+
913
[[elasticsearch-migration-guide-4.1-4.2.removal]]
1014
== Removals
1115

Diff for: src/main/asciidoc/reference/elasticsearch-misc.adoc

+51-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@
44
This chapter covers additional support for Elasticsearch operations that cannot be directly accessed via the repository interface.
55
It is recommended to add those operations as custom implementation as described in <<repositories.custom-implementations>> .
66

7+
[[elasticsearc.misc.index.settings]]
8+
== Index settings
9+
10+
When creating Elasticsearch indices with Spring Data Elasticsearch different index settings can be defined by using the `@Setting` annotation. The following arguments are available:
11+
12+
* `useServerConfiguration` does not send any settings parameters, so the Elasticsearch server configuration determines them.
13+
* `settingPath` refers to a JSON file defining the settings that must be resolvable in the classpath
14+
* `shards` the number of shards to use, defaults to _1_
15+
* `replicas` the number of replicas, defaults to _1_
16+
* `refreshIntervall`, defaults to _"1s"_
17+
* `indexStoreType`, defaults to _"fs"_
18+
19+
20+
It is as well possible to define https://www.elastic.co/guide/en/elasticsearch/reference/7.11/index-modules-index-sorting.html[index sorting] (check the linked Elasticsearch documentation for the possible field types and values):
21+
22+
====
23+
[source,java]
24+
----
25+
@Document(indexName = "entities")
26+
@Setting(
27+
sortFields = { "secondField", "firstField" }, <.>
28+
sortModes = { Setting.SortMode.max, Setting.SortMode.min }, <.>
29+
sortOrders = { Setting.SortOrder.desc, Setting.SortOrder.asc },
30+
sortMissingValues = { Setting.SortMissing._last, Setting.SortMissing._first })
31+
class Entity {
32+
@Nullable
33+
@Id private String id;
34+
35+
@Nullable
36+
@Field(name = "first_field", type = FieldType.Keyword)
37+
private String firstField;
38+
39+
@Nullable @Field(name = "second_field", type = FieldType.Keyword)
40+
private String secondField;
41+
42+
// getter and setter...
43+
}
44+
----
45+
<.> when defining sort fields, use the name of the Java property (_firstField_), not the name that might be defined for Elasticsearch (_first_field_)
46+
<.> `sortModes`, `sortOrders` and `sortMissingValues` are optional, but if they are set, the number of entries must match the number of `sortFields` elements
47+
====
48+
749
[[elasticsearch.misc.filter]]
850
== Filter Builder
951

@@ -20,7 +62,7 @@ SearchQuery searchQuery = new NativeSearchQueryBuilder()
2062
.withQuery(matchAllQuery())
2163
.withFilter(boolFilter().must(termFilter("id", documentId)))
2264
.build();
23-
65+
2466
Page<SampleEntity> sampleEntities = operations.searchForPage(searchQuery, SampleEntity.class, index);
2567
----
2668
====
@@ -31,6 +73,7 @@ Page<SampleEntity> sampleEntities = operations.searchForPage(searchQuery, Sample
3173
Elasticsearch has a scroll API for getting big result set in chunks.
3274
This is internally used by Spring Data Elasticsearch to provide the implementations of the `<T> SearchHitsIterator<T> SearchOperations.searchForStream(Query query, Class<T> clazz, IndexCoordinates index)` method.
3375

76+
====
3477
[source,java]
3578
----
3679
IndexCoordinates index = IndexCoordinates.of("sample-index");
@@ -50,9 +93,11 @@ while (stream.hasNext()) {
5093
5194
stream.close();
5295
----
96+
====
5397

5498
There are no methods in the `SearchOperations` API to access the scroll id, if it should be necessary to access this, the following methods of the `ElasticsearchRestTemplate` can be used:
5599

100+
====
56101
[source,java]
57102
----
58103
@@ -77,10 +122,12 @@ while (scroll.hasSearchHits()) {
77122
}
78123
template.searchScrollClear(scrollId);
79124
----
125+
====
80126

81127
To use the Scroll API with repository methods, the return type must defined as `Stream` in the Elasticsearch Repository.
82128
The implementation of the method will then use the scroll methods from the ElasticsearchTemplate.
83129

130+
====
84131
[source,java]
85132
----
86133
interface SampleEntityRepository extends Repository<SampleEntity, String> {
@@ -89,6 +136,7 @@ interface SampleEntityRepository extends Repository<SampleEntity, String> {
89136
90137
}
91138
----
139+
====
92140

93141
[[elasticsearch.misc.sorts]]
94142
== Sort options
@@ -97,7 +145,9 @@ In addition to the default sort options described <<repositories.paging-and-sort
97145

98146
If the class to be retrieved has a `GeoPoint` property named _location_, the following `Sort` would sort the results by distance to the given point:
99147

148+
====
100149
[source,java]
101150
----
102151
Sort.by(new GeoDistanceOrder("location", new GeoPoint(48.137154, 11.5761247)))
103152
----
153+
====

Diff for: src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

+3-11
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ The most important attributes are:
3636
This can contain a SpEL template expression like `"log-#{T(java.time.LocalDate).now().toString()}"`
3737
** `type`: [line-through]#the mapping type.
3838
If not set, the lowercased simple name of the class is used.# (deprecated since version 4.0)
39-
** `shards`: the number of shards for the index.
40-
** `replicas`: the number of replicas for the index.
41-
** `refreshIntervall`: Refresh interval for the index.
42-
Used for index creation.
43-
Default value is _"1s"_.
44-
** `indexStoreType`: Index storage type for the index.
45-
Used for index creation.
46-
Default value is _"fs"_.
4739
** `createIndex`: flag whether to create an index on repository bootstrapping.
4840
Default value is _true_.
4941
See <<elasticsearch.repositories.autocreation>>
@@ -71,13 +63,13 @@ The mapping metadata infrastructure is defined in a separate spring-data-commons
7163
==== Date format mapping
7264

7365
Properties that derive from `TemporalAccessor` or are of type `java.util.Date` must either have a `@Field` annotation
74-
of type `FieldType.Date` or a custom converter must be registered for this type. This paragraph describes the use of
66+
of type `FieldType.Date` or a custom converter must be registered for this type. This paragraph describes the use of
7567
`FieldType.Date`.
7668

77-
There are two attributes of the `@Field` annotation that define which date format information is written to the
69+
There are two attributes of the `@Field` annotation that define which date format information is written to the
7870
mapping (also see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats[Elasticsearch Built In Formats] and https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats[Elasticsearch Custom Date Formats])
7971

80-
The `format` attributes is used to define at least one of the predefined formats. If it is not defined, then a
72+
The `format` attributes is used to define at least one of the predefined formats. If it is not defined, then a
8173
default value of __date_optional_time_ and _epoch_millis_ is used.
8274

8375
The `pattern` attribute can be used to add additional custom format strings. If you want to use only custom date formats, you must set the `format` property to empty `{}`.

Diff for: src/main/asciidoc/reference/elasticsearch-operations.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ The default implementations of the interfaces offer:
2121
====
2222
.Index management and automatic creation of indices and mappings.
2323
24-
The `IndexOperations` interface and the provided implementation which can be obtained from an `ElasticsearchOperations` instance - for example with a call to `operations.indexOps(clazz)`- give the user the ability to create indices, put mappings or store template and alias information in the Elasticsearch cluster.
24+
The `IndexOperations` interface and the provided implementation which can be obtained from an `ElasticsearchOperations` instance - for example with a call to `operations.indexOps(clazz)`- give the user the ability to create indices, put mappings or store template and alias information in the Elasticsearch cluster. Details of the index that will be created
25+
can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
2526
2627
**None of these operations are done automatically** by the implementations of `IndexOperations` or `ElasticsearchOperations`. It is the user's responsibility to call the methods.
2728

Diff for: src/main/asciidoc/reference/elasticsearch-repositories.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Book {
3131

3232
The `@Document` annotation has an argument `createIndex`. If this argument is set to true - which is the default value - Spring Data Elasticsearch will during bootstrapping the repository support on application startup check if the index defined by the `@Document` annotation exists.
3333

34-
If it does not exist, the index will be created and the mappings derived from the entity's annotations (see <<elasticsearch.mapping>>) will be written to the newly created index.
34+
If it does not exist, the index will be created and the mappings derived from the entity's annotations (see <<elasticsearch.mapping>>) will be written to the newly created index. Details of the index that will be created can be set by using the `@Setting` annotation, refer to <<elasticsearc.misc.index.settings>> for further information.
3535

3636
include::elasticsearch-repository-queries.adoc[leveloffset=+1]
3737

@@ -131,7 +131,7 @@ class ProductService {
131131
public void setRepository(ProductRepository repository) {
132132
this.repository = repository;
133133
}
134-
}
134+
}
135135
----
136136
<1> Create a component by using the same calls as are used in the <<elasticsearch.operations>> chapter.
137137
<2> Let the CDI framework inject the Repository into your class.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch;
17+
18+
import org.springframework.core.NestedRuntimeException;
19+
20+
/**
21+
* @author Peter-Josef Meisch
22+
*/
23+
public class ResourceFailureException extends NestedRuntimeException {
24+
public ResourceFailureException(String msg) {
25+
super(msg);
26+
}
27+
28+
public ResourceFailureException(String msg, Throwable cause) {
29+
super(msg, cause);
30+
}
31+
}

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

+16
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,45 @@
5555

5656
/**
5757
* Use server-side settings when creating the index.
58+
*
59+
* @deprecated since 4.2, use the {@link Setting} annotation to configure settings
5860
*/
61+
@Deprecated
5962
boolean useServerConfiguration() default false;
6063

6164
/**
6265
* Number of shards for the index {@link #indexName()}. Used for index creation. <br/>
6366
* With version 4.0, the default value is changed from 5 to 1 to reflect the change in the default settings of
6467
* Elasticsearch which changed to 1 as well in Elasticsearch 7.0.
68+
* ComposableAnnotationsUnitTest.documentAnnotationShouldBeComposable:60
69+
*
70+
* @deprecated since 4.2, use the {@link Setting} annotation to configure settings
6571
*/
72+
@Deprecated
6673
short shards() default 1;
6774

6875
/**
6976
* Number of replicas for the index {@link #indexName()}. Used for index creation.
77+
*
78+
* @deprecated since 4.2, use the {@link Setting} annotation to configure settings
7079
*/
80+
@Deprecated
7181
short replicas() default 1;
7282

7383
/**
7484
* Refresh interval for the index {@link #indexName()}. Used for index creation.
85+
*
86+
* @deprecated since 4.2, use the {@link Setting} annotation to configure settings
7587
*/
88+
@Deprecated
7689
String refreshInterval() default "1s";
7790

7891
/**
7992
* Index storage type for the index {@link #indexName()}. Used for index creation.
93+
*
94+
* @deprecated since 4.2, use the {@link Setting} annotation to configure settings
8095
*/
96+
@Deprecated
8197
String indexStoreType() default "fs";
8298

8399
/**

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/Setting.java

+76-2
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,96 @@
1515
*/
1616
package org.springframework.data.elasticsearch.annotations;
1717

18-
import java.lang.annotation.*;
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Inherited;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
1923

2024
import org.springframework.data.annotation.Persistent;
2125

2226
/**
2327
* Elasticsearch Setting
2428
*
2529
* @author Mohsin Husen
30+
* @author Peter-Josef Meisch
2631
*/
2732

2833
@Persistent
2934
@Inherited
3035
@Retention(RetentionPolicy.RUNTIME)
31-
@Target({ElementType.TYPE})
36+
@Target({ ElementType.TYPE })
3237
public @interface Setting {
3338

39+
/**
40+
* Resource path for a settings configuration
41+
*/
3442
String settingPath() default "";
3543

44+
/**
45+
* Use server-side settings when creating the index.
46+
*/
47+
boolean useServerConfiguration() default false;
48+
49+
/**
50+
* Number of shards for the index. Used for index creation. <br/>
51+
* With version 4.0, the default value is changed from 5 to 1 to reflect the change in the default settings of
52+
* Elasticsearch which changed to 1 as well in Elasticsearch 7.0.
53+
*/
54+
short shards() default 1;
55+
56+
/**
57+
* Number of replicas for the index. Used for index creation.
58+
*/
59+
short replicas() default 1;
60+
61+
/**
62+
* Refresh interval for the index. Used for index creation.
63+
*/
64+
String refreshInterval() default "1s";
65+
66+
/**
67+
* Index storage type for the index. Used for index creation.
68+
*/
69+
String indexStoreType() default "fs";
70+
71+
/**
72+
* fields to define an index sorting
73+
*
74+
* @since 4.2
75+
*/
76+
String[] sortFields() default {};
77+
78+
/**
79+
* defines the order for {@link #sortFields()}. If present, it must have the same number of elements
80+
*
81+
* @since 4.2
82+
*/
83+
SortOrder[] sortOrders() default {};
84+
85+
/**
86+
* defines the mode for {@link #sortFields()}. If present, it must have the same number of elements
87+
*
88+
* @since 4.2
89+
*/
90+
SortMode[] sortModes() default {};
91+
92+
/**
93+
* defines the missing value for {@link #sortFields()}. If present, it must have the same number of elements
94+
*
95+
* @since 4.2
96+
*/
97+
SortMissing[] sortMissingValues() default {};
98+
99+
enum SortOrder {
100+
asc, desc
101+
}
102+
103+
enum SortMode {
104+
min, max
105+
}
106+
107+
enum SortMissing {
108+
_last, _first
109+
}
36110
}

0 commit comments

Comments
 (0)