An open API service indexing awesome lists of open source software.

https://github.com/jloisel/elastic-crud

Simple yet elegant ElasticSearch Crud Repository.
https://github.com/jloisel/elastic-crud

crud elastcsearch elasticsearch-client elasticsearch-queries elasticsearch6 jackson java json-serialization spring

Last synced: 2 months ago
JSON representation

Simple yet elegant ElasticSearch Crud Repository.

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/jloisel/elastic-crud.svg)](https://travis-ci.org/jloisel/elastic-crud)
[![Dependency Status](https://www.versioneye.com/user/projects/568d2e269c1b98002b000030/badge.svg?style=flat)](https://www.versioneye.com/user/projects/568d2e269c1b98002b000030)
[![Coverage Status](https://coveralls.io/repos/jloisel/elastic-crud/badge.svg?branch=master&service=github)](https://coveralls.io/github/jloisel/elastic-crud?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.jeromeloisel/db-spring-elasticsearch-starter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.jeromeloisel/db-spring-elasticsearch-starter)
[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jcabi/jcabi-email/badge.svg)](http://www.javadoc.io/doc/com.jeromeloisel/elastic-crud)

## Elasticsearch Simple CRUD Repository

Easily perform Create / Read / Update / Delete operations on beans stored in Elasticsearch. [Spring Data Elasticsearch](https://github.com/spring-projects/spring-data-elasticsearch) lacks maintenance and is already a few Elasticsearch versions behind the latest version.

This project powers our [JMeter Load Testing platform](https://octoperf.com).

### Versions

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](https://github.com/elastic/elasticsearch).

### Spring

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

```xml

com.jeromeloisel
db-spring-elasticsearch-starter
5.6.3

```
### Vanilla Java

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

```xml

com.jeromeloisel
db-conversion-jackson
5.6.3

```
This dependency provides the Jackson Json serialization mechanism.

```xml

com.jeromeloisel
db-repository-elasticsearch
5.6.3

```

This dependency provides the **ElasticSearchRepositoryFactory** to create **ElasticRepository**.

### Java Example

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

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

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

The following code shows how to use the CRUD repository:

```java
@Autowired
private ElasticSearchRepositoryFactory factory;

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

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

// Find by id
final Optional byId = repository.findOne(withId.getId());
assertTrue(repository.exists(byId));

// Search by firstname (with "not_analyzed" string mapping)
final TermQueryBuilder term = new TermQueryBuilder("firstname", PERSON.getFirstname());
final List found = repository.search(term);
assertTrue(found.contains(byId));

// Delete from Elasticsearch definitively
repository.delete(withId.getId());
assertFalse(repository.exists(byId));
}
```

Also, scrolling through massive amount of results is made dead easy with the scrolling API:
```java
@Autowired
private DatabaseScrollingFactory factory;

public void example() {
// Incorporated bulk delete
factory
.newScroll("myIndex")
.withQuery(new MatchAllQueryBuilder())
.scroll(factory.bulkDelete());

}
```
You simply have to implement the `DatabaseScroll` interface:

```java
@FunctionalInterface
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 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html. The following example Json shows how to enable _source field:

```json
{
"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](https://github.com/FasterXML/jackson) by default. To use Jackson Json serialization, simply add Jackson as dependency:

```xml

com.fasterxml.jackson.core
jackson-databind
${jackson.version}

```

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](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html) must be provided.