Core utilities and common classes for Elasticsearch, providing fundamental building blocks like resource management, IO utilities, time handling, and reference counting.
npx @tessl/cli install tessl/maven-org-elasticsearch--elasticsearch-core@9.0.0Elasticsearch Core is a foundational Java library providing essential utilities and infrastructure components for the Elasticsearch ecosystem. It offers core building blocks for resource management, I/O operations, time handling, functional programming with checked exceptions, and common data structures. The library is designed to be lightweight and dependency-free, serving as the foundation that other Elasticsearch modules build upon.
implementation 'org.elasticsearch:elasticsearch-core:9.0.3'import org.elasticsearch.core.*;Individual imports:
import org.elasticsearch.core.RefCounted;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.core.Tuple;import org.elasticsearch.core.*;
import java.util.concurrent.TimeUnit;
public class Example {
public void resourceManagement() {
// Resource management with RefCounted
RefCounted resource = AbstractRefCounted.of(() -> {
System.out.println("Resource closed");
});
resource.incRef(); // Increment reference count
resource.decRef(); // Decrement reference count (closes when reaches 0)
}
public void timeOperations() {
// Time value handling
TimeValue duration = TimeValue.timeValueSeconds(30);
TimeValue timeout = new TimeValue(5, TimeUnit.MINUTES);
System.out.println("Duration: " + duration.toString());
System.out.println("Timeout: " + timeout.toHumanReadableString(2));
}
public void functionalOperations() throws Exception {
// Checked functional interfaces
CheckedFunction<String, Integer, NumberFormatException> parser =
Integer::parseInt;
Integer result = parser.apply("42");
CheckedConsumer<String, Exception> printer = System.out::println;
printer.accept("Hello World");
}
}Elasticsearch Core is organized around several key design patterns:
Provides lifecycle management patterns for resources that need explicit cleanup, including reference counting and releasable interfaces.
public interface RefCounted {
void incRef();
boolean tryIncRef();
boolean decRef();
boolean hasReferences();
default void mustIncRef();
}
public interface Releasable extends Closeable {
void close();
}
public abstract class AbstractRefCounted implements RefCounted {
protected AbstractRefCounted();
protected abstract void closeInternal();
public static AbstractRefCounted of(Runnable onClose);
}Checked exception variants of standard Java functional interfaces, enabling functional programming patterns with proper exception handling.
@FunctionalInterface
public interface CheckedFunction<T, R, E extends Exception> {
R apply(T t) throws E;
}
@FunctionalInterface
public interface CheckedConsumer<T, E extends Exception> {
void accept(T t) throws E;
default CheckedConsumer<T, E> andThen(CheckedConsumer<? super T, E> after);
}
@FunctionalInterface
public interface CheckedSupplier<T, E extends Exception> {
T get() throws E;
}
@FunctionalInterface
public interface CheckedRunnable<E extends Exception> {
void run() throws E;
}Core utility classes for common operations including I/O, time handling, string processing, and data manipulation.
public final class IOUtils {
public static void close(Closeable... objects);
public static void close(@Nullable Closeable closeable);
public static void closeWhileHandlingException(Closeable... objects);
public static void fsync(Path fileToSync, boolean isDir);
}
public class TimeValue implements Comparable<TimeValue> {
public TimeValue(long millis);
public TimeValue(long duration, TimeUnit timeUnit);
public static TimeValue timeValueSeconds(long seconds);
public static TimeValue parseTimeValue(String sValue, String settingName);
}
public record Tuple<V1, V2>(V1 v1, V2 v2) {
public static <V1, V2> Tuple<V1, V2> tuple(V1 v1, V2 v2);
}Annotations, enums, and type utilities that provide metadata and type manipulation capabilities for the Elasticsearch ecosystem.
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
public @interface Nullable {
}
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
public @interface SuppressForbidden {
String reason();
}
public enum RestApiVersion {
V_9(9), V_8(8);
public static RestApiVersion current();
public static RestApiVersion forMajor(int major);
public boolean matches(Predicate<RestApiVersion> predicate);
}