|
31 | 31 | import org.springframework.data.elasticsearch.annotations.Field;
|
32 | 32 | import org.springframework.data.elasticsearch.annotations.FieldType;
|
33 | 33 | import org.springframework.data.elasticsearch.annotations.Mapping;
|
| 34 | +import org.springframework.data.elasticsearch.annotations.ScriptedField; |
34 | 35 | import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
35 | 36 | import org.springframework.data.elasticsearch.core.query.Criteria;
|
36 | 37 | import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
@@ -95,6 +96,88 @@ void shouldUseRuntimeFieldWithoutScript() {
|
95 | 96 | assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("1");
|
96 | 97 | }
|
97 | 98 |
|
| 99 | + @Test // #2727 |
| 100 | + @DisplayName("should return runtime fields values") |
| 101 | + void shouldReturnRuntimeFieldsValues() { |
| 102 | + |
| 103 | + var entity = new SAREntity(); |
| 104 | + entity.setId("42"); |
| 105 | + entity.setValue(3); |
| 106 | + |
| 107 | + operations.save(entity); |
| 108 | + |
| 109 | + var runtimeField1 = getRuntimeField("scriptedValue1", 2); |
| 110 | + var runtimeField2 = getRuntimeField("scriptedValue2", 3); |
| 111 | + |
| 112 | + var query = CriteriaQuery.builder(Criteria.where("value").is(3)).build(); |
| 113 | + query.addRuntimeField(runtimeField1); |
| 114 | + query.addRuntimeField(runtimeField2); |
| 115 | + query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("*").build()); |
| 116 | + query.addFields("scriptedValue1", "scriptedValue2"); |
| 117 | + var searchHits = operations.search(query, SAREntity.class); |
| 118 | + |
| 119 | + assertThat(searchHits.getTotalHits()).isEqualTo(1); |
| 120 | + var foundEntity = searchHits.getSearchHit(0).getContent(); |
| 121 | + assertThat(foundEntity.value).isEqualTo(3); |
| 122 | + assertThat(foundEntity.getScriptedValue1()).isEqualTo(6); |
| 123 | + assertThat(foundEntity.getScriptedValue2()).isEqualTo(9); |
| 124 | + } |
| 125 | + |
| 126 | + @Document(indexName = "#{@indexNameProvider.indexName()}-sar") |
| 127 | + public static class SAREntity { |
| 128 | + @Nullable private String id; |
| 129 | + @Field(type = FieldType.Integer) |
| 130 | + @Nullable Integer value; |
| 131 | + @ScriptedField |
| 132 | + @Nullable Integer scriptedValue1; |
| 133 | + @ScriptedField |
| 134 | + @Nullable Integer scriptedValue2; |
| 135 | + // getter and setter omitted |
| 136 | + |
| 137 | + @Nullable |
| 138 | + public String getId() { |
| 139 | + return id; |
| 140 | + } |
| 141 | + |
| 142 | + public void setId(@Nullable String id) { |
| 143 | + this.id = id; |
| 144 | + } |
| 145 | + |
| 146 | + @Nullable |
| 147 | + public Integer getValue() { |
| 148 | + return value; |
| 149 | + } |
| 150 | + |
| 151 | + public void setValue(@Nullable Integer value) { |
| 152 | + this.value = value; |
| 153 | + } |
| 154 | + |
| 155 | + @Nullable |
| 156 | + public Integer getScriptedValue1() { |
| 157 | + return scriptedValue1; |
| 158 | + } |
| 159 | + |
| 160 | + public void setScriptedValue1(@Nullable Integer scriptedValue1) { |
| 161 | + this.scriptedValue1 = scriptedValue1; |
| 162 | + } |
| 163 | + |
| 164 | + @Nullable |
| 165 | + public Integer getScriptedValue2() { |
| 166 | + return scriptedValue2; |
| 167 | + } |
| 168 | + |
| 169 | + public void setScriptedValue2(@Nullable Integer scriptedValue2) { |
| 170 | + this.scriptedValue2 = scriptedValue2; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static RuntimeField getRuntimeField(String fieldName, int factor) { |
| 175 | + return new RuntimeField( |
| 176 | + fieldName, |
| 177 | + "long", |
| 178 | + String.format("emit(doc['value'].size() > 0 ? doc['value'].value * %d : 0)", factor)); |
| 179 | + } |
| 180 | + |
98 | 181 | @Test // #2431
|
99 | 182 | @DisplayName("should return value from runtime field defined in mapping")
|
100 | 183 | void shouldReturnValueFromRuntimeFieldDefinedInMapping() {
|
|
0 commit comments