mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-13 23:52:10 +00:00
parent
6b39b39d35
commit
68ba4cd39b
@ -32,16 +32,7 @@ import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
|
|||||||
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
||||||
import co.elastic.clients.elasticsearch._types.query_dsl.Like;
|
import co.elastic.clients.elasticsearch._types.query_dsl.Like;
|
||||||
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
|
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
|
||||||
import co.elastic.clients.elasticsearch.core.BulkRequest;
|
import co.elastic.clients.elasticsearch.core.*;
|
||||||
import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.DeleteRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.GetRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.IndexRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.MgetRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.MsearchRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.UpdateRequest;
|
|
||||||
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
||||||
import co.elastic.clients.elasticsearch.core.bulk.CreateOperation;
|
import co.elastic.clients.elasticsearch.core.bulk.CreateOperation;
|
||||||
import co.elastic.clients.elasticsearch.core.bulk.IndexOperation;
|
import co.elastic.clients.elasticsearch.core.bulk.IndexOperation;
|
||||||
@ -52,6 +43,7 @@ import co.elastic.clients.elasticsearch.core.search.Highlight;
|
|||||||
import co.elastic.clients.elasticsearch.core.search.Rescore;
|
import co.elastic.clients.elasticsearch.core.search.Rescore;
|
||||||
import co.elastic.clients.elasticsearch.core.search.SourceConfig;
|
import co.elastic.clients.elasticsearch.core.search.SourceConfig;
|
||||||
import co.elastic.clients.elasticsearch.indices.*;
|
import co.elastic.clients.elasticsearch.indices.*;
|
||||||
|
import co.elastic.clients.elasticsearch.indices.ExistsRequest;
|
||||||
import co.elastic.clients.elasticsearch.indices.update_aliases.Action;
|
import co.elastic.clients.elasticsearch.indices.update_aliases.Action;
|
||||||
import co.elastic.clients.json.JsonData;
|
import co.elastic.clients.json.JsonData;
|
||||||
import co.elastic.clients.json.JsonpDeserializer;
|
import co.elastic.clients.json.JsonpDeserializer;
|
||||||
@ -1134,10 +1126,13 @@ class RequestConverter {
|
|||||||
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
|
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
|
||||||
query.getRuntimeFields().forEach(runtimeField -> {
|
query.getRuntimeFields().forEach(runtimeField -> {
|
||||||
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
|
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
|
||||||
RuntimeField.Builder builder = rt.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
RuntimeField.Builder builder = rt
|
||||||
|
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType()));
|
||||||
String script = runtimeField.getScript();
|
String script = runtimeField.getScript();
|
||||||
if (script != null)
|
|
||||||
|
if (script != null) {
|
||||||
builder = builder.script(s -> s.inline(is -> is.source(script)));
|
builder = builder.script(s -> s.inline(is -> is.source(script)));
|
||||||
|
}
|
||||||
return builder;
|
return builder;
|
||||||
});
|
});
|
||||||
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(esRuntimeField));
|
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(esRuntimeField));
|
||||||
@ -1168,7 +1163,6 @@ class RequestConverter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, IndexCoordinates indexCoordinates,
|
private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, IndexCoordinates indexCoordinates,
|
||||||
SearchRequest.Builder builder, boolean forCount, boolean useScroll) {
|
SearchRequest.Builder builder, boolean forCount, boolean useScroll) {
|
||||||
|
|
||||||
@ -1330,9 +1324,9 @@ class RequestConverter {
|
|||||||
private void addHighlight(Query query, MultisearchBody.Builder builder) {
|
private void addHighlight(Query query, MultisearchBody.Builder builder) {
|
||||||
|
|
||||||
Highlight highlight = query.getHighlightQuery()
|
Highlight highlight = query.getHighlightQuery()
|
||||||
.map(highlightQuery -> new HighlightQueryBuilder(elasticsearchConverter.getMappingContext())
|
.map(highlightQuery -> new HighlightQueryBuilder(elasticsearchConverter.getMappingContext())
|
||||||
.getHighlight(highlightQuery.getHighlight(), highlightQuery.getType()))
|
.getHighlight(highlightQuery.getHighlight(), highlightQuery.getType()))
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
|
|
||||||
builder.highlight(highlight);
|
builder.highlight(highlight);
|
||||||
}
|
}
|
||||||
@ -1428,9 +1422,9 @@ class RequestConverter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
builder //
|
builder //
|
||||||
.suggest(query.getSuggester()) //
|
.suggest(query.getSuggester()) //
|
||||||
.collapse(query.getFieldCollapse()) //
|
.collapse(query.getFieldCollapse()) //
|
||||||
.sort(query.getSortOptions());
|
.sort(query.getSortOptions());
|
||||||
|
|
||||||
if (!isEmpty(query.getAggregations())) {
|
if (!isEmpty(query.getAggregations())) {
|
||||||
builder.aggregations(query.getAggregations());
|
builder.aggregations(query.getAggregations());
|
||||||
|
@ -32,10 +32,9 @@ public class RuntimeField {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String type;
|
private final String type;
|
||||||
@Nullable
|
@Nullable private final String script;
|
||||||
private final String script;
|
|
||||||
|
|
||||||
public RuntimeField(String name, String type){
|
public RuntimeField(String name, String type) {
|
||||||
this(name, type, null);
|
this(name, type, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +58,7 @@ public class RuntimeField {
|
|||||||
public Map<String, Object> getMapping() {
|
public Map<String, Object> getMapping() {
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("type", type);
|
map.put("type", type);
|
||||||
|
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
map.put("script", script);
|
map.put("script", script);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -10,25 +24,26 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author cdalxndr
|
* @author cdalxndr
|
||||||
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
class RuntimeFieldTest {
|
class RuntimeFieldTest {
|
||||||
|
|
||||||
@Test //#2267
|
@Test // #2267
|
||||||
@DisplayName("should return mapping with script")
|
@DisplayName("should return mapping with script")
|
||||||
void testMapping() {
|
void testMapping() {
|
||||||
|
|
||||||
RuntimeField runtimeField = new RuntimeField("name", "double", "myscript");
|
RuntimeField runtimeField = new RuntimeField("name", "double", "myscript");
|
||||||
Map<String, Object> mapping = runtimeField.getMapping();
|
Map<String, Object> mapping = runtimeField.getMapping();
|
||||||
assertThat(mapping).containsEntry("type", "double")
|
assertThat(mapping).containsEntry("type", "double").containsEntry("script", "myscript");
|
||||||
.containsEntry("script", "myscript");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test //#2267
|
@Test // #2267
|
||||||
@DisplayName("should return mapping without script")
|
@DisplayName("should return mapping without script")
|
||||||
void testMappingNoScript() {
|
void testMappingNoScript() {
|
||||||
|
|
||||||
RuntimeField runtimeField = new RuntimeField("name", "double");
|
RuntimeField runtimeField = new RuntimeField("name", "double");
|
||||||
Map<String, Object> mapping = runtimeField.getMapping();
|
Map<String, Object> mapping = runtimeField.getMapping();
|
||||||
assertThat(mapping).containsEntry("type", "double")
|
assertThat(mapping).containsEntry("type", "double").doesNotContainKey("script");
|
||||||
.doesNotContainKey("script");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user