diff --git a/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactory.java b/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactory.java index e3fcff5eb..039cd3afe 100644 --- a/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactory.java +++ b/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactory.java @@ -116,6 +116,7 @@ * @author Subhobrata Dey * @author Farid Faoudi * @author Peer Mueller + * @author vdisk * @since 4.0 */ // todo make package private again after refactoring @@ -670,6 +671,10 @@ private SearchRequest prepareSearchRequest(Query query, @Nullable Class clazz query.getFields().forEach(sourceBuilder::fetchField); } + if (!isEmpty(query.getStoredFields())) { + sourceBuilder.storedFields(query.getStoredFields()); + } + if (query.getIndicesOptions() != null) { request.indicesOptions(toElasticsearchIndicesOptions(query.getIndicesOptions())); } diff --git a/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/query/NativeSearchQueryBuilder.java b/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/query/NativeSearchQueryBuilder.java index e0103e50b..21e25e4b5 100755 --- a/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/query/NativeSearchQueryBuilder.java +++ b/src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/query/NativeSearchQueryBuilder.java @@ -54,6 +54,7 @@ * @author Farid Azaza * @author Peter-Josef Meisch * @author Peer Mueller + * @author vdisk */ public class NativeSearchQueryBuilder { @@ -67,6 +68,7 @@ public class NativeSearchQueryBuilder { @Nullable private List highlightFields = new ArrayList<>(); private Pageable pageable = Pageable.unpaged(); @Nullable private List fields = new ArrayList<>(); + @Nullable protected List storedFields; @Nullable private SourceFilter sourceFilter; @Nullable private CollapseBuilder collapseBuilder; @Nullable private List indicesBoost = new ArrayList<>(); @@ -242,6 +244,23 @@ public NativeSearchQueryBuilder withFields(String... fields) { return this; } + public NativeSearchQueryBuilder withStoredFields(Collection storedFields) { + if (this.storedFields == null) { + this.storedFields = new ArrayList<>(storedFields); + } else { + this.storedFields.addAll(storedFields); + } + return this; + } + + public NativeSearchQueryBuilder withStoredFields(String... storedFields) { + if (this.storedFields == null) { + this.storedFields = new ArrayList<>(storedFields.length); + } + Collections.addAll(this.storedFields, storedFields); + return this; + } + public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) { this.sourceFilter = sourceFilter; return this; @@ -342,6 +361,10 @@ public NativeSearchQuery build() { nativeSearchQuery.setFields(fields); } + if (storedFields != null) { + nativeSearchQuery.setStoredFields(storedFields); + } + if (sourceFilter != null) { nativeSearchQuery.addSourceFilter(sourceFilter); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index a9dd53fdf..c38310985 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -84,6 +84,7 @@ * @author Subhobrata Dey * @author Marc Vanbrabant * @author Anton Naydenov + * @author vdisk * @since 3.2 */ public class MappingElasticsearchConverter @@ -1148,6 +1149,11 @@ private void updateFieldsAndSourceFilter(Query query, Class domainClass) { query.setFields(updateFieldNames(fields, persistentEntity)); } + List storedFields = query.getStoredFields(); + if (!CollectionUtils.isEmpty(storedFields)) { + query.setStoredFields(updateFieldNames(storedFields, persistentEntity)); + } + SourceFilter sourceFilter = query.getSourceFilter(); if (sourceFilter != null) { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java index daa0bd3d5..23a7ac706 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/BaseQuery.java @@ -43,12 +43,14 @@ * @author Farid Azaza * @author Peter-Josef Meisch * @author Peer Mueller + * @author vdisk */ public class BaseQuery implements Query { protected Pageable pageable = DEFAULT_PAGE; @Nullable protected Sort sort; protected List fields = new ArrayList<>(); + @Nullable protected List storedFields; @Nullable protected SourceFilter sourceFilter; protected float minScore; @Nullable protected Collection ids; @@ -109,6 +111,28 @@ public void setFields(List fields) { this.fields.addAll(fields); } + @Override + public void addStoredFields(String... storedFields) { + if (storedFields.length == 0) { + return; + } + if (this.storedFields == null) { + this.storedFields = new ArrayList<>(storedFields.length); + } + addAll(this.storedFields, storedFields); + } + + @Nullable + @Override + public List getStoredFields() { + return storedFields; + } + + @Override + public void setStoredFields(@Nullable List storedFields) { + this.storedFields = storedFields; + } + @Override public void addSourceFilter(SourceFilter sourceFilter) { this.sourceFilter = sourceFilter; diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java index 853958870..5aec88cab 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/Query.java @@ -41,6 +41,7 @@ * @author Farid Azaza * @author Peter-Josef Meisch * @author Peer Mueller + * @author vdisk */ public interface Query { @@ -108,6 +109,28 @@ static Query findAll() { */ void setFields(List fields); + /** + * Add stored fields to be added as part of search request + * + * @param storedFields + */ + void addStoredFields(String... storedFields); + + /** + * Get stored fields to be returned as part of search request + * + * @return null if not set + */ + @Nullable + List getStoredFields(); + + /** + * Set stored fields to be returned as part of search request + * + * @param storedFields + */ + void setStoredFields(@Nullable List storedFields); + /** * Add source filter to be added as part of search request * diff --git a/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/CriteriaQueryMappingUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/CriteriaQueryMappingUnitTests.java index e081979f4..939b13e81 100644 --- a/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/CriteriaQueryMappingUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/CriteriaQueryMappingUnitTests.java @@ -56,6 +56,7 @@ * * @author Peter-Josef Meisch * @author Sascha Woo + * @author vdisk */ public class CriteriaQueryMappingUnitTests { @@ -443,6 +444,22 @@ void shouldMapNamesInSourceFieldsAndSourceFilters() { softly.assertAll(); } + @Test + @DisplayName("should map names in source stored fields") + void shouldMapNamesInSourceStoredFields() { + + Query query = Query.findAll(); + query.addStoredFields("firstName", "lastName"); + + mappingElasticsearchConverter.updateQuery(query, Person.class); + + SoftAssertions softly = new SoftAssertions(); + List storedFields = query.getStoredFields(); + softly.assertThat(storedFields).isNotNull(); + softly.assertThat(storedFields).containsExactly("first-name", "last-name"); + softly.assertAll(); + } + // endregion // region helper functions diff --git a/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactoryTests.java b/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactoryTests.java index c9e91a798..3be2e8882 100644 --- a/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactoryTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RequestFactoryTests.java @@ -73,6 +73,7 @@ * @author Peter-Josef Meisch * @author Roman Puchkovskiy * @author Peer Mueller + * @author vdisk */ @SuppressWarnings("ConstantConditions") @ExtendWith(MockitoExtension.class) @@ -547,6 +548,18 @@ void shouldSetRequestCacheFalseOnSearchRequest() { assertThat(searchRequest.requestCache()).isFalse(); } + @Test + @DisplayName("should set stored fields on SearchRequest") + void shouldSetStoredFieldsOnSearchRequest() { + + Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withStoredFields("lastName", "location").build(); + + SearchRequest searchRequest = requestFactory.searchRequest(query, Person.class, IndexCoordinates.of("persons")); + + assertThat(searchRequest.source().storedFields()).isNotNull(); + assertThat(searchRequest.source().storedFields().fieldNames()).isEqualTo(Arrays.asList("last-name", "current-location")); + } + // region entities static class Person { @Nullable @Id String id;