반응형
dependenceis
implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch")
application.properties
# 쿼리 로그 확인을 위한 설정
logging.level.tracer=trace
ElasticsearchConfig
@Configuration
public class ElasticsearchConfig extends ElasticsearchConfiguration {
@Override
public ClientConfiguration clientConfiguration() {
return ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
}
}
User
@Data
@Document(
indexName = "user",
writeTypeHint = WriteTypeHint.FALSE // _class 필드 생성 방지
)
public class User {
@Id
private String id;
private String name;
private int age;
private List<String> nicknames;
private GeoPoint location;
@Field(type = FieldType.Date)
private ZonedDateTime createdAt;
}
UserRepository
@Repository
public interface UserRepository extends ElasticsearchRepository<User, String> {
List<User> findAllByCreatedAtAfter(ZonedDateTime createdAt);
}
UserRepositoryTest
@SpringBootTest
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void saveAndGet() {
User user = new User();
user.setId(UUID.randomUUID().toString());
user.setName("tyler");
user.setAge(34);
user.setNicknames(List.of("tyler-a", "tyler-b"));
user.setLocation(new GeoPoint(37.38523859949659, 127.12297619723063));
user.setCreatedAt(LocalDateTime.now().atZone(ZoneId.of("Asia/Seoul")));
userRepository.save(user);
User dbUser = userRepository.findById(user.getId()).get();
List<User> users = userRepository.findAllByCreatedAtAfter(LocalDateTime.now().minusMinutes(1).atZone(ZoneId.of("Asia/Seoul")));
assertEquals("tyler", dbUser.getName());
assertEquals("tyler", users.get(0).getName());
assertEquals(1, users.size());
}
}
반응형
'Development > Spring' 카테고리의 다른 글
[Spring] Exposed (0) | 2024.04.10 |
---|---|
[Spring] HTTP Interface(Deprecated) (0) | 2024.03.19 |
[Spring] 헥사고날 아키텍처(Hexagonal Architecture) (2) | 2023.11.23 |
[Spring] WireMock (0) | 2023.08.18 |
[Spring] SpEL (0) | 2023.08.09 |