Support of Elasticsearch for Spring Data
CAST supports Elasticsearch via
its com.castsoftware.nosqljava
extension. Details about how this support is provided for Java with Spring Data source code is discussed below.
Supported Client Libraries
Supported Operations
| Insert |
com.github.vanroy.springdata.jest.JestElasticsearchTemplate
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.prepareIndex
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.putMapping
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.createIndex
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.createIndexIfNotCreated
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.createIndexWithSettings
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.prepareIndex
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.putMapping
org.springframework.data.elasticsearch.core
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate.saveAll
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.saveAll
- org.springframework.data.elasticsearch.core.IndexOperations.create
- org.springframework.data.elasticsearch.core.IndexOperations.putMapping
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.save
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.save
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations.save
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.save
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.saveAll
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate.saveAll
- org.springframework.data.elasticsearch.core.ReactiveIndexOperations.create
- org.springframework.data.elasticsearch.core.ReactiveIndexOperations.putMapping
org.springframework.data.repository
- org.springframework.data.repository.CrudRepository.save
- org.springframework.data.repository.CrudRepository.saveAll
- org.springframework.data.repository.ElasticsearchRepository.save
- org.springframework.data.repository.ElasticsearchRepository.saveAll
org.springframework.data.elasticsearch.repository.support
- org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.save
- org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.saveAll
- org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository.save
- org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository.saveAll
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchIndicesClient.create
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.Indices.createIndex
|
| Select |
|
| Update |
com.github.vanroy.springdata.jest.JestElasticsearchTemplate
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.bulkUpdate
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.prepareUpdate
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.update
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.bulkIndex
org.springframework.data.elasticsearch.client
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient.bulk
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient.index
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient.update
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.bulk
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.index
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.update
org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.bulkUpdate
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.bulkIndex
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.index
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.update
org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.bulkUpdate
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.bulkIndex
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.index
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.update
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.update
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.updateByQuery
- org.springframework.data.elasticsearch.core.DocumentOperations.bulkIndex
- org.springframework.data.elasticsearch.core.DocumentOperations.bulkUpdate
|
| Delete |
org.springframework.data.repository.CrudRepository
- org.springframework.data.repository.CrudRepository.delete
- org.springframework.data.repository.CrudRepository.deleteById
- org.springframework.data.repository.CrudRepository.deleteAllById
- org.springframework.data.repository.CrudRepository.deleteAll
org.springframework.data.repository.ElasticsearchRepositoryy
- org.springframework.data.repository.ElasticsearchRepository.delete
- org.springframework.data.repository.ElasticsearchRepository.deleteById
- org.springframework.data.repository.ElasticsearchRepository.deleteAllById
- org.springframework.data.repository.ElasticsearchRepository.deleteAll
org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository
- org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.delete
- org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.deleteById
- org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.deleteAll
org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository
- org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository.delete
- org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository.deleteById
- org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository.deleteAll
org.springframework.data.repository.reactive.ReactiveCrudRepository
- org.springframework.data.repository.reactive.ReactiveCrudRepository.delete
- org.springframework.data.repository.reactive.ReactiveCrudRepository.deleteAll
- org.springframework.data.repository.reactive.ReactiveCrudRepository.deleteById
- org.springframework.data.repository.reactive.ReactiveCrudRepository.deleteAllById
org.springframework.data.repository.reactive.ReactiveElasticsearchRepository
- org.springframework.data.repository.reactive.ReactiveElasticsearchRepository.delete
- org.springframework.data.repository.reactive.ReactiveElasticsearchRepository.deleteAll
- org.springframework.data.repository.reactive.ReactiveElasticsearchRepository.deleteById
- org.springframework.data.repository.reactive.ReactiveElasticsearchRepository.deleteAllById
org.springframework.data.elasticsearch.core
- org.springframework.data.elasticsearch.core.ElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.delete
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.DocumentOperations.delete
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.delete
- org.springframework.data.elasticsearch.core.IndexOperations.delete
- org.springframework.data.elasticsearch.core.ElasticsearchTemplate.deleteIndex
- org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations.delete
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations.deleteById
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations.deleteBy
- org.springframework.data.elasticsearch.core.ReactiveDocumentOperations.delete
- org.springframework.data.elasticsearch.core.ElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.delete
- org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate.delete
- org.springframework.data.elasticsearch.core.DocumentOperations.delete
- org.springframework.data.elasticsearch.core.ReactiveIndexOperations.delete
org.springframework.data.elasticsearch.client
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient.delete
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient.deleteByQuery
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.delete
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.deleteByQuery
- org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchIndicesClient.delete
- org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.Indices.deleteIndex
- com.github.vanroy.springdata.jest.JestElasticsearchTemplate.delete
|
Objects
|
Java Elasticsearch Cluster |
|
Java Elasticsearch Index |
|
Java Unknown Elasticsearch Cluster |
|
Java Unknown Elasticsearch Index |
Links
All links are created between Elasticsearch Cluster object and
Elasticsearch Index objects:
| parentLink |
- |
| useDeleteLink |
delete
deleteAll
deleteById
deleteAllById |
| useInsertLink |
save
saveAll |
| useSelectLink |
findAllById
findAll
findById
count
existsById
searchSimilar
indexOps
get
multiGet
search |
| useUpdateLink |
bulkUpdate
|
What results can you expect?
Some example scenarios are shown below:
Cluster and Index Creation
public class EsConfig {
@Autowired
private EsSinkProperties properties;
@Bean
public Client client() throws Exception {
Settings esSettings = Settings.builder()
.put("cluster.name", properties.getClusterName())
.build();
TransportClient client = new PreBuiltTransportClient(esSettings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(properties.getHost()), Integer.parseInt(properties.getPort())));
}
}
public class EsSinkProperties {
/
* Elasticsearch cluster name.
*/
private String clusterName = "elasticsearch";
/
* Elasticsearch host name.
*/
private String host = "localhost";
/
* Elasticsearch native port.
*/
private String port = "9300";
@NotBlank
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
}
@Document(indexName = "trader", type = "trade")
public class Trade {
@Id
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

Insert Operation
public override void Configure(EntityTypeBuilder<Contractor> builder)
{
builder.ToTable("Trial");
builder.OwnsOne(m => m.Name, a =>
{
a.Property(p => p.FirstName).HasMaxLength(300)
.HasColumnName("FirstName")
.HasDefaultValue("");
a.Property(p => p.LastName).HasMaxLength(300)
.HasColumnName("LastName")
.HasDefaultValue("");
a.Ignore(p => p.FullName);
});
public Book save(Book book) {
return bookRepository.save(book);
}

Delete Operation
public void delete(Book book) {
bookRepository.delete(book);
}

Select Operation
public Iterable<Book> findAll() {
return bookRepository.findAll();
}

Query Methods
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
List<Book> findByTitle(String title);
}
public Page<Book> findByAuthor(String author, PageRequest pageRequest) {
return bookRepository.findByAuthor(author, pageRequest);
}

Elasticsearch Operations / Elasticsearch Template
public void deleteIndex() {
operations.indexOps(Conference.class).delete();
}

ReactiveElasticsearchClient
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient;
@Repository
public class ElasticRepository {
public static final String COURSE_INDEX = "course-store";
private final ReactiveElasticsearchClient client;
private final ObjectMapper objectMapper;
public ElasticRepository(ReactiveElasticsearchClient client, ObjectMapper objectMapper) {
this.client = client;
this.objectMapper = objectMapper;
}
public Mono<DeleteResponse> deleteCourse() {
return client.delete(deleteRequest -> deleteRequest.index(COURSE_INDEX));
}
}

Known Limitations
- Index is created as unknown, if the name is not retrieved from the
properties file or if the name could not be resolved.
- Limited support for Spring Data Elasticsearch 3.x
- In 3.x, CRUD operations performed using ElasticsearchRepository
are supported
- In 3.x no support for CRUD operations performed using
ElasticsearchTemplate. However, if the user configures version
3.x jars inside its class path, then ElasticsearchTemplate will
produce links.