Create The Operatinghours Timerange Entity

In this lesson, we'll build two interconnected entities that will help us manage restaurant opening hours.

Understanding the Entity Structure

Restaurant operating hours can be complex, requiring a structured approach to store opening and closing times for each day of the week.

The TimeRange entity will represent a single opening period with a start and end time, while OperatingHours will contain a TimeRange for each day of the week.

Creating the TimeRange Entity

Let's start by creating the simpler of the two entities - TimeRange:

package com.devtiro.restaurant.domain.entities; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class TimeRange { @Field(type = FieldType.Keyword) private String openTime; // Stores opening time (e.g., "09:00") @Field(type = FieldType.Keyword) private String closeTime; // Stores closing time (e.g., "17:00") }

We use FieldType.Keyword here because times should be matched exactly, not analyzed for text search.

Creating the OperatingHours Entity

Now we'll create the OperatingHours entity which will contain a TimeRange for each day:

package com.devtiro.restaurant.domain.entities; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class OperatingHours { @Field(type = FieldType.Nested) private TimeRange monday; // TimeRange for Monday @Field(type = FieldType.Nested) private TimeRange tuesday; // TimeRange for Tuesday @Field(type = FieldType.Nested) private TimeRange wednesday; // TimeRange for Wednesday @Field(type = FieldType.Nested) private TimeRange thursday; // TimeRange for Thursday @Field(type = FieldType.Nested) private TimeRange friday; // TimeRange for Friday @Field(type = FieldType.Nested) private TimeRange saturday; // TimeRange for Saturday @Field(type = FieldType.Nested) private TimeRange sunday; // TimeRange for Sunday }

The FieldType.Nested annotation is particularly important here.

It tells Elasticsearch to treat each TimeRange as a nested object, maintaining the relationship between opening and closing times.

This allows us to query operating hours as complete units rather than independent fields.

Summary

  • Created the TimeRange entity to store opening and closing times
  • Built the OperatingHours entity containing nested TimeRange objects
  • Used FieldType.Nested to maintain relationships between fields
  • Prepared both entities for integration with the Restaurant entity
© 2026 Devtiro Ltd. All rights reserved