一、定义ES索引
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
@Document(indexName = "charge_pile_log" + Constants.ES_TIME_DYNAMIC_INDEX)
public class ChargePileLogESDomain {@Idprivate String id;@Field(type = FieldType.Integer)private Integer businessType;
}public static final String ES_TIME_DYNAMIC_INDEX = "-#{ T(com.energy.cloud.log.service.util.DateUtil).getUTCMonth() }";
二、定义Repository
import com.energy.cloud.log.service.domain.ChargePileLogESDomain;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;public interface ChargePileLogRepository extends ElasticsearchRepository<ChargePileLogESDomain, String> {
}
三、存储数据
@Resourceprivate ChargePileLogRepository chargePileLogRepository;List<ChargePileLogESDomain> saveList = new ArrayList<>();
chargePileLogRepository.saveAll(saveList);
四、查询数据(索引名称带通配符)
SearchRequest request = new SearchRequest(charge_pile_log*);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
request.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
五、常量类和工具类
public static final String ES_TIME_DYNAMIC_INDEX = "-#{ T(com.energy.cloud.log.service.util.DateUtil).getUTCMonth() }";public class DateUtil {public static String getUTCMonth(){DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM");DateTime now = DateTime.now(DateTimeZone.UTC);return fmt.print(now);}
}