Skip to content

Commit a22419c

Browse files
authored
Add support for stored fields.
Original Pull Request #2005 Closes #2004
1 parent 29d2100 commit a22419c

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactory.java

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
* @author Subhobrata Dey
117117
* @author Farid Faoudi
118118
* @author Peer Mueller
119+
* @author vdisk
119120
* @since 4.0
120121
*/
121122
// todo make package private again after refactoring
@@ -670,6 +671,10 @@ private SearchRequest prepareSearchRequest(Query query, @Nullable Class<?> clazz
670671
query.getFields().forEach(sourceBuilder::fetchField);
671672
}
672673

674+
if (!isEmpty(query.getStoredFields())) {
675+
sourceBuilder.storedFields(query.getStoredFields());
676+
}
677+
673678
if (query.getIndicesOptions() != null) {
674679
request.indicesOptions(toElasticsearchIndicesOptions(query.getIndicesOptions()));
675680
}

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/query/NativeSearchQueryBuilder.java

+23
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Farid Azaza
5555
* @author Peter-Josef Meisch
5656
* @author Peer Mueller
57+
* @author vdisk
5758
*/
5859
public class NativeSearchQueryBuilder {
5960

@@ -67,6 +68,7 @@ public class NativeSearchQueryBuilder {
6768
@Nullable private List<HighlightBuilder.Field> highlightFields = new ArrayList<>();
6869
private Pageable pageable = Pageable.unpaged();
6970
@Nullable private List<String> fields = new ArrayList<>();
71+
@Nullable protected List<String> storedFields;
7072
@Nullable private SourceFilter sourceFilter;
7173
@Nullable private CollapseBuilder collapseBuilder;
7274
@Nullable private List<IndexBoost> indicesBoost = new ArrayList<>();
@@ -242,6 +244,23 @@ public NativeSearchQueryBuilder withFields(String... fields) {
242244
return this;
243245
}
244246

247+
public NativeSearchQueryBuilder withStoredFields(Collection<String> storedFields) {
248+
if (this.storedFields == null) {
249+
this.storedFields = new ArrayList<>(storedFields);
250+
} else {
251+
this.storedFields.addAll(storedFields);
252+
}
253+
return this;
254+
}
255+
256+
public NativeSearchQueryBuilder withStoredFields(String... storedFields) {
257+
if (this.storedFields == null) {
258+
this.storedFields = new ArrayList<>(storedFields.length);
259+
}
260+
Collections.addAll(this.storedFields, storedFields);
261+
return this;
262+
}
263+
245264
public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) {
246265
this.sourceFilter = sourceFilter;
247266
return this;
@@ -342,6 +361,10 @@ public NativeSearchQuery build() {
342361
nativeSearchQuery.setFields(fields);
343362
}
344363

364+
if (storedFields != null) {
365+
nativeSearchQuery.setStoredFields(storedFields);
366+
}
367+
345368
if (sourceFilter != null) {
346369
nativeSearchQuery.addSourceFilter(sourceFilter);
347370
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @author Subhobrata Dey
8585
* @author Marc Vanbrabant
8686
* @author Anton Naydenov
87+
* @author vdisk
8788
* @since 3.2
8889
*/
8990
public class MappingElasticsearchConverter
@@ -1148,6 +1149,11 @@ private void updateFieldsAndSourceFilter(Query query, Class<?> domainClass) {
11481149
query.setFields(updateFieldNames(fields, persistentEntity));
11491150
}
11501151

1152+
List<String> storedFields = query.getStoredFields();
1153+
if (!CollectionUtils.isEmpty(storedFields)) {
1154+
query.setStoredFields(updateFieldNames(storedFields, persistentEntity));
1155+
}
1156+
11511157
SourceFilter sourceFilter = query.getSourceFilter();
11521158

11531159
if (sourceFilter != null) {

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

+24
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
* @author Farid Azaza
4444
* @author Peter-Josef Meisch
4545
* @author Peer Mueller
46+
* @author vdisk
4647
*/
4748
public class BaseQuery implements Query {
4849

4950
protected Pageable pageable = DEFAULT_PAGE;
5051
@Nullable protected Sort sort;
5152
protected List<String> fields = new ArrayList<>();
53+
@Nullable protected List<String> storedFields;
5254
@Nullable protected SourceFilter sourceFilter;
5355
protected float minScore;
5456
@Nullable protected Collection<String> ids;
@@ -109,6 +111,28 @@ public void setFields(List<String> fields) {
109111
this.fields.addAll(fields);
110112
}
111113

114+
@Override
115+
public void addStoredFields(String... storedFields) {
116+
if (storedFields.length == 0) {
117+
return;
118+
}
119+
if (this.storedFields == null) {
120+
this.storedFields = new ArrayList<>(storedFields.length);
121+
}
122+
addAll(this.storedFields, storedFields);
123+
}
124+
125+
@Nullable
126+
@Override
127+
public List<String> getStoredFields() {
128+
return storedFields;
129+
}
130+
131+
@Override
132+
public void setStoredFields(@Nullable List<String> storedFields) {
133+
this.storedFields = storedFields;
134+
}
135+
112136
@Override
113137
public void addSourceFilter(SourceFilter sourceFilter) {
114138
this.sourceFilter = sourceFilter;

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

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Farid Azaza
4242
* @author Peter-Josef Meisch
4343
* @author Peer Mueller
44+
* @author vdisk
4445
*/
4546
public interface Query {
4647

@@ -108,6 +109,28 @@ static Query findAll() {
108109
*/
109110
void setFields(List<String> fields);
110111

112+
/**
113+
* Add stored fields to be added as part of search request
114+
*
115+
* @param storedFields
116+
*/
117+
void addStoredFields(String... storedFields);
118+
119+
/**
120+
* Get stored fields to be returned as part of search request
121+
*
122+
* @return null if not set
123+
*/
124+
@Nullable
125+
List<String> getStoredFields();
126+
127+
/**
128+
* Set stored fields to be returned as part of search request
129+
*
130+
* @param storedFields
131+
*/
132+
void setStoredFields(@Nullable List<String> storedFields);
133+
111134
/**
112135
* Add source filter to be added as part of search request
113136
*

Diff for: src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/CriteriaQueryMappingUnitTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
*
5757
* @author Peter-Josef Meisch
5858
* @author Sascha Woo
59+
* @author vdisk
5960
*/
6061
public class CriteriaQueryMappingUnitTests {
6162

@@ -443,6 +444,22 @@ void shouldMapNamesInSourceFieldsAndSourceFilters() {
443444
softly.assertAll();
444445
}
445446

447+
@Test
448+
@DisplayName("should map names in source stored fields")
449+
void shouldMapNamesInSourceStoredFields() {
450+
451+
Query query = Query.findAll();
452+
query.addStoredFields("firstName", "lastName");
453+
454+
mappingElasticsearchConverter.updateQuery(query, Person.class);
455+
456+
SoftAssertions softly = new SoftAssertions();
457+
List<String> storedFields = query.getStoredFields();
458+
softly.assertThat(storedFields).isNotNull();
459+
softly.assertThat(storedFields).containsExactly("first-name", "last-name");
460+
softly.assertAll();
461+
}
462+
446463
// endregion
447464

448465
// region helper functions

Diff for: src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactoryTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author Peter-Josef Meisch
7474
* @author Roman Puchkovskiy
7575
* @author Peer Mueller
76+
* @author vdisk
7677
*/
7778
@SuppressWarnings("ConstantConditions")
7879
@ExtendWith(MockitoExtension.class)
@@ -547,6 +548,18 @@ void shouldSetRequestCacheFalseOnSearchRequest() {
547548
assertThat(searchRequest.requestCache()).isFalse();
548549
}
549550

551+
@Test
552+
@DisplayName("should set stored fields on SearchRequest")
553+
void shouldSetStoredFieldsOnSearchRequest() {
554+
555+
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withStoredFields("lastName", "location").build();
556+
557+
SearchRequest searchRequest = requestFactory.searchRequest(query, Person.class, IndexCoordinates.of("persons"));
558+
559+
assertThat(searchRequest.source().storedFields()).isNotNull();
560+
assertThat(searchRequest.source().storedFields().fieldNames()).isEqualTo(Arrays.asList("last-name", "current-location"));
561+
}
562+
550563
// region entities
551564
static class Person {
552565
@Nullable @Id String id;

0 commit comments

Comments
 (0)