Build Status Dependency Status Coverage Status Maven Central Javadoc

Elasticsearch Simple CRUD Repository

Easily perform Create / Read / Update / Delete operations on beans stored in Elasticsearch. Spring Data Elasticsearch lacks maintenance and is already a few Elasticsearch versions behind the latest version.

This project powers our JMeter Load Testing platform.


The following table shows the correspondance between our versions and Elasticsearch versions:

Version ElasticSearch Version
1.1.x 2.1.x
2.2.x 2.2.x
2.3.x 2.3.x
5.1.x 5.1.x
5.6.x 5.6.x

As of 2.2.x, the project is going to strictly follow the same versioning as elasticsearch.


Add the following Maven dependency to get started quickly with Spring:


Vanilla Java

To get started with Vanilla Java application, you need to add two dependencies:


This dependency provides the Jackson Json serialization mechanism.


This dependency provides the ElasticSearchRepositoryFactory to create ElasticRepository.

Java Example

Suppose we would like to persist the following Bean in Elasticsearch:

@Document(indexName="datas", type="person")
public class Person implements Entity {
  String id;
  String firstname;
  String lastname;

      @JsonProperty("id") final String id, 
      @JsonProperty("firstname") final String firstname, 
      @JsonProperty("lastname") final String lastname) {
    super(); = id;
    this.firstname = checkNotNull(firstname);
    this.lastname = checkNotNull(lastname);

The following code shows how to use the CRUD repository:

private ElasticSearchRepositoryFactory factory;

public void method() {
  final ElasticRepository<Person> repository = factory.create(Person.class);

  final Person person = Person.builder().id("").firstname("John").lastname("Smith").build();
  final Person withId =;

  // Find by id
  final Optional<Person> byId = repository.findOne(withId.getId());

  // Search by firstname (with "not_analyzed" string mapping)
  final TermQueryBuilder term = new TermQueryBuilder("firstname", PERSON.getFirstname());
  final List<Person> found =;

  // Delete from Elasticsearch definitively

Also, scrolling through massive amount of results is made dead easy with the scrolling API:

private DatabaseScrollingFactory factory;

public void example() {
  // Incorporated bulk delete
    .withQuery(new MatchAllQueryBuilder())


You simply have to implement the DatabaseScroll interface:

public interface DatabaseScroll {

  default void onStartBatch() throws IOException {


  void accept(SearchHit hit) throws IOException;

  default void onEndBatch() throws IOException {


Type mapping

Beans stored in Elasticsearch must have _source field enabled: see The following example Json shows how to enable _source field:

  "template": "datas",
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1,
    "index.refresh_interval": -1,
  "mappings": {
    "_default_": {
      "_all": {
          "enabled": false
       "_source": {
          "enabled": true

Index refresh

Every mutating query (insert, delete) performed on the index automatically refreshes it. I would recommend to disable index refresh as shows in the Json above.

Json Serialization

The Json serialization is configured to use Jackson by default. To use Jackson Json serialization, simply add Jackson as dependency:


Replace ${jackson.version} with the version you are using.

If you intend to use your own Json serialization mechanism (like Gson), please provide an implementation for the JsonSerializationFactory interface.

Elasticsearch Client

An instance of the Elasticsearch Client must be provided.