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.
- Host: GitHub
- URL: https://github.com/jloisel/elastic-crud
- Owner: jloisel
- License: apache-2.0
- Created: 2016-01-06T09:34:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-31T15:06:16.000Z (over 7 years ago)
- Last Synced: 2025-03-23T16:11:40.773Z (3 months ago)
- Topics: crud, elastcsearch, elasticsearch-client, elasticsearch-queries, elasticsearch6, jackson, java, json-serialization, spring
- Language: Java
- Homepage:
- Size: 141 KB
- Stars: 47
- Watchers: 7
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/jloisel/elastic-crud)
[](https://www.versioneye.com/user/projects/568d2e269c1b98002b000030)
[](https://coveralls.io/github/jloisel/elastic-crud?branch=master)
[](https://maven-badges.herokuapp.com/maven-central/com.jeromeloisel/db-spring-elasticsearch-starter)
[](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 JavaTo 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.