Class ElasticsearchTemplate
java.lang.Object
org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate
- All Implemented Interfaces:
Aware
,ApplicationContextAware
,DocumentOperations
,ElasticsearchOperations
,ScriptOperations
,SearchOperations
Implementation of
ElasticsearchOperations
using the new
Elasticsearch client.- Since:
- 4.4
- Author:
- Peter-Josef Meisch, Hamid Rahimi, Illia Ulianov, Haibo Liu
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback interface to be used withexecute(ElasticsearchTemplate.ClientCallback)
for operating directly on theElasticsearchClient
.Nested classes/interfaces inherited from class org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
AbstractElasticsearchTemplate.DocumentCallback<T>, AbstractElasticsearchTemplate.ReadDocumentCallback<T>, AbstractElasticsearchTemplate.ReadSearchDocumentResponseCallback<T>, AbstractElasticsearchTemplate.ReadSearchScrollDocumentResponseCallback<T>, AbstractElasticsearchTemplate.SearchDocumentResponseCallback<T>
-
Field Summary
Fields inherited from class org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
elasticsearchConverter, entityCallbacks, entityOperations, refreshPolicy, routingResolver
-
Constructor Summary
ConstructorDescriptionElasticsearchTemplate
(co.elastic.clients.elasticsearch.ElasticsearchClient client) ElasticsearchTemplate
(co.elastic.clients.elasticsearch.ElasticsearchClient client, ElasticsearchConverter elasticsearchConverter) -
Method Summary
Modifier and TypeMethodDescriptionvoid
bulkUpdate
(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) Bulk update all objects.protected List<IndexedObjectInformation>
checkForBulkOperationFailure
(co.elastic.clients.elasticsearch.core.BulkResponse bulkResponse) extract the list ofIndexedObjectInformation
from aBulkResponse
.closePointInTime
(String pit) Closes a point in timecluster()
return aClusterOperations
instance that uses the same client communication setup as this ElasticsearchOperations instance.long
count
(Query query, Class<?> clazz, IndexCoordinates index) return number of elements found by given querydelete
(DeleteQuery query, Class<?> clazz) Delete all records matching the query.delete
(DeleteQuery query, Class<?> clazz, IndexCoordinates index) Delete all records matching the query.delete
(Query query, Class<?> clazz, IndexCoordinates index) Delete all records matching the query.boolean
deleteScript
(String name) Deletes the script with the given namedoBulkOperation
(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) protected AbstractElasticsearchTemplate
doCopy()
must return a copy of this instance that will for example be used to set a custom routing resolver without modifying the original object.protected String
doDelete
(String id, String routing, IndexCoordinates index) protected boolean
doExists
(String id, IndexCoordinates index) doIndex
(IndexQuery query, IndexCoordinates indexCoordinates) protected <T> SearchHits<T>
doSearch
(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) protected <T> SearchHits<T>
doSearch
(Query query, Class<T> clazz, IndexCoordinates index) protected <T> SearchHits<T>
doSearch
(SearchTemplateQuery query, Class<T> clazz, IndexCoordinates index) <T> T
execute
(ElasticsearchTemplate.ClientCallback<T> callback) Execute a callback with theElasticsearchClient
and provide exception translation.<T> T
get
(String id, Class<T> clazz, IndexCoordinates index) Retrieves an object from the index specified in the entity's Document annotation.tries to extract the version of the Elasticsearch clusterGest the script with the given name.Creates aQuery
to find get all documents with given ids.get anIndexOperations
that is bound to the given classindexOps
(IndexCoordinates index) get anIndexOperations
that is bound to the given indexCreates aQuery
to get all documents.<T> List<MultiGetItem<T>>
multiGet
(Query query, Class<T> clazz, IndexCoordinates index) Execute a multiGet against elasticsearch for the given ids.<T> List<SearchHits<T>>
multiSearch
(List<? extends Query> queries, Class<T> clazz, IndexCoordinates index) Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.List<SearchHits<?>>
multiSearch
(List<? extends Query> queries, List<Class<?>> classes) Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.List<SearchHits<?>>
multiSearch
(List<? extends Query> queries, List<Class<?>> classes, List<IndexCoordinates> indexes) Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.List<SearchHits<?>>
multiSearch
(List<? extends Query> queries, List<Class<?>> classes, IndexCoordinates index) Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.openPointInTime
(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable) Opens a point in time (pit) in Elasticsearch.boolean
Stores the given script in the Elasticsearch cluster.queryBuilderWithIds
(List<String> ids) Creates aBaseQueryBuilder
that has the given ids setto the parameter value.reindex
(ReindexRequest reindexRequest) Copies documents from a source to a destination.<T> SearchHits<T>
search
(Query query, Class<T> clazz, IndexCoordinates index) Execute the criteria query against elasticsearch and return result asSearchHits
void
searchScrollClear
(List<String> scrollIds) <T> SearchScrollHits<T>
searchScrollContinue
(String scrollId, long scrollTimeInMillis, Class<T> clazz, IndexCoordinates index) <T> SearchScrollHits<T>
searchScrollStart
(long scrollTimeInMillis, Query query, Class<T> clazz, IndexCoordinates index) submitReindex
(ReindexRequest reindexRequest) Submits a reindex task.update
(UpdateQuery updateQuery, IndexCoordinates index) Partial update of the document.updateByQuery
(UpdateQuery updateQuery, IndexCoordinates index) Update document(s) by queryMethods inherited from class org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
buildUpdateQueryByEntity, bulkIndex, bulkIndex, bulkIndex, bulkOperation, bulkUpdate, count, delete, delete, delete, delete, delete, exists, exists, get, getElasticsearchConverter, getEntityCreator, getEntityRouting, getIndexCoordinatesFor, getRefreshPolicy, index, logVersions, maybeCallbackAfterConvert, maybeCallbackAfterLoad, maybeCallbackAfterSave, maybeCallbackAfterSaveWithQueries, maybeCallbackAfterSaveWithQuery, maybeCallbackBeforeConvert, maybeCallbackBeforeConvertWithQueries, maybeCallbackBeforeConvertWithQuery, multiGet, multiSearch, save, save, save, save, save, search, search, search, searchForStream, searchForStream, searchScrollClear, setApplicationContext, setEntityCallbacks, setRefreshPolicy, toArray, update, update, updateIndexedObjectsWithQueries, withRefreshPolicy, withRouting
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.data.elasticsearch.core.DocumentOperations
bulkIndex, bulkUpdate
Methods inherited from interface org.springframework.data.elasticsearch.core.ElasticsearchOperations
convertId
Methods inherited from interface org.springframework.data.elasticsearch.core.SearchOperations
count, openPointInTime, searchOne, searchOne
-
Constructor Details
-
ElasticsearchTemplate
public ElasticsearchTemplate(co.elastic.clients.elasticsearch.ElasticsearchClient client) -
ElasticsearchTemplate
public ElasticsearchTemplate(co.elastic.clients.elasticsearch.ElasticsearchClient client, ElasticsearchConverter elasticsearchConverter)
-
-
Method Details
-
doCopy
Description copied from class:AbstractElasticsearchTemplate
must return a copy of this instance that will for example be used to set a custom routing resolver without modifying the original object.- Specified by:
doCopy
in classAbstractElasticsearchTemplate
-
indexOps
Description copied from interface:ElasticsearchOperations
get anIndexOperations
that is bound to the given class- Returns:
- IndexOperations
-
indexOps
Description copied from interface:ElasticsearchOperations
get anIndexOperations
that is bound to the given index- Returns:
- IndexOperations
-
cluster
Description copied from interface:ElasticsearchOperations
return aClusterOperations
instance that uses the same client communication setup as this ElasticsearchOperations instance.- Returns:
- ClusterOperations implementation
-
get
Description copied from interface:DocumentOperations
Retrieves an object from the index specified in the entity's Document annotation.- Parameters:
id
- the id of the objectclazz
- the entity class,index
- the index from which the object is read.- Returns:
- the entity
-
multiGet
Description copied from interface:DocumentOperations
Execute a multiGet against elasticsearch for the given ids.- Parameters:
query
- the query defining the ids of the objects to getclazz
- the type of the object to be returnedindex
- the index(es) from which the objects are read.- Returns:
- list of
MultiGetItem
s - See Also:
-
bulkUpdate
Description copied from interface:DocumentOperations
Bulk update all objects. Will do update.- Parameters:
queries
- the queries to execute in bulkbulkOptions
- options to be added to the bulk request
-
delete
Description copied from interface:DocumentOperations
Delete all records matching the query.- Parameters:
query
- query defining the objectsclazz
- The entity class must be annotated withDocument
- Returns:
- response with detailed information
-
delete
Description copied from interface:DocumentOperations
Delete all records matching the query.- Parameters:
query
- query defining the objectsclazz
- The entity class, must be annotated withDocument
index
- the index from which to delete- Returns:
- response with detailed information
-
delete
Description copied from interface:DocumentOperations
Delete all records matching the query.- Parameters:
query
- query defining the objectsclazz
- The entity class must be annotated withDocument
index
- the index from which to delete- Returns:
- response with detailed information
-
update
Description copied from interface:DocumentOperations
Partial update of the document.- Parameters:
updateQuery
- query defining the updateindex
- the index where to update the records- Returns:
- the update response
-
updateByQuery
Description copied from interface:DocumentOperations
Update document(s) by query- Parameters:
updateQuery
- query defining the update, must not be nullindex
- the index where to update the records , must not be null- Returns:
- the update response
-
doIndex
- Specified by:
doIndex
in classAbstractElasticsearchTemplate
-
doExists
- Specified by:
doExists
in classAbstractElasticsearchTemplate
-
doDelete
- Specified by:
doDelete
in classAbstractElasticsearchTemplate
-
reindex
Description copied from interface:DocumentOperations
Copies documents from a source to a destination. The source can be any existing index, alias, or data stream. The destination must differ from the source. For example, you cannot reindex a data stream into itself. (@see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html)- Parameters:
reindexRequest
- reindex request parameters- Returns:
- the reindex response
-
submitReindex
Description copied from interface:DocumentOperations
Submits a reindex task. (@see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html)- Parameters:
reindexRequest
- reindex request parameters- Returns:
- the task id
-
doBulkOperation
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) - Specified by:
doBulkOperation
in classAbstractElasticsearchTemplate
-
getClusterVersion
Description copied from class:AbstractElasticsearchTemplate
tries to extract the version of the Elasticsearch cluster- Specified by:
getClusterVersion
in classAbstractElasticsearchTemplate
- Returns:
- the version as string if it can be retrieved
-
getVendor
- Specified by:
getVendor
in classAbstractElasticsearchTemplate
- Returns:
- the vendor name of the used cluster and client library
-
getRuntimeLibraryVersion
- Specified by:
getRuntimeLibraryVersion
in classAbstractElasticsearchTemplate
- Returns:
- the version of the used client runtime library.
-
count
Description copied from interface:SearchOperations
return number of elements found by given query- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mappingindex
- the index to run the query against- Returns:
- count
-
search
Description copied from interface:SearchOperations
Execute the criteria query against elasticsearch and return result asSearchHits
- Type Parameters:
T
- element return type- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mappingindex
- the index to run the query against- Returns:
- SearchHits containing the list of found objects
-
doSearch
-
doSearch
protected <T> SearchHits<T> doSearch(SearchTemplateQuery query, Class<T> clazz, IndexCoordinates index) -
doSearch
protected <T> SearchHits<T> doSearch(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) - Specified by:
doSearch
in classAbstractElasticsearchTemplate
-
searchScrollStart
public <T> SearchScrollHits<T> searchScrollStart(long scrollTimeInMillis, Query query, Class<T> clazz, IndexCoordinates index) - Specified by:
searchScrollStart
in classAbstractElasticsearchTemplate
-
searchScrollContinue
public <T> SearchScrollHits<T> searchScrollContinue(String scrollId, long scrollTimeInMillis, Class<T> clazz, IndexCoordinates index) - Specified by:
searchScrollContinue
in classAbstractElasticsearchTemplate
-
searchScrollClear
- Specified by:
searchScrollClear
in classAbstractElasticsearchTemplate
-
multiSearch
public <T> List<SearchHits<T>> multiSearch(List<? extends Query> queries, Class<T> clazz, IndexCoordinates index) Description copied from interface:SearchOperations
Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.- Type Parameters:
T
- element return type- Parameters:
queries
- the queries to executeclazz
- the entity clazz used for property mappingindex
- the index to run the query against- Returns:
- list of SearchHits
-
multiSearch
Description copied from interface:SearchOperations
Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.- Parameters:
queries
- the queries to executeclasses
- the entity classes- Returns:
- list of SearchHits
-
multiSearch
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes, IndexCoordinates index) Description copied from interface:SearchOperations
Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.- Parameters:
queries
- the queries to executeclasses
- the entity classes used for property mappingindex
- the index to run the queries against- Returns:
- list of SearchHits
-
multiSearch
public List<SearchHits<?>> multiSearch(List<? extends Query> queries, List<Class<?>> classes, List<IndexCoordinates> indexes) Description copied from interface:SearchOperations
Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.- Parameters:
queries
- the queries to executeclasses
- the entity classes used for property mappingindexes
- the indexes to run the queries against- Returns:
- list of SearchHits
-
openPointInTime
public String openPointInTime(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable) Description copied from interface:SearchOperations
Opens a point in time (pit) in Elasticsearch.- Specified by:
openPointInTime
in interfaceSearchOperations
- Overrides:
openPointInTime
in classAbstractElasticsearchTemplate
- Parameters:
index
- the index name(s) to usekeepAlive
- the duration the pit shoult be kept aliveignoreUnavailable
- if {$literal true} the call will fail if any of the indices is missing or closed- Returns:
- the pit identifier
-
closePointInTime
Description copied from interface:SearchOperations
Closes a point in time- Specified by:
closePointInTime
in interfaceSearchOperations
- Overrides:
closePointInTime
in classAbstractElasticsearchTemplate
- Parameters:
pit
- the pit identifier as returned bySearchOperations.openPointInTime(IndexCoordinates, Duration, Boolean)
- Returns:
- true on success
-
putScript
Description copied from interface:ScriptOperations
Stores the given script in the Elasticsearch cluster.- Specified by:
putScript
in interfaceScriptOperations
- Overrides:
putScript
in classAbstractElasticsearchTemplate
- Returns:
- {true if successful
-
getScript
Description copied from interface:ScriptOperations
Gest the script with the given name.- Specified by:
getScript
in interfaceScriptOperations
- Overrides:
getScript
in classAbstractElasticsearchTemplate
- Parameters:
name
- the name of the script- Returns:
- Script or null when a script with this name does not exist.
-
deleteScript
Description copied from interface:ScriptOperations
Deletes the script with the given name- Specified by:
deleteScript
in interfaceScriptOperations
- Overrides:
deleteScript
in classAbstractElasticsearchTemplate
- Parameters:
name
- the name of the script.- Returns:
- true if the request was acknowledged by the cluster.
-
execute
Execute a callback with theElasticsearchClient
and provide exception translation.- Type Parameters:
T
- the type returned from the callback- Parameters:
callback
- the callback to execute, must not be null- Returns:
- the callback result
-
matchAllQuery
Description copied from interface:SearchOperations
Creates aQuery
to get all documents. Must be implemented by the concrete implementations to provide an appropriate query using the respective client.- Returns:
- a query to find all documents
-
idsQuery
Description copied from interface:SearchOperations
Creates aQuery
to find get all documents with given ids. Must be implemented by the concrete implementations to provide an appropriate query using the respective client.- Parameters:
ids
- the list of ids must not be null- Returns:
- query returning the documents with the given ids
-
queryBuilderWithIds
Description copied from interface:SearchOperations
Creates aBaseQueryBuilder
that has the given ids setto the parameter value. No other properties of the bulder are set.- Parameters:
ids
- the list of ids must not be null- Returns:
- query returning the documents with the given ids
-
checkForBulkOperationFailure
protected List<IndexedObjectInformation> checkForBulkOperationFailure(co.elastic.clients.elasticsearch.core.BulkResponse bulkResponse) extract the list ofIndexedObjectInformation
from aBulkResponse
.- Parameters:
bulkResponse
- the response to evaluate- Returns:
- the list of the
IndexedObjectInformation
s
-