mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 20:12:11 +00:00
Fix field parameters in search request creation.
Original Pull Request #2739 Closes #2727 (cherry picked from commit f087d5aac392c1be09ea2cdd0245168a34d74154)
This commit is contained in:
parent
aaaa18ec4c
commit
69329bc62d
@ -30,6 +30,7 @@ import co.elastic.clients.elasticsearch._types.mapping.Property;
|
||||
import co.elastic.clients.elasticsearch._types.mapping.RuntimeField;
|
||||
import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
|
||||
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.FieldAndFormat;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Like;
|
||||
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
|
||||
import co.elastic.clients.elasticsearch.core.*;
|
||||
@ -1050,10 +1051,9 @@ class RequestConverter {
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getFields())) {
|
||||
bb.fields(fb -> {
|
||||
query.getFields().forEach(fb::field);
|
||||
return fb;
|
||||
});
|
||||
var fieldAndFormats = query.getFields().stream()
|
||||
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
|
||||
bb.fields(fieldAndFormats);
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getStoredFields())) {
|
||||
@ -1169,10 +1169,9 @@ class RequestConverter {
|
||||
builder.source(getSourceConfig(query));
|
||||
|
||||
if (!isEmpty(query.getFields())) {
|
||||
builder.fields(fb -> {
|
||||
query.getFields().forEach(fb::field);
|
||||
return fb;
|
||||
});
|
||||
var fieldAndFormats = query.getFields().stream()
|
||||
.map(field -> FieldAndFormat.of(b -> b.field(field))).toList();
|
||||
builder.fields(fieldAndFormats);
|
||||
}
|
||||
|
||||
if (!isEmpty(query.getStoredFields())) {
|
||||
|
@ -31,6 +31,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
import org.springframework.data.elasticsearch.annotations.Mapping;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||
@ -95,6 +96,88 @@ public abstract class RuntimeFieldsIntegrationTests {
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test // #2727
|
||||
@DisplayName("should return runtime fields values")
|
||||
void shouldReturnRuntimeFieldsValues() {
|
||||
|
||||
var entity = new SAREntity();
|
||||
entity.setId("42");
|
||||
entity.setValue(3);
|
||||
|
||||
operations.save(entity);
|
||||
|
||||
var runtimeField1 = getRuntimeField("scriptedValue1", 2);
|
||||
var runtimeField2 = getRuntimeField("scriptedValue2", 3);
|
||||
|
||||
var query = CriteriaQuery.builder(Criteria.where("value").is(3)).build();
|
||||
query.addRuntimeField(runtimeField1);
|
||||
query.addRuntimeField(runtimeField2);
|
||||
query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("*").build());
|
||||
query.addFields("scriptedValue1", "scriptedValue2");
|
||||
var searchHits = operations.search(query, SAREntity.class);
|
||||
|
||||
assertThat(searchHits.getTotalHits()).isEqualTo(1);
|
||||
var foundEntity = searchHits.getSearchHit(0).getContent();
|
||||
assertThat(foundEntity.value).isEqualTo(3);
|
||||
assertThat(foundEntity.getScriptedValue1()).isEqualTo(6);
|
||||
assertThat(foundEntity.getScriptedValue2()).isEqualTo(9);
|
||||
}
|
||||
|
||||
@Document(indexName = "#{@indexNameProvider.indexName()}-sar")
|
||||
public static class SAREntity {
|
||||
@Nullable private String id;
|
||||
@Field(type = FieldType.Integer)
|
||||
@Nullable Integer value;
|
||||
@ScriptedField
|
||||
@Nullable Integer scriptedValue1;
|
||||
@ScriptedField
|
||||
@Nullable Integer scriptedValue2;
|
||||
// getter and setter omitted
|
||||
|
||||
@Nullable
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(@Nullable String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(@Nullable Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getScriptedValue1() {
|
||||
return scriptedValue1;
|
||||
}
|
||||
|
||||
public void setScriptedValue1(@Nullable Integer scriptedValue1) {
|
||||
this.scriptedValue1 = scriptedValue1;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getScriptedValue2() {
|
||||
return scriptedValue2;
|
||||
}
|
||||
|
||||
public void setScriptedValue2(@Nullable Integer scriptedValue2) {
|
||||
this.scriptedValue2 = scriptedValue2;
|
||||
}
|
||||
}
|
||||
|
||||
private static RuntimeField getRuntimeField(String fieldName, int factor) {
|
||||
return new RuntimeField(
|
||||
fieldName,
|
||||
"long",
|
||||
String.format("emit(doc['value'].size() > 0 ? doc['value'].value * %d : 0)", factor));
|
||||
}
|
||||
|
||||
@Test // #2431
|
||||
@DisplayName("should return value from runtime field defined in mapping")
|
||||
void shouldReturnValueFromRuntimeFieldDefinedInMapping() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user