Interface ElasticsearchTemplate
- All Superinterfaces:
org.eclipse.jnosql.mapping.document.DocumentTemplate,org.eclipse.jnosql.mapping.semistructured.SemiStructuredTemplate,jakarta.nosql.Template
DocumentTemplate contract for the mapping layer.
This interface extends the standard Eclipse JNoSQL DocumentTemplate
operations with support for native Elasticsearch search requests. The inherited
template methods provide the regular mapped document operations, while
search(SearchRequest) allows callers to execute an Elasticsearch
SearchRequest and receive mapped entity instances.
The native search method is useful when an application needs Elasticsearch features that are not directly represented by the standard JNoSQL mapping API, such as custom query DSL structures, aggregations, highlighting, sorting, pagination, or other Elasticsearch-specific search capabilities.
Implementations are expected to delegate the native request to the underlying Elasticsearch communication layer and map the returned documents back to the corresponding entity type managed by Eclipse JNoSQL.
@Inject
private ElasticsearchTemplate elasticsearchTemplate;
SearchRequest request = new SearchRequest.Builder()
.index("documents")
.query(query -> query.match(match -> match
.field("title")
.query("Eclipse JNoSQL")))
.build();
Stream<Document> documents = elasticsearchTemplate.search(request);
documents.forEach(System.out::println);
- See Also:
-
DocumentTemplateSearchRequest
-
Method Summary
Methods inherited from interface org.eclipse.jnosql.mapping.semistructured.SemiStructuredTemplate
count, count, count, delete, deleteAll, exists, findAll, prepare, prepare, query, query, select, selectCursor, selectOffSet, singleResult, singleResult, singleResultMethods inherited from interface jakarta.nosql.Template
delete, delete, find, insert, insert, insert, insert, select, update, update
-
Method Details
-
search
Executes a native Elasticsearch search request and maps the returned documents to entity instances.This method should be used when the standard JNoSQL mapping operations are not expressive enough for the required Elasticsearch query. The provided
SearchRequestis passed to the Elasticsearch Java client through the communication layer, and each returned document is converted to the mapped entity type.The request must not be
null. The target index should normally be consistent with the entity mapping or with the index explicitly defined in the request.@Inject private ElasticsearchTemplate elasticsearchTemplate; SearchRequest request = SearchRequest.of(search -> search .index("documents") .query(query -> query .match(match -> match .field("title") .query("Eclipse JNoSQL")))); Stream<Document> documents = elasticsearchTemplate.search(request); documents.forEach(document -> { System.out.println(document); });- Type Parameters:
T- the entity type returned by the search operation- Parameters:
request- the native Elasticsearch search request- Returns:
- a stream of mapped entities returned by the Elasticsearch search operation
- Throws:
NullPointerException- when the search request isnull
-