Polishing.

See #2267
This commit is contained in:
Peter-Josef Meisch 2022-08-11 06:53:07 +02:00
parent 6b39b39d35
commit 68ba4cd39b
No known key found for this signature in database
GPG Key ID: DE108246970C7708
3 changed files with 39 additions and 30 deletions

View File

@ -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.query_dsl.Like;
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
import co.elastic.clients.elasticsearch.core.BulkRequest;
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.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.bulk.CreateOperation;
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.SourceConfig;
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.json.JsonData;
import co.elastic.clients.json.JsonpDeserializer;
@ -1134,10 +1126,13 @@ class RequestConverter {
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
query.getRuntimeFields().forEach(runtimeField -> {
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();
if (script != null)
if (script != null) {
builder = builder.script(s -> s.inline(is -> is.source(script)));
}
return builder;
});
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,
SearchRequest.Builder builder, boolean forCount, boolean useScroll) {
@ -1330,9 +1324,9 @@ class RequestConverter {
private void addHighlight(Query query, MultisearchBody.Builder builder) {
Highlight highlight = query.getHighlightQuery()
.map(highlightQuery -> new HighlightQueryBuilder(elasticsearchConverter.getMappingContext())
.getHighlight(highlightQuery.getHighlight(), highlightQuery.getType()))
.orElse(null);
.map(highlightQuery -> new HighlightQueryBuilder(elasticsearchConverter.getMappingContext())
.getHighlight(highlightQuery.getHighlight(), highlightQuery.getType()))
.orElse(null);
builder.highlight(highlight);
}
@ -1428,9 +1422,9 @@ class RequestConverter {
});
builder //
.suggest(query.getSuggester()) //
.collapse(query.getFieldCollapse()) //
.sort(query.getSortOptions());
.suggest(query.getSuggester()) //
.collapse(query.getFieldCollapse()) //
.sort(query.getSortOptions());
if (!isEmpty(query.getAggregations())) {
builder.aggregations(query.getAggregations());

View File

@ -32,10 +32,9 @@ public class RuntimeField {
private final String name;
private final String type;
@Nullable
private final String script;
@Nullable private final String script;
public RuntimeField(String name, String type){
public RuntimeField(String name, String type) {
this(name, type, null);
}
@ -59,6 +58,7 @@ public class RuntimeField {
public Map<String, Object> getMapping() {
Map<String, Object> map = new HashMap<>();
map.put("type", type);
if (script != null) {
map.put("script", script);
}

View File

@ -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;
import static org.assertj.core.api.Assertions.*;
import java.util.Map;
@ -10,25 +24,26 @@ import org.junit.jupiter.api.Test;
/**
* @author cdalxndr
* @since 5.0
*/
class RuntimeFieldTest {
@Test //#2267
@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");
assertThat(mapping).containsEntry("type", "double").containsEntry("script", "myscript");
}
@Test //#2267
@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");
assertThat(mapping).containsEntry("type", "double").doesNotContainKey("script");
}
}
}