Create The Photo Entity
In our restaurant review platform, we need a way to store and manage photos that users can attach to their restaurant listings and reviews.
Creating the Photo Entity
Let's build our Photo entity which will store information about uploaded images in our application.
This entity will be designed to be nested within both Restaurant and Review entities, making it flexible for various use cases.
Here's how we'll implement the Photo entity:
package com.devtiro.restaurant.domain.entities;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Photo {
@Field(type = FieldType.Keyword)
private String url;
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDateTime uploadDate;
}Field Configurations
The Photo entity contains two main fields that serve distinct purposes.
The url field uses FieldType.Keyword since we want exact matches when searching for specific photo URLs in our system.
For the uploadDate field, we use FieldType.Date with a specific format of DateFormat.date_hour_minute_second to ensure proper storage and retrieval of the upload timestamp.
Date Format Specification
When working with LocalDateTime in Elasticsearch, specifying the date format is particularly important.
The DateFormat.date_hour_minute_second format allows us to store and query both the date and time components with precision up to the second.
This level of detail is useful for tracking when photos were uploaded and ordering them chronologically.
Summary
- Created the
Photoentity withurlanduploadDatefields - Used
FieldType.Keywordfor theurlfield for exact matching - Configured
LocalDateTimewith proper date format - Prepared entity for nesting in
RestaurantandReviewentities