{"id":18480674,"url":"https://github.com/jloisel/elastic-crud","last_synced_at":"2025-04-08T16:31:27.787Z","repository":{"id":57725590,"uuid":"49126308","full_name":"jloisel/elastic-crud","owner":"jloisel","description":"Simple yet elegant ElasticSearch Crud Repository.","archived":false,"fork":false,"pushed_at":"2017-10-31T15:06:16.000Z","size":144,"stargazers_count":47,"open_issues_count":0,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-23T16:11:40.773Z","etag":null,"topics":["crud","elastcsearch","elasticsearch-client","elasticsearch-queries","elasticsearch6","jackson","java","json-serialization","spring"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jloisel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-06T09:34:56.000Z","updated_at":"2024-05-30T15:13:08.000Z","dependencies_parsed_at":"2022-09-26T21:51:14.638Z","dependency_job_id":null,"html_url":"https://github.com/jloisel/elastic-crud","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jloisel%2Felastic-crud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jloisel%2Felastic-crud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jloisel%2Felastic-crud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jloisel%2Felastic-crud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jloisel","download_url":"https://codeload.github.com/jloisel/elastic-crud/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247882014,"owners_count":21011975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crud","elastcsearch","elasticsearch-client","elasticsearch-queries","elasticsearch6","jackson","java","json-serialization","spring"],"created_at":"2024-11-06T12:19:44.136Z","updated_at":"2025-04-08T16:31:26.764Z","avatar_url":"https://github.com/jloisel.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/jloisel/elastic-crud.svg)](https://travis-ci.org/jloisel/elastic-crud)\n[![Dependency Status](https://www.versioneye.com/user/projects/568d2e269c1b98002b000030/badge.svg?style=flat)](https://www.versioneye.com/user/projects/568d2e269c1b98002b000030)\n[![Coverage Status](https://coveralls.io/repos/jloisel/elastic-crud/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/jloisel/elastic-crud?branch=master)\n[![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)\n[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jcabi/jcabi-email/badge.svg)](http://www.javadoc.io/doc/com.jeromeloisel/elastic-crud)\n\n## Elasticsearch Simple CRUD Repository\n\nEasily 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.\n\nThis project powers our [JMeter Load Testing platform](https://octoperf.com).\n\n### Versions\n\nThe following table shows the correspondance between our versions and Elasticsearch versions:\n\n| Version       | ElasticSearch Version |\n| ------------- |:---------------------:|\n| 1.1.x      | 2.1.x |\n| 2.2.x      | 2.2.x |\n| 2.3.x      | 2.3.x |\n| 5.1.x      | 5.1.x |\n| 5.6.x      | 5.6.x |\n\nAs of 2.2.x, the project is going to strictly follow the same versioning as [elasticsearch](https://github.com/elastic/elasticsearch).\n\n### Spring\n\nAdd the following Maven dependency to get started quickly with Spring:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.jeromeloisel\u003c/groupId\u003e\n    \u003cartifactId\u003edb-spring-elasticsearch-starter\u003c/artifactId\u003e\n    \u003cversion\u003e5.6.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n### Vanilla Java\n\nTo get started with Vanilla Java application, you need to add two dependencies:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.jeromeloisel\u003c/groupId\u003e\n    \u003cartifactId\u003edb-conversion-jackson\u003c/artifactId\u003e\n    \u003cversion\u003e5.6.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\nThis dependency provides the Jackson Json serialization mechanism.\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.jeromeloisel\u003c/groupId\u003e\n    \u003cartifactId\u003edb-repository-elasticsearch\u003c/artifactId\u003e\n    \u003cversion\u003e5.6.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nThis dependency provides the **ElasticSearchRepositoryFactory** to create **ElasticRepository**.\n\n### Java Example\n\nSuppose we would like to persist the following Bean in Elasticsearch:\n\n```java\n@Value\n@Builder\n@Document(indexName=\"datas\", type=\"person\")\npublic class Person implements Entity {\n  @Wither\n  String id;\n  String firstname;\n  String lastname;\n  \n  @JsonCreator\n  Person(\n      @JsonProperty(\"id\") final String id, \n      @JsonProperty(\"firstname\") final String firstname, \n      @JsonProperty(\"lastname\") final String lastname) {\n    super();\n    this.id = id;\n    this.firstname = checkNotNull(firstname);\n    this.lastname = checkNotNull(lastname);\n  }\n} \n```\n\nThe following code shows how to use the CRUD repository:\n\n```java\n@Autowired\nprivate ElasticSearchRepositoryFactory factory;\n\npublic void method() {\n  final ElasticRepository\u003cPerson\u003e repository = factory.create(Person.class);\n  \n  final Person person = Person.builder().id(\"\").firstname(\"John\").lastname(\"Smith\").build();\n  final Person withId = repository.save(person);\n  \n  // Find by id\n  final Optional\u003cPerson\u003e byId = repository.findOne(withId.getId());\n  assertTrue(repository.exists(byId));\n  \n  // Search by firstname (with \"not_analyzed\" string mapping)\n  final TermQueryBuilder term = new TermQueryBuilder(\"firstname\", PERSON.getFirstname());\n  final List\u003cPerson\u003e found = repository.search(term);\n  assertTrue(found.contains(byId));\n  \n  // Delete from Elasticsearch definitively\n  repository.delete(withId.getId());\n  assertFalse(repository.exists(byId));\n}\n```\n\nAlso, scrolling through massive amount of results is made dead easy with the scrolling API:\n```java\n@Autowired\nprivate DatabaseScrollingFactory factory;\n\npublic void example() {\n  // Incorporated bulk delete\n  factory\n    .newScroll(\"myIndex\")\n    .withQuery(new MatchAllQueryBuilder())\n    .scroll(factory.bulkDelete());\n  \n}\n```\nYou simply have to implement the `DatabaseScroll` interface:\n\n```java\n@FunctionalInterface\npublic interface DatabaseScroll {\n\n  default void onStartBatch() throws IOException {\n\n  }\n\n  void accept(SearchHit hit) throws IOException;\n\n  default void onEndBatch() throws IOException {\n\n  }\n}\n\n```\n\n### Type mapping\n\nBeans 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:\n\n```json\n{\n  \"template\": \"datas\",\n  \"settings\": {\n    \"number_of_shards\": 5,\n    \"number_of_replicas\": 1,\n    \"index.refresh_interval\": -1,\n  },\n  \"mappings\": {\n    \"_default_\": {\n      \"_all\": {\n          \"enabled\": false\n       },\n       \"_source\": {\n          \"enabled\": true\n       }\n    }\n  }\n}\n```\n\n### Index refresh\n\nEvery mutating query (insert, delete) performed on the index automatically refreshes it. I would recommend to disable index refresh as shows in the Json above.\n\n### Json Serialization\n\nThe Json serialization is configured to use [Jackson](https://github.com/FasterXML/jackson) by default. To use Jackson Json serialization, simply add Jackson as dependency:\n\n```xml\n\u003cdependency\u003e\n\t\u003cgroupId\u003ecom.fasterxml.jackson.core\u003c/groupId\u003e\n\t\u003cartifactId\u003ejackson-databind\u003c/artifactId\u003e\n\t\u003cversion\u003e${jackson.version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nReplace **${jackson.version}** with the version you are using.\n\nIf you intend to use your own Json serialization mechanism (like Gson), please provide an implementation for the **JsonSerializationFactory** interface.\n\n### Elasticsearch Client\n\nAn instance of the [Elasticsearch Client](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html) must be provided.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjloisel%2Felastic-crud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjloisel%2Felastic-crud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjloisel%2Felastic-crud/lists"}