Class AbstractElasticsearchTemplate
java.lang.Object
org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
- All Implemented Interfaces:
Aware
,ApplicationContextAware
,DocumentOperations
,ElasticsearchOperations
,ScriptOperations
,SearchOperations
,SqlOperations
- Direct Known Subclasses:
ElasticsearchTemplate
public abstract class AbstractElasticsearchTemplate
extends Object
implements ElasticsearchOperations, ApplicationContextAware
This class contains methods that are common to different implementations of the
ElasticsearchOperations
interface that use different clients, like the different Java clients from Elasticsearch or some external
implementation that might use a different client. This class must not contain imports or use classes that are
specific to one of these implementations.
Note: Although this class is public, it is not considered to be part of the official Spring Data Elasticsearch API and so might change at any time.
- Author:
- Sascha Woo, Peter-Josef Meisch, Roman Puchkovskiy, Subhobrata Dey, Steven Pearce, Anton Naydenov, Haibo Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
protected class
protected class
protected class
protected static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ElasticsearchConverter
protected EntityCallbacks
protected EntityOperations
protected RefreshPolicy
protected RoutingResolver
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractElasticsearchTemplate
(ElasticsearchConverter elasticsearchConverter) -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> UpdateQuery
buildUpdateQueryByEntity
(T entity) bulkIndex
(List<IndexQuery> queries, Class<?> clazz) Bulk index all objects.bulkIndex
(List<IndexQuery> queries, BulkOptions bulkOptions, Class<?> clazz) Bulk index all objects.final List<IndexedObjectInformation>
bulkIndex
(List<IndexQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) Bulk index all objects.bulkOperation
(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) void
bulkUpdate
(List<UpdateQuery> queries, Class<?> clazz) Bulk update all objects.closePointInTime
(String pit) Closes a point in timelong
return number of elements found by given queryDeletes the given entitydelete
(Object entity, IndexCoordinates index) Deletes the given entityDelete the one object with provided id.delete
(String id, IndexCoordinates index) Delete the one object with provided id.boolean
deleteScript
(String name) Deletes the script with the given nameabstract List<IndexedObjectInformation>
doBulkOperation
(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) protected abstract 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 abstract String
doDelete
(String id, String routing, IndexCoordinates index) protected abstract boolean
doExists
(String id, IndexCoordinates index) abstract String
doIndex
(IndexQuery query, IndexCoordinates indexCoordinates) protected abstract <T> SearchHits<T>
doSearch
(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) boolean
Check if an entity with given id exists.boolean
exists
(String id, IndexCoordinates index) Check if an entity with given id exists.<T> T
Retrieves an object from the index specified in the entity's Document annotation.abstract String
tries to extract the version of the Elasticsearch clusterprotected <T> SearchDocumentResponse.EntityCreator<T>
getEntityCreator
(AbstractElasticsearchTemplate.ReadDocumentCallback<T> documentCallback) getEntityRouting
(Object entity) gets the routing for an entity which might be defined by a join-type relationgetIndexCoordinatesFor
(Class<?> clazz) abstract String
Gest the script with the given name.abstract String
index
(IndexQuery query, IndexCoordinates index) Index an object.void
logs the versions of the different Elasticsearch components.protected <T> T
maybeCallbackAfterConvert
(T entity, Document document, IndexCoordinates index) protected <T> Document
maybeCallbackAfterLoad
(Document document, Class<T> type, IndexCoordinates indexCoordinates) protected <T> T
maybeCallbackAfterSave
(T entity, IndexCoordinates index) protected void
maybeCallbackAfterSaveWithQueries
(List<?> queries, IndexCoordinates index) protected void
maybeCallbackAfterSaveWithQuery
(Object query, IndexCoordinates index) protected <T> T
maybeCallbackBeforeConvert
(T entity, IndexCoordinates index) protected void
maybeCallbackBeforeConvertWithQueries
(List<?> queries, IndexCoordinates index) protected void
maybeCallbackBeforeConvertWithQuery
(Object query, IndexCoordinates index) <T> List<MultiGetItem<T>>
Execute a multiGet against elasticsearch for the given ids.<T> List<SearchHits<T>>
multiSearch
(List<? extends Query> queries, Class<T> clazz) 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.<T> Iterable<T>
saves the given entities to the index retrieved from the entities' Document annotation<T> Iterable<T>
save
(Iterable<T> entities, IndexCoordinates index) saves the given entities to the given index<T> T
save
(T entity) Saves an entity to the index specified in the entity's Document annotationfinal <T> Iterable<T>
save
(T... entities) saves the given entities to the index retrieved from the entities' Document annotation<T> T
save
(T entity, IndexCoordinates index) Saves an entity to the index specified in the entity's Document annotation<T> SearchHits<T>
search
(MoreLikeThisQuery query, Class<T> clazz) more like this query to search for documents that are "like" a specific document.<T> SearchHits<T>
search
(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) more like this query to search for documents that are "like" a specific document.<T> SearchHits<T>
Execute the criteria query against elasticsearch and return result asSearchHits
<T> SearchHitsIterator<T>
searchForStream
(Query query, Class<T> clazz) Executes the givenQuery
against elasticsearch and return result asSearchHitsIterator
.<T> SearchHitsIterator<T>
searchForStream
(Query query, Class<T> clazz, IndexCoordinates index) Executes the givenQuery
against elasticsearch and return result asSearchHitsIterator
.void
searchScrollClear
(String scrollId) abstract void
searchScrollClear
(List<String> scrollIds) abstract <T> SearchScrollHits<T>
searchScrollContinue
(String scrollId, long scrollTimeInMillis, Class<T> clazz, IndexCoordinates index) abstract <T> SearchScrollHits<T>
searchScrollStart
(long scrollTimeInMillis, Query query, Class<T> clazz, IndexCoordinates index) void
setApplicationContext
(ApplicationContext applicationContext) void
setEntityCallbacks
(EntityCallbacks entityCallbacks) void
setRefreshPolicy
(RefreshPolicy refreshPolicy) protected static String[]
<T> UpdateResponse
update
(T entity) Partially update a document by the given entity.<T> UpdateResponse
update
(T entity, IndexCoordinates index) Partially update a document by the given entity.protected void
updateIndexedObjectsWithQueries
(List<?> queries, List<IndexedObjectInformation> indexedObjectInformationList) withRefreshPolicy
(RefreshPolicy refreshPolicy) Returns a copy of this instance with the same configuration, but that uses a differentRefreshPolicy
.withRouting
(RoutingResolver routingResolver) Returns a copy of this instance with the same configuration, but that uses a differentRoutingResolver
to obtain routing information.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, bulkUpdate, delete, delete, get, multiGet, reindex, submitReindex, update, updateByQuery
Methods inherited from interface org.springframework.data.elasticsearch.core.ElasticsearchOperations
cluster, convertId, indexOps, indexOps
Methods inherited from interface org.springframework.data.elasticsearch.core.SearchOperations
count, count, idsQuery, matchAllQuery, multiSearch, multiSearch, multiSearch, multiSearch, openPointInTime, queryBuilderWithIds, search, searchOne, searchOne
Methods inherited from interface org.springframework.data.elasticsearch.core.sql.SqlOperations
search
-
Field Details
-
elasticsearchConverter
-
entityOperations
-
entityCallbacks
-
refreshPolicy
-
routingResolver
-
-
Constructor Details
-
AbstractElasticsearchTemplate
public AbstractElasticsearchTemplate() -
AbstractElasticsearchTemplate
-
-
Method Details
-
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. -
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
setEntityCallbacks
Set theEntityCallbacks
instance to use when invokingcallbacks
like theBeforeConvertCallback
. Overrides potentially existingEntityCallbacks
.- Parameters:
entityCallbacks
- must not be null.- Throws:
IllegalArgumentException
- if the given instance is null.- Since:
- 4.0
-
setRefreshPolicy
-
getRefreshPolicy
-
logVersions
public void logVersions()logs the versions of the different Elasticsearch components.- Since:
- 4.3
-
save
public <T> T save(T entity) Description copied from interface:DocumentOperations
Saves an entity to the index specified in the entity's Document annotation- Specified by:
save
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to save, must not be null- Returns:
- the saved entity
-
save
Description copied from interface:DocumentOperations
Saves an entity to the index specified in the entity's Document annotation- Specified by:
save
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to save, must not be nullindex
- the index to save the entity in, must not be null- Returns:
- the saved entity
-
save
Description copied from interface:DocumentOperations
saves the given entities to the index retrieved from the entities' Document annotation- Specified by:
save
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entities
- must not be null- Returns:
- the saved entites
-
save
Description copied from interface:DocumentOperations
saves the given entities to the given index- Specified by:
save
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entities
- must not be nullindex
- the index to save the entities in, must not be null- Returns:
- the saved entities
-
save
Description copied from interface:DocumentOperations
saves the given entities to the index retrieved from the entities' Document annotation- Specified by:
save
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entities
- must not be null- Returns:
- the saved entities as Iterable
-
index
Description copied from interface:DocumentOperations
Index an object. Will do save or update.- Specified by:
index
in interfaceDocumentOperations
- Parameters:
query
- the query defining the objectindex
- the index where the object is stored.- Returns:
- returns the document id
-
doIndex
-
get
Description copied from interface:DocumentOperations
Retrieves an object from the index specified in the entity's Document annotation.- Specified by:
get
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
id
- the id of the objectclazz
- the entity class,- Returns:
- the entity
-
multiGet
Description copied from interface:DocumentOperations
Execute a multiGet against elasticsearch for the given ids.- Specified by:
multiGet
in interfaceDocumentOperations
- Parameters:
query
- the query defining the ids of the objects to getclazz
- the type of the object to be returned- Returns:
- list of
MultiGetItem
s - See Also:
-
exists
Description copied from interface:DocumentOperations
Check if an entity with given id exists.- Specified by:
exists
in interfaceDocumentOperations
- Parameters:
id
- the _id of the document to look for.clazz
- the domain type used.- Returns:
- true if a matching document exists, false otherwise.
-
exists
Description copied from interface:DocumentOperations
Check if an entity with given id exists.- Specified by:
exists
in interfaceDocumentOperations
- Parameters:
id
- the _id of the document to look for.index
- the target index, must not be null- Returns:
- true if a matching document exists, false otherwise.
-
doExists
-
delete
Description copied from interface:DocumentOperations
Delete the one object with provided id.- Specified by:
delete
in interfaceDocumentOperations
- Parameters:
id
- the document ot deleteentityType
- must not be null.- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperations
Deletes the given entity- Specified by:
delete
in interfaceDocumentOperations
- Parameters:
entity
- the entity to delete- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperations
Deletes the given entity- Specified by:
delete
in interfaceDocumentOperations
- Parameters:
entity
- the entity to deleteindex
- the index from which to delete- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperations
Delete the one object with provided id.- Specified by:
delete
in interfaceDocumentOperations
- Parameters:
id
- the document to deleteindex
- the index from which to delete- Returns:
- documentId of the document deleted
-
doDelete
-
bulkIndex
Description copied from interface:DocumentOperations
Bulk index all objects. Will do save or update.- Specified by:
bulkIndex
in interfaceDocumentOperations
- Parameters:
queries
- the queries to execute in bulkclazz
- the entity class- Returns:
- the information about the indexed objects
-
bulkIndex
public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions, Class<?> clazz) Description copied from interface:DocumentOperations
Bulk index all objects. Will do save or update.- Specified by:
bulkIndex
in interfaceDocumentOperations
- Parameters:
queries
- the queries to execute in bulkbulkOptions
- options to be added to the bulk requestclazz
- the entity class- Returns:
- the information about of the indexed objects
-
bulkIndex
public final List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) Description copied from interface:DocumentOperations
Bulk index all objects. Will do save or update.- Specified by:
bulkIndex
in interfaceDocumentOperations
- Parameters:
queries
- the queries to execute in bulkbulkOptions
- options to be added to the bulk request- Returns:
- the information about of the indexed objects
-
bulkUpdate
Description copied from interface:DocumentOperations
Bulk update all objects. Will do update.- Specified by:
bulkUpdate
in interfaceDocumentOperations
- Parameters:
queries
- the queries to execute in bulkclazz
- the entity class
-
bulkOperation
public List<IndexedObjectInformation> bulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) -
doBulkOperation
public abstract List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) -
update
Description copied from interface:DocumentOperations
Partially update a document by the given entity.- Specified by:
update
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to update partially, must not be null.- Returns:
- the update response
-
update
Description copied from interface:DocumentOperations
Partially update a document by the given entity.- Specified by:
update
in interfaceDocumentOperations
- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to update partially, must not be null.index
- the index to use for the update instead of the one defined by the entity, must not be null- Returns:
- the update response
-
buildUpdateQueryByEntity
-
count
Description copied from interface:SearchOperations
return number of elements found by given query- Specified by:
count
in interfaceSearchOperations
- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mapping and index name extraction- Returns:
- count
-
searchForStream
Description copied from interface:SearchOperations
Executes the givenQuery
against elasticsearch and return result asSearchHitsIterator
.- Specified by:
searchForStream
in interfaceSearchOperations
- Type Parameters:
T
- element return type- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mapping and index name extraction- Returns:
- a
SearchHitsIterator
that wraps an Elasticsearch scroll context that needs to be closed. The try-with-resources construct should be used to ensure that the close method is invoked after the operations are completed.
-
searchForStream
public <T> SearchHitsIterator<T> searchForStream(Query query, Class<T> clazz, IndexCoordinates index) Description copied from interface:SearchOperations
Executes the givenQuery
against elasticsearch and return result asSearchHitsIterator
.- Specified by:
searchForStream
in interfaceSearchOperations
- 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:
- a
SearchHitsIterator
that wraps an Elasticsearch scroll context that needs to be closed. The try-with-resources construct should be used to ensure that the close method is invoked after the operations are completed.
-
search
Description copied from interface:SearchOperations
more like this query to search for documents that are "like" a specific document.- Specified by:
search
in interfaceSearchOperations
- Type Parameters:
T
- element return type- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mapping and index name extraction- Returns:
- SearchHits containing the list of found objects
-
search
Description copied from interface:SearchOperations
more like this query to search for documents that are "like" a specific document.- Specified by:
search
in interfaceSearchOperations
- 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
protected abstract <T> SearchHits<T> doSearch(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) -
multiSearch
Description copied from interface:SearchOperations
Execute the multi search query against elasticsearch and return result asList
ofSearchHits
.- Specified by:
multiSearch
in interfaceSearchOperations
- Type Parameters:
T
- element return type- Parameters:
queries
- the queries to executeclazz
- the entity clazz- Returns:
- list of SearchHits
-
search
Description copied from interface:SearchOperations
Execute the criteria query against elasticsearch and return result asSearchHits
- Specified by:
search
in interfaceSearchOperations
- Type Parameters:
T
- element return type- Parameters:
query
- the query to executeclazz
- the entity clazz used for property mapping and index name extraction- Returns:
- SearchHits containing the list of found objects
-
searchScrollStart
public abstract <T> SearchScrollHits<T> searchScrollStart(long scrollTimeInMillis, Query query, Class<T> clazz, IndexCoordinates index) -
searchScrollContinue
public abstract <T> SearchScrollHits<T> searchScrollContinue(String scrollId, long scrollTimeInMillis, Class<T> clazz, IndexCoordinates index) -
searchScrollClear
-
searchScrollClear
-
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
- 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
- Parameters:
pit
- the pit identifier as returned bySearchOperations.openPointInTime(IndexCoordinates, Duration, Boolean)
- Returns:
- true on success
-
getElasticsearchConverter
- Specified by:
getElasticsearchConverter
in interfaceElasticsearchOperations
-
toArray
-
getIndexCoordinatesFor
- Specified by:
getIndexCoordinatesFor
in interfaceElasticsearchOperations
- Parameters:
clazz
- the entity class- Returns:
- the IndexCoordinates defined on the entity.
- Since:
- 4.0
-
getEntityRouting
Description copied from interface:ElasticsearchOperations
gets the routing for an entity which might be defined by a join-type relation- Specified by:
getEntityRouting
in interfaceElasticsearchOperations
- Parameters:
entity
- the entity- Returns:
- the routing, may be null if not set.
-
getEntityCreator
protected <T> SearchDocumentResponse.EntityCreator<T> getEntityCreator(AbstractElasticsearchTemplate.ReadDocumentCallback<T> documentCallback) -
getClusterVersion
tries to extract the version of the Elasticsearch cluster- Returns:
- the version as string if it can be retrieved
-
getVendor
- Returns:
- the vendor name of the used cluster and client library
- Since:
- 4.3
-
getRuntimeLibraryVersion
- Returns:
- the version of the used client runtime library.
- Since:
- 4.3
-
maybeCallbackBeforeConvert
-
maybeCallbackBeforeConvertWithQuery
-
maybeCallbackBeforeConvertWithQueries
-
maybeCallbackAfterSave
-
maybeCallbackAfterSaveWithQuery
-
maybeCallbackAfterSaveWithQueries
-
maybeCallbackAfterConvert
-
maybeCallbackAfterLoad
protected <T> Document maybeCallbackAfterLoad(Document document, Class<T> type, IndexCoordinates indexCoordinates) -
updateIndexedObjectsWithQueries
protected void updateIndexedObjectsWithQueries(List<?> queries, List<IndexedObjectInformation> indexedObjectInformationList) -
putScript
Description copied from interface:ScriptOperations
Stores the given script in the Elasticsearch cluster.- Specified by:
putScript
in interfaceScriptOperations
- Returns:
- {true if successful
-
getScript
Description copied from interface:ScriptOperations
Gest the script with the given name.- Specified by:
getScript
in interfaceScriptOperations
- 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
- Parameters:
name
- the name of the script.- Returns:
- true if the request was acknowledged by the cluster.
-
withRouting
Description copied from interface:ElasticsearchOperations
Returns a copy of this instance with the same configuration, but that uses a differentRoutingResolver
to obtain routing information.- Specified by:
withRouting
in interfaceElasticsearchOperations
- Parameters:
routingResolver
- theRoutingResolver
value, must not be null.- Returns:
ElasticsearchOperations
instance
-
withRefreshPolicy
Description copied from interface:ElasticsearchOperations
Returns a copy of this instance with the same configuration, but that uses a differentRefreshPolicy
.- Specified by:
withRefreshPolicy
in interfaceElasticsearchOperations
- Parameters:
refreshPolicy
- theRefreshPolicy
value.- Returns:
ElasticsearchOperations
instance.
-