Skip to content

Commit 198ad0e

Browse files
committed
DATAES-579 - Polishing.
Use Lombok's Data where possible. Extract duplicate code into ResourceUtil and StreamQueries. Generalize multiGet to return List instead of LinkedList. Reorder methods. Reduce properties usage by removing unused properties from test entities. Remove final keyword usage in methods. Formatting. Original pull request: #283.
1 parent 4c206f7 commit 198ad0e

File tree

62 files changed

+718
-1967
lines changed

Some content is hidden

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

62 files changed

+718
-1967
lines changed

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

+44-4
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,81 @@
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.elasticsearch.index.VersionType;
25+
2126
import org.springframework.data.annotation.Persistent;
2227

2328
/**
24-
* Document
29+
* Identifies a domain object to be persisted to Elasticsearch.
2530
*
2631
* @author Rizwan Idrees
2732
* @author Mohsin Husen
2833
* @author Mason Chan
2934
* @author Ivan Greene
35+
* @author Mark Paluch
3036
*/
31-
3237
@Persistent
3338
@Inherited
3439
@Retention(RetentionPolicy.RUNTIME)
35-
@Target({ElementType.TYPE})
40+
@Target({ ElementType.TYPE })
3641
public @interface Document {
3742

43+
/**
44+
* Name of the Elasticsearch index.
45+
* <ul>
46+
* <li>Lowercase only</li>
47+
* <li><Cannot include \, /, *, ?, ", <, >, |, ` ` (space character), ,, #/li>
48+
* <li>Cannot start with -, _, +</li>
49+
* <li>Cannot be . or ..</li>
50+
* <li>Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit
51+
* faster)</li>
52+
* </ul>
53+
*/
3854
String indexName();
3955

56+
/**
57+
* Mapping type name.
58+
*/
4059
String type() default "";
4160

61+
/**
62+
* Use server-side settings when creating the index.
63+
*/
4264
boolean useServerConfiguration() default false;
4365

66+
/**
67+
* Number of shards for the index {@link #indexName()}. Used for index creation.
68+
*/
4469
short shards() default 5;
4570

71+
/**
72+
* Number of replicas for the index {@link #indexName()}. Used for index creation.
73+
*/
4674
short replicas() default 1;
4775

76+
/**
77+
* Refresh interval for the index {@link #indexName()}. Used for index creation.
78+
*/
4879
String refreshInterval() default "1s";
4980

81+
/**
82+
* Index storage type for the index {@link #indexName()}. Used for index creation.
83+
*/
5084
String indexStoreType() default "fs";
5185

86+
/**
87+
* Configuration whether to create an index on repository bootstrapping.
88+
*/
5289
boolean createIndex() default true;
5390

91+
/**
92+
* Configuration of version management.
93+
*/
5494
VersionType versionType() default VersionType.EXTERNAL;
5595
}

Diff for: src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.Charset;
2121
import java.util.ArrayList;
2222
import java.util.Collection;
23-
import java.util.LinkedList;
2423
import java.util.List;
2524

2625
import org.elasticsearch.action.get.GetResponse;
@@ -29,6 +28,7 @@
2928
import org.elasticsearch.action.search.SearchResponse;
3029
import org.elasticsearch.common.document.DocumentField;
3130
import org.elasticsearch.search.SearchHit;
31+
3232
import org.springframework.core.convert.ConversionService;
3333
import org.springframework.core.convert.support.DefaultConversionService;
3434
import org.springframework.data.domain.Pageable;
@@ -189,8 +189,8 @@ public <T> T mapResult(GetResponse response, Class<T> clazz) {
189189
}
190190

191191
@Override
192-
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
193-
LinkedList<T> list = new LinkedList<>();
192+
public <T> List<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
193+
List<T> list = new ArrayList<>();
194194
for (MultiGetItemResponse response : responses.getResponses()) {
195195
if (!response.isFailed() && response.getResponse().isExists()) {
196196
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);

Diff for: src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

+66-55
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.stream.Collectors;
21+
1822
import org.elasticsearch.action.update.UpdateResponse;
1923
import org.elasticsearch.cluster.metadata.AliasMetaData;
2024
import org.elasticsearch.common.Nullable;
25+
2126
import org.springframework.data.domain.Page;
2227
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2328
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
24-
import org.springframework.data.elasticsearch.core.query.*;
29+
import org.springframework.data.elasticsearch.core.query.AliasQuery;
30+
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
31+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
32+
import org.springframework.data.elasticsearch.core.query.GetQuery;
33+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
34+
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
35+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
36+
import org.springframework.data.elasticsearch.core.query.StringQuery;
37+
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
2538
import org.springframework.data.util.CloseableIterator;
2639

27-
import java.util.LinkedList;
28-
import java.util.List;
29-
import java.util.Map;
30-
import java.util.stream.Collectors;
31-
3240
/**
3341
* ElasticsearchOperations
3442
*
@@ -42,9 +50,20 @@
4250
public interface ElasticsearchOperations {
4351

4452
/**
45-
* @return Converter in use
53+
* adding new alias
54+
*
55+
* @param query
56+
* @return
4657
*/
47-
ElasticsearchConverter getElasticsearchConverter();
58+
boolean addAlias(AliasQuery query);
59+
60+
/**
61+
* removing previously created alias
62+
*
63+
* @param query
64+
* @return
65+
*/
66+
boolean removeAlias(AliasQuery query);
4867

4968
/**
5069
* Create an index for a class
@@ -102,7 +121,6 @@ public interface ElasticsearchOperations {
102121
*/
103122
<T> boolean putMapping(Class<T> clazz, Object mappings);
104123

105-
106124
/**
107125
* Get mapping for a class
108126
*
@@ -133,6 +151,15 @@ public interface ElasticsearchOperations {
133151
*/
134152
<T> Map<String, Object> getSetting(Class<T> clazz);
135153

154+
/**
155+
* get all the alias pointing to specified index
156+
*
157+
* @param indexName
158+
* @return
159+
*/
160+
List<AliasMetaData> queryForAlias(String indexName);
161+
162+
<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);
136163

137164
/**
138165
* Execute the query against elasticsearch and return the first returned object
@@ -199,7 +226,8 @@ public interface ElasticsearchOperations {
199226
<T> List<Page<T>> queryForPage(List<SearchQuery> queries, Class<T> clazz);
200227

201228
/**
202-
* Execute the multi-search against elasticsearch and return result as {@link List} of {@link Page} using custom mapper
229+
* Execute the multi-search against elasticsearch and return result as {@link List} of {@link Page} using custom
230+
* mapper
203231
*
204232
* @param queries
205233
* @param clazz
@@ -217,7 +245,8 @@ public interface ElasticsearchOperations {
217245
List<Page<?>> queryForPage(List<SearchQuery> queries, List<Class<?>> classes);
218246

219247
/**
220-
* Execute the multi-search against elasticsearch and return result as {@link List} of {@link Page} using custom mapper
248+
* Execute the multi-search against elasticsearch and return result as {@link List} of {@link Page} using custom
249+
* mapper
221250
*
222251
* @param queries
223252
* @param classes
@@ -255,7 +284,8 @@ public interface ElasticsearchOperations {
255284
/**
256285
* Executes the given {@link CriteriaQuery} against elasticsearch and return result as {@link CloseableIterator}.
257286
* <p>
258-
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of error.
287+
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of
288+
* error.
259289
*
260290
* @param <T> element return type
261291
* @param query
@@ -268,7 +298,8 @@ public interface ElasticsearchOperations {
268298
/**
269299
* Executes the given {@link SearchQuery} against elasticsearch and return result as {@link CloseableIterator}.
270300
* <p>
271-
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of error.
301+
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of
302+
* error.
272303
*
273304
* @param <T> element return type
274305
* @param query
@@ -279,9 +310,11 @@ public interface ElasticsearchOperations {
279310
<T> CloseableIterator<T> stream(SearchQuery query, Class<T> clazz);
280311

281312
/**
282-
* Executes the given {@link SearchQuery} against elasticsearch and return result as {@link CloseableIterator} using custom mapper.
313+
* Executes the given {@link SearchQuery} against elasticsearch and return result as {@link CloseableIterator} using
314+
* custom mapper.
283315
* <p>
284-
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of error.
316+
* Returns a {@link CloseableIterator} that wraps an Elasticsearch scroll context that needs to be closed in case of
317+
* error.
285318
*
286319
* @param <T> element return type
287320
* @param query
@@ -394,7 +427,7 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
394427
* @param clazz
395428
* @return
396429
*/
397-
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
430+
<T> List<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
398431

399432
/**
400433
* Execute a multiGet against elasticsearch for the given ids with MultiGetResultMapper
@@ -404,7 +437,7 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
404437
* @param multiGetResultMapper
405438
* @return
406439
*/
407-
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper multiGetResultMapper);
440+
<T> List<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper multiGetResultMapper);
408441

409442
/**
410443
* Index an object. Will do save or update
@@ -446,14 +479,14 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
446479
*/
447480
String delete(String indexName, String type, String id);
448481

449-
450482
/**
451483
* Delete all records matching the criteria
452484
*
453485
* @param clazz
454486
* @param criteriaQuery
455487
*/
456488
<T> void delete(CriteriaQuery criteriaQuery, Class<T> clazz);
489+
457490
/**
458491
* Delete the one object with provided id
459492
*
@@ -525,15 +558,13 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
525558
* refresh the index
526559
*
527560
* @param indexName
528-
*
529561
*/
530562
void refresh(String indexName);
531563

532564
/**
533565
* refresh the index
534566
*
535567
* @param clazz
536-
*
537568
*/
538569
<T> void refresh(Class<T> clazz);
539570

@@ -542,7 +573,7 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
542573
*
543574
* @param query The search query.
544575
* @param scrollTimeInMillis The time in millisecond for scroll feature
545-
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
576+
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
546577
* @param clazz The class of entity to retrieve.
547578
* @return The scan id for input query.
548579
*/
@@ -553,18 +584,19 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
553584
*
554585
* @param query The search query.
555586
* @param scrollTimeInMillis The time in millisecond for scroll feature
556-
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
587+
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
557588
* @param mapper Custom impl to map result to entities
558589
* @return The scan id for input query.
559590
*/
560-
<T> ScrolledPage<T> startScroll(long scrollTimeInMillis, SearchQuery query, Class<T> clazz, SearchResultMapper mapper);
591+
<T> ScrolledPage<T> startScroll(long scrollTimeInMillis, SearchQuery query, Class<T> clazz,
592+
SearchResultMapper mapper);
561593

562594
/**
563595
* Returns scrolled page for given query
564596
*
565597
* @param criteriaQuery The search query.
566598
* @param scrollTimeInMillis The time in millisecond for scroll feature
567-
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
599+
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
568600
* @param clazz The class of entity to retrieve.
569601
* @return The scan id for input query.
570602
*/
@@ -575,20 +607,22 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
575607
*
576608
* @param criteriaQuery The search query.
577609
* @param scrollTimeInMillis The time in millisecond for scroll feature
578-
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
610+
* {@link org.elasticsearch.action.search.SearchRequestBuilder#setScroll(org.elasticsearch.common.unit.TimeValue)}.
579611
* @param mapper Custom impl to map result to entities
580612
* @return The scan id for input query.
581613
*/
582-
<T> ScrolledPage<T> startScroll(long scrollTimeInMillis, CriteriaQuery criteriaQuery, Class<T> clazz, SearchResultMapper mapper);
583-
614+
<T> ScrolledPage<T> startScroll(long scrollTimeInMillis, CriteriaQuery criteriaQuery, Class<T> clazz,
615+
SearchResultMapper mapper);
584616

585617
<T> ScrolledPage<T> continueScroll(@Nullable String scrollId, long scrollTimeInMillis, Class<T> clazz);
586-
<T> ScrolledPage<T> continueScroll(@Nullable String scrollId, long scrollTimeInMillis, Class<T> clazz, SearchResultMapper mapper);
618+
619+
<T> ScrolledPage<T> continueScroll(@Nullable String scrollId, long scrollTimeInMillis, Class<T> clazz,
620+
SearchResultMapper mapper);
621+
587622
/**
588623
* Clears the search contexts associated with specified scroll ids.
589624
*
590625
* @param scrollId
591-
*
592626
*/
593627
<T> void clearScroll(String scrollId);
594628

@@ -602,33 +636,10 @@ default List<List<?>> queryForList(List<SearchQuery> queries, List<Class<?>> cla
602636
*/
603637
<T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz);
604638

605-
/**
606-
* adding new alias
607-
*
608-
* @param query
609-
* @return
610-
*/
611-
Boolean addAlias(AliasQuery query);
612-
613-
/**
614-
* removing previously created alias
615-
*
616-
* @param query
617-
* @return
618-
*/
619-
Boolean removeAlias(AliasQuery query);
639+
ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz);
620640

621641
/**
622-
* get all the alias pointing to specified index
623-
*
624-
* @param indexName
625-
* @return
642+
* @return Converter in use
626643
*/
627-
List<AliasMetaData> queryForAlias(String indexName);
628-
629-
630-
<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);
631-
632-
633-
ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz);
644+
ElasticsearchConverter getElasticsearchConverter();
634645
}

0 commit comments

Comments
 (0)