|
| 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.mapping; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.elasticsearch.index.query.QueryBuilders.*; |
| 20 | + |
| 21 | +import lombok.Builder; |
| 22 | +import lombok.Data; |
| 23 | + |
| 24 | +import org.elasticsearch.client.RestHighLevelClient; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.data.annotation.Id; |
| 32 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 33 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 34 | +import org.springframework.data.elasticsearch.annotations.FieldType; |
| 35 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 36 | +import org.springframework.data.elasticsearch.core.IndexOperations; |
| 37 | +import org.springframework.data.elasticsearch.core.SearchHits; |
| 38 | +import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; |
| 39 | +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; |
| 40 | +import org.springframework.data.elasticsearch.core.query.Query; |
| 41 | +import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration; |
| 42 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 43 | +import org.springframework.data.mapping.model.FieldNamingStrategy; |
| 44 | +import org.springframework.data.mapping.model.SnakeCaseFieldNamingStrategy; |
| 45 | +import org.springframework.test.context.ContextConfiguration; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Peter-Josef Meisch |
| 49 | + */ |
| 50 | +@SpringIntegrationTest |
| 51 | +@ContextConfiguration(classes = { FieldNamingStrategyIntegrationTest.Config.class }) |
| 52 | +public class FieldNamingStrategyIntegrationTest { |
| 53 | + |
| 54 | + @Autowired private ElasticsearchOperations operations; |
| 55 | + |
| 56 | + @Configuration |
| 57 | + static class Config extends ElasticsearchRestTemplateConfiguration { |
| 58 | + |
| 59 | + @Override |
| 60 | + @Bean |
| 61 | + public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter, |
| 62 | + RestHighLevelClient elasticsearchClient) { |
| 63 | + return super.elasticsearchOperations(elasticsearchConverter, elasticsearchClient); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected FieldNamingStrategy fieldNamingStrategy() { |
| 68 | + return new SnakeCaseFieldNamingStrategy(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void setUp() { |
| 74 | + IndexOperations indexOps = this.operations.indexOps(Entity.class); |
| 75 | + indexOps.delete(); |
| 76 | + indexOps.create(); |
| 77 | + indexOps.putMapping(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test // #1565 |
| 81 | + @DisplayName("should use configured FieldNameStrategy") |
| 82 | + void shouldUseConfiguredFieldNameStrategy() { |
| 83 | + |
| 84 | + Entity entity = new Entity.EntityBuilder().id("42").someText("the text to be searched").build(); |
| 85 | + operations.save(entity); |
| 86 | + |
| 87 | + // use a native query here to prevent automatic property name matching |
| 88 | + Query query = new NativeSearchQueryBuilder().withQuery(matchQuery("some_text", "searched")).build(); |
| 89 | + SearchHits<Entity> searchHits = operations.search(query, Entity.class); |
| 90 | + |
| 91 | + assertThat(searchHits.getTotalHits()).isEqualTo(1); |
| 92 | + } |
| 93 | + |
| 94 | + @Data |
| 95 | + @Builder |
| 96 | + @Document(indexName = "field-naming-strategy-test") |
| 97 | + static class Entity { |
| 98 | + @Id private String id; |
| 99 | + @Field(type = FieldType.Text) private String someText; |
| 100 | + } |
| 101 | +} |
0 commit comments