|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.DisplayName; |
| 22 | +import org.junit.jupiter.api.Order; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.data.annotation.Id; |
| 26 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 27 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 28 | +import org.springframework.data.elasticsearch.annotations.FieldType; |
| 29 | +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
| 30 | +import org.springframework.data.elasticsearch.core.query.Criteria; |
| 31 | +import org.springframework.data.elasticsearch.core.query.CriteriaQuery; |
| 32 | +import org.springframework.data.elasticsearch.core.query.Query; |
| 33 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 34 | +import org.springframework.data.elasticsearch.utils.IndexNameProvider; |
| 35 | +import org.springframework.lang.Nullable; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Peter-Josef Meisch |
| 39 | + */ |
| 40 | +@SpringIntegrationTest |
| 41 | +public abstract class RuntimeFieldsIntegrationTests { |
| 42 | + |
| 43 | + @Autowired private ElasticsearchOperations operations; |
| 44 | + @Autowired protected IndexNameProvider indexNameProvider; |
| 45 | + private IndexOperations indexOperations; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + |
| 50 | + indexNameProvider.increment(); |
| 51 | + indexOperations = operations.indexOps(SomethingToBuy.class); |
| 52 | + indexOperations.createWithMapping(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @Order(java.lang.Integer.MAX_VALUE) |
| 57 | + void cleanup() { |
| 58 | + operations.indexOps(IndexCoordinates.of("*")).delete(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test // #1971 |
| 62 | + @DisplayName("should use runtime-field from query in search") |
| 63 | + void shouldUseRuntimeFieldFromQueryInSearch() { |
| 64 | + |
| 65 | + insert("1", "item 1", 13.5); |
| 66 | + insert("2", "item 2", 15); |
| 67 | + Query query = new CriteriaQuery(new Criteria("priceWithTax").greaterThanEqual(16.5)); |
| 68 | + RuntimeField runtimeField = new RuntimeField("priceWithTax", "double", "emit(doc['price'].value * 1.19)"); |
| 69 | + query.addRuntimeField(runtimeField); |
| 70 | + |
| 71 | + SearchHits<SomethingToBuy> searchHits = operations.search(query, SomethingToBuy.class); |
| 72 | + |
| 73 | + assertThat(searchHits.getTotalHits()).isEqualTo(1); |
| 74 | + assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("2"); |
| 75 | + } |
| 76 | + |
| 77 | + private void insert(String id, String description, double price) { |
| 78 | + SomethingToBuy entity = new SomethingToBuy(); |
| 79 | + entity.setId(id); |
| 80 | + entity.setDescription(description); |
| 81 | + entity.setPrice(price); |
| 82 | + operations.save(entity); |
| 83 | + } |
| 84 | + |
| 85 | + @Document(indexName = "#{@indexNameProvider.indexName()}") |
| 86 | + private static class SomethingToBuy { |
| 87 | + private @Id @Nullable String id; |
| 88 | + |
| 89 | + @Nullable @Field(type = FieldType.Text) private String description; |
| 90 | + |
| 91 | + @Nullable @Field(type = FieldType.Double) private Double price; |
| 92 | + |
| 93 | + @Nullable |
| 94 | + public String getId() { |
| 95 | + return id; |
| 96 | + } |
| 97 | + |
| 98 | + public void setId(@Nullable String id) { |
| 99 | + this.id = id; |
| 100 | + } |
| 101 | + |
| 102 | + @Nullable |
| 103 | + public String getDescription() { |
| 104 | + return description; |
| 105 | + } |
| 106 | + |
| 107 | + public void setDescription(@Nullable String description) { |
| 108 | + this.description = description; |
| 109 | + } |
| 110 | + |
| 111 | + @Nullable |
| 112 | + public Double getPrice() { |
| 113 | + return price; |
| 114 | + } |
| 115 | + |
| 116 | + public void setPrice(@Nullable Double price) { |
| 117 | + this.price = price; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments