mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-04 01:32:11 +00:00
support RuntimeField with no script.
Original Pull Request #2268 Closes #2267
This commit is contained in:
parent
4f4c99ec1f
commit
6b39b39d35
@ -102,6 +102,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Sascha Woo
|
||||
* @author cdalxndr
|
||||
* @since 4.4
|
||||
*/
|
||||
class RequestConverter {
|
||||
@ -1130,12 +1131,16 @@ class RequestConverter {
|
||||
});
|
||||
|
||||
if (!query.getRuntimeFields().isEmpty()) {
|
||||
|
||||
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
|
||||
query.getRuntimeFields().forEach(runtimeField -> {
|
||||
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(RuntimeField.of(rt -> rt //
|
||||
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType())) //
|
||||
.script(s -> s.inline(is -> is.source(runtimeField.getScript()))))));
|
||||
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
|
||||
RuntimeField.Builder builder = rt.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
||||
String script = runtimeField.getScript();
|
||||
if (script != null)
|
||||
builder = builder.script(s -> s.inline(is -> is.source(script)));
|
||||
return builder;
|
||||
});
|
||||
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(esRuntimeField));
|
||||
});
|
||||
bb.runtimeMappings(runtimeMappings);
|
||||
}
|
||||
|
@ -18,25 +18,31 @@ package org.springframework.data.elasticsearch.core;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Defines a runtime field to be added to a Query
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author cdalxndr
|
||||
* @since 4.3
|
||||
*/
|
||||
public class RuntimeField {
|
||||
|
||||
private final String name;
|
||||
private final String type;
|
||||
@Nullable
|
||||
private final String script;
|
||||
|
||||
public RuntimeField(String name, String type, String script) {
|
||||
public RuntimeField(String name, String type){
|
||||
this(name, type, null);
|
||||
}
|
||||
|
||||
public RuntimeField(String name, String type, @Nullable String script) {
|
||||
|
||||
Assert.notNull(name, "name must not be null");
|
||||
Assert.notNull(type, "type must not be null");
|
||||
Assert.notNull(script, "script must not be null");
|
||||
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
@ -51,10 +57,11 @@ public class RuntimeField {
|
||||
* @return the mapping as a Map like it is needed for the Elasticsearch client
|
||||
*/
|
||||
public Map<String, Object> getMapping() {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("type", type);
|
||||
map.put("script", script);
|
||||
if (script != null) {
|
||||
map.put("script", script);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@ -68,7 +75,7 @@ public class RuntimeField {
|
||||
/**
|
||||
* @since 4.4
|
||||
*/
|
||||
public String getScript() {
|
||||
public @Nullable String getScript() {
|
||||
return script;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author cdalxndr
|
||||
*/
|
||||
class RuntimeFieldTest {
|
||||
|
||||
@Test //#2267
|
||||
@DisplayName("should return mapping with script")
|
||||
void testMapping() {
|
||||
RuntimeField runtimeField = new RuntimeField("name", "double", "myscript");
|
||||
Map<String, Object> mapping = runtimeField.getMapping();
|
||||
assertThat(mapping).containsEntry("type", "double")
|
||||
.containsEntry("script", "myscript");
|
||||
}
|
||||
|
||||
@Test //#2267
|
||||
@DisplayName("should return mapping without script")
|
||||
void testMappingNoScript() {
|
||||
RuntimeField runtimeField = new RuntimeField("name", "double");
|
||||
Map<String, Object> mapping = runtimeField.getMapping();
|
||||
assertThat(mapping).containsEntry("type", "double")
|
||||
.doesNotContainKey("script");
|
||||
}
|
||||
|
||||
}
|
@ -38,6 +38,7 @@ import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Peter-Josef Meisch
|
||||
* @author cdalxndr
|
||||
*/
|
||||
@SpringIntegrationTest
|
||||
public abstract class RuntimeFieldsIntegrationTests implements NewElasticsearchClientDevelopment {
|
||||
@ -77,6 +78,22 @@ public abstract class RuntimeFieldsIntegrationTests implements NewElasticsearchC
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("2");
|
||||
}
|
||||
|
||||
@DisabledIf(value = "newElasticsearchClient", disabledReason = "todo #2171, ES issue 298")
|
||||
@Test // #2267
|
||||
@DisplayName("should use runtime-field without script")
|
||||
void shouldUseRuntimeFieldWithoutScript() {
|
||||
|
||||
insert("1", "11", 10);
|
||||
Query query = new CriteriaQuery(new Criteria("description").matches(11.0));
|
||||
RuntimeField runtimeField = new RuntimeField("description", "double");
|
||||
query.addRuntimeField(runtimeField);
|
||||
|
||||
SearchHits<SomethingToBuy> searchHits = operations.search(query, SomethingToBuy.class);
|
||||
|
||||
assertThat(searchHits.getTotalHits()).isEqualTo(1);
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("1");
|
||||
}
|
||||
|
||||
private void insert(String id, String description, double price) {
|
||||
SomethingToBuy entity = new SomethingToBuy();
|
||||
entity.setId(id);
|
||||
|
Loading…
x
Reference in New Issue
Block a user