Skip to content

Add optional fetchSource flag to the SourceFilter. #3014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2014,9 +2014,12 @@ private VersionType retrieveVersionTypeFromPersistentEntity(@Nullable Class<?> c
private SourceConfig getSourceConfig(Query query) {

if (query.getSourceFilter() != null) {
return SourceConfig.of(s -> s //
.filter(sfb -> {
SourceFilter sourceFilter = query.getSourceFilter();
return SourceConfig.of(s -> {
SourceFilter sourceFilter = query.getSourceFilter();
if (sourceFilter.fetchSource() != null) {
s.fetch(sourceFilter.fetchSource());
} else {
s.filter(sfb -> {
String[] includes = sourceFilter.getIncludes();
String[] excludes = sourceFilter.getExcludes();

Expand All @@ -2029,7 +2032,10 @@ private SourceConfig getSourceConfig(Query query) {
}

return sfb;
}));
});
}
return s;
});
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ private void updatePropertiesInFieldsAndSourceFilter(Query query, Class<?> domai
.toArray(new String[] {});
}

query.addSourceFilter(new FetchSourceFilter(includes, excludes));
query.addSourceFilter(new FetchSourceFilter(sourceFilter.fetchSource(), includes, excludes));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
*/
public class FetchSourceFilter implements SourceFilter {

@Nullable private final Boolean fetchSource;
@Nullable private final String[] includes;
@Nullable private final String[] excludes;

/**
* @since 5.2
*/
public static SourceFilter of(@Nullable final String[] includes, @Nullable final String[] excludes) {
return new FetchSourceFilter(includes, excludes);
public static SourceFilter of(@Nullable Boolean fetchSource, @Nullable final String[] includes,
@Nullable final String[] excludes) {
return new FetchSourceFilter(fetchSource, includes, excludes);
}

/**
Expand All @@ -48,11 +50,18 @@ public static SourceFilter of(Function<FetchSourceFilterBuilder, FetchSourceFilt
return builderFunction.apply(new FetchSourceFilterBuilder()).build();
}

public FetchSourceFilter(@Nullable final String[] includes, @Nullable final String[] excludes) {
public FetchSourceFilter(@Nullable Boolean fetchSource, @Nullable final String[] includes,
@Nullable final String[] excludes) {
this.fetchSource = fetchSource;
this.includes = includes;
this.excludes = excludes;
}

@Override
public Boolean fetchSource() {
return fetchSource;
}

@Override
public String[] getIncludes() {
return includes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
public class FetchSourceFilterBuilder {

@Nullable private Boolean fetchSource;
@Nullable private String[] includes;
@Nullable private String[] excludes;

Expand All @@ -38,12 +39,17 @@ public FetchSourceFilterBuilder withExcludes(String... excludes) {
return this;
}

public FetchSourceFilterBuilder withFetchSource(Boolean fetchSource) {
this.fetchSource = fetchSource;
return this;
}

public SourceFilter build() {
if (includes == null)
includes = new String[0];
if (excludes == null)
excludes = new String[0];

return new FetchSourceFilter(includes, excludes);
return new FetchSourceFilter(fetchSource, includes, excludes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public interface SourceFilter {
*/
@Nullable
String[] getExcludes();

/**
* Flag to set the _source parameter in a query to true or false. If this is not null, the values returned from
* getIncludes() and getExcludes() are ignored
*
* @since 5.5
*/
@Nullable
default Boolean fetchSource() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
Expand Down Expand Up @@ -186,6 +187,38 @@ public String[] getExcludes() {
assertThat(entity.getField3()).isNull();
}

@Test // #3009
@DisplayName("should not return any fields when source is set to false")
void shouldNotReturnAnyFieldsWhenSourceIsSetToFalse() {

Query query = Query.findAll();
query.addSourceFilter(FetchSourceFilter.of(b -> b.withFetchSource(false)));

SearchHits<Entity> entities = operations.search(query, Entity.class);

assertThat(entities).hasSize(1);
Entity entity = entities.getSearchHit(0).getContent();
assertThat(entity.getField1()).isNull();
assertThat(entity.getField2()).isNull();
assertThat(entity.getField3()).isNull();
}

@Test // #3009
@DisplayName("should return all fields when source is set to true")
void shouldReturnAllFieldsWhenSourceIsSetToTrue() {

Query query = Query.findAll();
query.addSourceFilter(FetchSourceFilter.of(b -> b.withFetchSource(true)));

SearchHits<Entity> entities = operations.search(query, Entity.class);

assertThat(entities).hasSize(1);
Entity entity = entities.getSearchHit(0).getContent();
assertThat(entity.getField1()).isNotNull();
assertThat(entity.getField2()).isNotNull();
assertThat(entity.getField3()).isNotNull();
}

@Document(indexName = "#{@indexNameProvider.indexName()}")
public static class Entity {
@Nullable
Expand Down