mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 14:52:20 +00:00
Add support for stored fields.
Original Pull Request #2005 Closes #2004
This commit is contained in:
parent
29d21000a9
commit
a22419c418
@ -116,6 +116,7 @@ import org.springframework.util.StringUtils;
|
||||
* @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 @@ public class RequestFactory {
|
||||
query.getFields().forEach(sourceBuilder::fetchField);
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getStoredFields())) {
|
||||
sourceBuilder.storedFields(query.getStoredFields());
|
||||
}
|
||||
|
||||
if (query.getIndicesOptions() != null) {
|
||||
request.indicesOptions(toElasticsearchIndicesOptions(query.getIndicesOptions()));
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ import org.springframework.lang.Nullable;
|
||||
* @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<HighlightBuilder.Field> highlightFields = new ArrayList<>();
|
||||
private Pageable pageable = Pageable.unpaged();
|
||||
@Nullable private List<String> fields = new ArrayList<>();
|
||||
@Nullable protected List<String> storedFields;
|
||||
@Nullable private SourceFilter sourceFilter;
|
||||
@Nullable private CollapseBuilder collapseBuilder;
|
||||
@Nullable private List<IndexBoost> indicesBoost = new ArrayList<>();
|
||||
@ -242,6 +244,23 @@ public class NativeSearchQueryBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQueryBuilder withStoredFields(Collection<String> 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 class NativeSearchQueryBuilder {
|
||||
nativeSearchQuery.setFields(fields);
|
||||
}
|
||||
|
||||
if (storedFields != null) {
|
||||
nativeSearchQuery.setStoredFields(storedFields);
|
||||
}
|
||||
|
||||
if (sourceFilter != null) {
|
||||
nativeSearchQuery.addSourceFilter(sourceFilter);
|
||||
}
|
||||
|
@ -84,6 +84,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Subhobrata Dey
|
||||
* @author Marc Vanbrabant
|
||||
* @author Anton Naydenov
|
||||
* @author vdisk
|
||||
* @since 3.2
|
||||
*/
|
||||
public class MappingElasticsearchConverter
|
||||
@ -1148,6 +1149,11 @@ public class MappingElasticsearchConverter
|
||||
query.setFields(updateFieldNames(fields, persistentEntity));
|
||||
}
|
||||
|
||||
List<String> storedFields = query.getStoredFields();
|
||||
if (!CollectionUtils.isEmpty(storedFields)) {
|
||||
query.setStoredFields(updateFieldNames(storedFields, persistentEntity));
|
||||
}
|
||||
|
||||
SourceFilter sourceFilter = query.getSourceFilter();
|
||||
|
||||
if (sourceFilter != null) {
|
||||
|
@ -43,12 +43,14 @@ import org.springframework.util.Assert;
|
||||
* @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<String> fields = new ArrayList<>();
|
||||
@Nullable protected List<String> storedFields;
|
||||
@Nullable protected SourceFilter sourceFilter;
|
||||
protected float minScore;
|
||||
@Nullable protected Collection<String> ids;
|
||||
@ -109,6 +111,28 @@ public class BaseQuery implements Query {
|
||||
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<String> getStoredFields() {
|
||||
return storedFields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStoredFields(@Nullable List<String> storedFields) {
|
||||
this.storedFields = storedFields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSourceFilter(SourceFilter sourceFilter) {
|
||||
this.sourceFilter = sourceFilter;
|
||||
|
@ -41,6 +41,7 @@ import org.springframework.util.Assert;
|
||||
* @author Farid Azaza
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Peer Mueller
|
||||
* @author vdisk
|
||||
*/
|
||||
public interface Query {
|
||||
|
||||
@ -108,6 +109,28 @@ public interface Query {
|
||||
*/
|
||||
void setFields(List<String> 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<String> getStoredFields();
|
||||
|
||||
/**
|
||||
* Set stored fields to be returned as part of search request
|
||||
*
|
||||
* @param storedFields
|
||||
*/
|
||||
void setStoredFields(@Nullable List<String> storedFields);
|
||||
|
||||
/**
|
||||
* Add source filter to be added as part of search request
|
||||
*
|
||||
|
@ -56,6 +56,7 @@ import org.springframework.lang.Nullable;
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Sascha Woo
|
||||
* @author vdisk
|
||||
*/
|
||||
public class CriteriaQueryMappingUnitTests {
|
||||
|
||||
@ -443,6 +444,22 @@ public class CriteriaQueryMappingUnitTests {
|
||||
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<String> storedFields = query.getStoredFields();
|
||||
softly.assertThat(storedFields).isNotNull();
|
||||
softly.assertThat(storedFields).containsExactly("first-name", "last-name");
|
||||
softly.assertAll();
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region helper functions
|
||||
|
@ -73,6 +73,7 @@ import org.springframework.lang.Nullable;
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Peer Mueller
|
||||
* @author vdisk
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@ -547,6 +548,18 @@ class RequestFactoryTests {
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user