Skip to content

Commit 6b39b39

Browse files
authored
support RuntimeField with no script.
Original Pull Request #2268 Closes #2267
1 parent 4f4c99e commit 6b39b39

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
*
103103
* @author Peter-Josef Meisch
104104
* @author Sascha Woo
105+
* @author cdalxndr
105106
* @since 4.4
106107
*/
107108
class RequestConverter {
@@ -1130,12 +1131,16 @@ public MsearchRequest searchMsearchRequest(
11301131
});
11311132

11321133
if (!query.getRuntimeFields().isEmpty()) {
1133-
11341134
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
11351135
query.getRuntimeFields().forEach(runtimeField -> {
1136-
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(RuntimeField.of(rt -> rt //
1137-
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType())) //
1138-
.script(s -> s.inline(is -> is.source(runtimeField.getScript()))))));
1136+
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
1137+
RuntimeField.Builder builder = rt.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
1138+
String script = runtimeField.getScript();
1139+
if (script != null)
1140+
builder = builder.script(s -> s.inline(is -> is.source(script)));
1141+
return builder;
1142+
});
1143+
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(esRuntimeField));
11391144
});
11401145
bb.runtimeMappings(runtimeMappings);
11411146
}

Diff for: src/main/java/org/springframework/data/elasticsearch/core/RuntimeField.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,31 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020

21+
import org.springframework.lang.Nullable;
2122
import org.springframework.util.Assert;
2223

2324
/**
2425
* Defines a runtime field to be added to a Query
2526
*
2627
* @author Peter-Josef Meisch
28+
* @author cdalxndr
2729
* @since 4.3
2830
*/
2931
public class RuntimeField {
3032

3133
private final String name;
3234
private final String type;
35+
@Nullable
3336
private final String script;
3437

35-
public RuntimeField(String name, String type, String script) {
38+
public RuntimeField(String name, String type){
39+
this(name, type, null);
40+
}
41+
42+
public RuntimeField(String name, String type, @Nullable String script) {
3643

3744
Assert.notNull(name, "name must not be null");
3845
Assert.notNull(type, "type must not be null");
39-
Assert.notNull(script, "script must not be null");
4046

4147
this.name = name;
4248
this.type = type;
@@ -51,10 +57,11 @@ public String getName() {
5157
* @return the mapping as a Map like it is needed for the Elasticsearch client
5258
*/
5359
public Map<String, Object> getMapping() {
54-
5560
Map<String, Object> map = new HashMap<>();
5661
map.put("type", type);
57-
map.put("script", script);
62+
if (script != null) {
63+
map.put("script", script);
64+
}
5865
return map;
5966
}
6067

@@ -68,7 +75,7 @@ public String getType() {
6875
/**
6976
* @since 4.4
7077
*/
71-
public String getScript() {
78+
public @Nullable String getScript() {
7279
return script;
7380
}
7481
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.data.elasticsearch.core;
2+
3+
4+
import static org.assertj.core.api.Assertions.*;
5+
6+
import java.util.Map;
7+
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
11+
/**
12+
* @author cdalxndr
13+
*/
14+
class RuntimeFieldTest {
15+
16+
@Test //#2267
17+
@DisplayName("should return mapping with script")
18+
void testMapping() {
19+
RuntimeField runtimeField = new RuntimeField("name", "double", "myscript");
20+
Map<String, Object> mapping = runtimeField.getMapping();
21+
assertThat(mapping).containsEntry("type", "double")
22+
.containsEntry("script", "myscript");
23+
}
24+
25+
@Test //#2267
26+
@DisplayName("should return mapping without script")
27+
void testMappingNoScript() {
28+
RuntimeField runtimeField = new RuntimeField("name", "double");
29+
Map<String, Object> mapping = runtimeField.getMapping();
30+
assertThat(mapping).containsEntry("type", "double")
31+
.doesNotContainKey("script");
32+
}
33+
34+
}

Diff for: src/test/java/org/springframework/data/elasticsearch/core/RuntimeFieldsIntegrationTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* @author Peter-Josef Meisch
41+
* @author cdalxndr
4142
*/
4243
@SpringIntegrationTest
4344
public abstract class RuntimeFieldsIntegrationTests implements NewElasticsearchClientDevelopment {
@@ -77,6 +78,22 @@ void shouldUseRuntimeFieldFromQueryInSearch() {
7778
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("2");
7879
}
7980

81+
@DisabledIf(value = "newElasticsearchClient", disabledReason = "todo #2171, ES issue 298")
82+
@Test // #2267
83+
@DisplayName("should use runtime-field without script")
84+
void shouldUseRuntimeFieldWithoutScript() {
85+
86+
insert("1", "11", 10);
87+
Query query = new CriteriaQuery(new Criteria("description").matches(11.0));
88+
RuntimeField runtimeField = new RuntimeField("description", "double");
89+
query.addRuntimeField(runtimeField);
90+
91+
SearchHits<SomethingToBuy> searchHits = operations.search(query, SomethingToBuy.class);
92+
93+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
94+
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("1");
95+
}
96+
8097
private void insert(String id, String description, double price) {
8198
SomethingToBuy entity = new SomethingToBuy();
8299
entity.setId(id);

0 commit comments

Comments
 (0)