Skip to content

Commit 69329bc

Browse files
committed
Fix field parameters in search request creation.
Original Pull Request #2739 Closes #2727 (cherry picked from commit f087d5a)
1 parent aaaa18e commit 69329bc

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import co.elastic.clients.elasticsearch._types.mapping.RuntimeField;
3131
import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
3232
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
33+
import co.elastic.clients.elasticsearch._types.query_dsl.FieldAndFormat;
3334
import co.elastic.clients.elasticsearch._types.query_dsl.Like;
3435
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
3536
import co.elastic.clients.elasticsearch.core.*;
@@ -1050,10 +1051,9 @@ public MsearchRequest searchMsearchRequest(
10501051
}
10511052

10521053
if (!isEmpty(query.getFields())) {
1053-
bb.fields(fb -> {
1054-
query.getFields().forEach(fb::field);
1055-
return fb;
1056-
});
1054+
var fieldAndFormats = query.getFields().stream()
1055+
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
1056+
bb.fields(fieldAndFormats);
10571057
}
10581058

10591059
if (!isEmpty(query.getStoredFields())) {
@@ -1169,10 +1169,9 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
11691169
builder.source(getSourceConfig(query));
11701170

11711171
if (!isEmpty(query.getFields())) {
1172-
builder.fields(fb -> {
1173-
query.getFields().forEach(fb::field);
1174-
return fb;
1175-
});
1172+
var fieldAndFormats = query.getFields().stream()
1173+
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
1174+
builder.fields(fieldAndFormats);
11761175
}
11771176

11781177
if (!isEmpty(query.getStoredFields())) {

src/test/java/org/springframework/data/elasticsearch/core/RuntimeFieldsIntegrationTests.java

+83
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.data.elasticsearch.annotations.Field;
3232
import org.springframework.data.elasticsearch.annotations.FieldType;
3333
import org.springframework.data.elasticsearch.annotations.Mapping;
34+
import org.springframework.data.elasticsearch.annotations.ScriptedField;
3435
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3536
import org.springframework.data.elasticsearch.core.query.Criteria;
3637
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
@@ -95,6 +96,88 @@ void shouldUseRuntimeFieldWithoutScript() {
9596
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("1");
9697
}
9798

99+
@Test // #2727
100+
@DisplayName("should return runtime fields values")
101+
void shouldReturnRuntimeFieldsValues() {
102+
103+
var entity = new SAREntity();
104+
entity.setId("42");
105+
entity.setValue(3);
106+
107+
operations.save(entity);
108+
109+
var runtimeField1 = getRuntimeField("scriptedValue1", 2);
110+
var runtimeField2 = getRuntimeField("scriptedValue2", 3);
111+
112+
var query = CriteriaQuery.builder(Criteria.where("value").is(3)).build();
113+
query.addRuntimeField(runtimeField1);
114+
query.addRuntimeField(runtimeField2);
115+
query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("*").build());
116+
query.addFields("scriptedValue1", "scriptedValue2");
117+
var searchHits = operations.search(query, SAREntity.class);
118+
119+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
120+
var foundEntity = searchHits.getSearchHit(0).getContent();
121+
assertThat(foundEntity.value).isEqualTo(3);
122+
assertThat(foundEntity.getScriptedValue1()).isEqualTo(6);
123+
assertThat(foundEntity.getScriptedValue2()).isEqualTo(9);
124+
}
125+
126+
@Document(indexName = "#{@indexNameProvider.indexName()}-sar")
127+
public static class SAREntity {
128+
@Nullable private String id;
129+
@Field(type = FieldType.Integer)
130+
@Nullable Integer value;
131+
@ScriptedField
132+
@Nullable Integer scriptedValue1;
133+
@ScriptedField
134+
@Nullable Integer scriptedValue2;
135+
// getter and setter omitted
136+
137+
@Nullable
138+
public String getId() {
139+
return id;
140+
}
141+
142+
public void setId(@Nullable String id) {
143+
this.id = id;
144+
}
145+
146+
@Nullable
147+
public Integer getValue() {
148+
return value;
149+
}
150+
151+
public void setValue(@Nullable Integer value) {
152+
this.value = value;
153+
}
154+
155+
@Nullable
156+
public Integer getScriptedValue1() {
157+
return scriptedValue1;
158+
}
159+
160+
public void setScriptedValue1(@Nullable Integer scriptedValue1) {
161+
this.scriptedValue1 = scriptedValue1;
162+
}
163+
164+
@Nullable
165+
public Integer getScriptedValue2() {
166+
return scriptedValue2;
167+
}
168+
169+
public void setScriptedValue2(@Nullable Integer scriptedValue2) {
170+
this.scriptedValue2 = scriptedValue2;
171+
}
172+
}
173+
174+
private static RuntimeField getRuntimeField(String fieldName, int factor) {
175+
return new RuntimeField(
176+
fieldName,
177+
"long",
178+
String.format("emit(doc['value'].size() > 0 ? doc['value'].value * %d : 0)", factor));
179+
}
180+
98181
@Test // #2431
99182
@DisplayName("should return value from runtime field defined in mapping")
100183
void shouldReturnValueFromRuntimeFieldDefinedInMapping() {

0 commit comments

Comments
 (0)