Skip to content

Add support for stored fields #2005

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 2 commits into from
Nov 21, 2021
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 @@ -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
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Farid Azaza
* @author Peter-Josef Meisch
* @author Peer Mueller
* @author vdisk
*/
public class NativeSearchQueryBuilder {

Expand All @@ -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<>();
Expand Down Expand Up @@ -242,6 +244,23 @@ public NativeSearchQueryBuilder withFields(String... fields) {
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;
Expand Down Expand Up @@ -342,6 +361,10 @@ public NativeSearchQuery build() {
nativeSearchQuery.setFields(fields);
}

if (storedFields != null) {
nativeSearchQuery.setStoredFields(storedFields);
}

if (sourceFilter != null) {
nativeSearchQuery.addSourceFilter(sourceFilter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
* @author Subhobrata Dey
* @author Marc Vanbrabant
* @author Anton Naydenov
* @author vdisk
* @since 3.2
*/
public class MappingElasticsearchConverter
Expand Down Expand Up @@ -1148,6 +1149,11 @@ private void updateFieldsAndSourceFilter(Query query, Class<?> domainClass) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> fields = new ArrayList<>();
@Nullable protected List<String> storedFields;
@Nullable protected SourceFilter sourceFilter;
protected float minScore;
@Nullable protected Collection<String> ids;
Expand Down Expand Up @@ -109,6 +111,28 @@ public void setFields(List<String> 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<String> getStoredFields() {
return storedFields;
}

@Override
public void setStoredFields(@Nullable List<String> storedFields) {
this.storedFields = storedFields;
}

@Override
public void addSourceFilter(SourceFilter sourceFilter) {
this.sourceFilter = sourceFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Farid Azaza
* @author Peter-Josef Meisch
* @author Peer Mueller
* @author vdisk
*/
public interface Query {

Expand Down Expand Up @@ -108,6 +109,28 @@ static Query findAll() {
*/
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*
* @author Peter-Josef Meisch
* @author Sascha Woo
* @author vdisk
*/
public class CriteriaQueryMappingUnitTests {

Expand Down Expand Up @@ -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<String> storedFields = query.getStoredFields();
softly.assertThat(storedFields).isNotNull();
softly.assertThat(storedFields).containsExactly("first-name", "last-name");
softly.assertAll();
}

// endregion

// region helper functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* @author Peter-Josef Meisch
* @author Roman Puchkovskiy
* @author Peer Mueller
* @author vdisk
*/
@SuppressWarnings("ConstantConditions")
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -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;
Expand Down