A lightweight Java client for interacting with Elasticsearch via REST, supporting document CRUD, bulk operations, advanced search queries, and scrollable search.
- Index, update, delete, and retrieve documents
- Bulk operations (index, update, delete)
- Flexible search with query string and match queries
- Scrollable search for large result sets
- Index management (delete, alias)
- Handles both POJOs and Jackson ObjectNode for document bodies
- Java 17 or higher
- Maven (for building)
- Elasticsearch instance (tested with 7.x)
Not released yet.
- Clone the repository:
git clone git@github.com:NiSchie/ElasticRestClient.git cd ElasticRestClient - Build the project:
mvn clean package
ElasticRestClient client = new ElasticRestClient(
"http://localhost:9200", // Elasticsearch host
"user", // Username
"password" // Password
);Index index = Index.of("my-index");
Id id = Id.of("doc-id");
Map<String, Object> doc = Map.of("field", "value");
client.document().index(index, id, doc); // POJO or Map, allows update if exists
//index only with auto-generated ID
client.document().index(index, doc); // No ID specified, Elasticsearch generates one
// Or using Jackson ObjectNode
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
com.fasterxml.jackson.databind.node.ObjectNode jsonDoc = mapper.valueToTree(doc);
client.document().index(index, id, jsonDoc);ElasticDocument doc = client.document().getDocument(index, id);
if (doc == null) {
// Not found
}After retrieving a document, you can access its source and metadata:
if (doc != null) {
// Get the source as a Map
Map<String, Object> source = doc.source();
// Get the source as a Jackson ObjectNode
com.fasterxml.jackson.databind.node.ObjectNode sourceJson = doc.sourceAsJSON();
// Get the entire document (including metadata) as a Jackson ObjectNode
com.fasterxml.jackson.databind.node.ObjectNode fullJson = doc.toJSON();
// Parse the source directly into a POJO
MyPojo pojo = doc.sourceAs(MyPojo.class);
}client.document().delete(index, id);StringSearchQuery query = StringSearchQuery.of("field:value AND other:foo");
List<ElasticDocument> results = client.document().searchDocuments(index, query);Long count = client.document().countByQuery(index, query);client.document().deleteByMatchQuery(index, query);BulkClient bulk = client.bulk();
bulk.addIndexRequest(index, id, doc); // POJO, Map, or Jackson ObjectNode
bulk.addUpdateRequest(index, id, Map.of("field", "newValue")); // Update specific fields
bulk.addDeleteRequest(index, id);
bulk.executeBulk(true); // Force executionScrollableSearch scroll = client.scrollSearch(index, query, 100);
List<ElasticDocument> hits;
while (scroll.scroll()) {
hits = scroll.getSearchHits();
// process hits
}- The
scroll()method performs the initial search on the first call and fetches the next page on subsequent calls using the scroll ID. - It returns the current page of hits as a
List<ElasticDocument>, or an empty list if there are no more results.
client.index().deleteIndex(index);
client.index().alias(index, "alias-name");- All document methods accept POJOs, Maps, or Jackson
ObjectNode/ArrayNode. - JSON serialization is handled automatically.
- Error handling:
getDocumentreturnsnullif not found; other methods throw on error.