Class AbstractElasticsearchTemplate
java.lang.Object
org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate
- All Implemented Interfaces:
Aware,ApplicationContextAware,DocumentOperations,ElasticsearchOperations,ScriptOperations,SearchOperations
- Direct Known Subclasses:
ElasticsearchRestTemplate,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 RestHighLevelClient and the next Java client 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 interfaceprotected classprotected classprotected classprotected static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ElasticsearchConverterprotected EntityCallbacksprotected EntityOperationsprotected RefreshPolicyprotected RoutingResolver -
Constructor Summary
ConstructorsConstructorDescriptionAbstractElasticsearchTemplate(ElasticsearchConverter elasticsearchConverter) -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> UpdateQuerybuildUpdateQueryByEntity(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) voidbulkUpdate(List<UpdateQuery> queries, Class<?> clazz) Bulk update all objects.closePointInTime(String pit) Closes a point in timelongreturn 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.Delete all records matching the query.booleandeleteScript(String name) Deletes the script with the given nameabstract List<IndexedObjectInformation>doBulkOperation(List<?> queries, BulkOptions bulkOptions, IndexCoordinates index) protected abstract AbstractElasticsearchTemplatedoCopy()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 StringdoDelete(String id, String routing, IndexCoordinates index) protected abstract booleandoExists(String id, IndexCoordinates index) abstract StringdoIndex(IndexQuery query, IndexCoordinates indexCoordinates) protected abstract <T> SearchHits<T>doSearch(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) booleanCheck if an entity with given id exists.booleanexists(String id, IndexCoordinates index) Check if an entity with given id exists.<T> TRetrieves an object from the index specified in the entity's Document annotation.abstract Stringtries 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 StringGest the script with the given name.abstract Stringindex(IndexQuery query, IndexCoordinates index) Index an object.voidlogs the versions of the different Elasticsearch components.protected <T> TmaybeCallbackAfterConvert(T entity, Document document, IndexCoordinates index) protected <T> DocumentmaybeCallbackAfterLoad(Document document, Class<T> type, IndexCoordinates indexCoordinates) protected <T> TmaybeCallbackAfterSave(T entity, IndexCoordinates index) protected voidmaybeCallbackAfterSaveWithQueries(List<?> queries, IndexCoordinates index) protected voidmaybeCallbackAfterSaveWithQuery(Object query, IndexCoordinates index) protected <T> TmaybeCallbackBeforeConvert(T entity, IndexCoordinates index) protected voidmaybeCallbackBeforeConvertWithQueries(List<?> queries, IndexCoordinates index) protected voidmaybeCallbackBeforeConvertWithQuery(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 asListofSearchHits.openPointInTime(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable) Opens a point in time (pit) in Elasticsearch.booleanStores 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> Tsave(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> Tsave(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 givenQueryagainst elasticsearch and return result asSearchHitsIterator.<T> SearchHitsIterator<T>searchForStream(Query query, Class<T> clazz, IndexCoordinates index) Executes the givenQueryagainst elasticsearch and return result asSearchHitsIterator.voidsearchScrollClear(String scrollId) abstract voidsearchScrollClear(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) voidsetApplicationContext(ApplicationContext applicationContext) voidsetEntityCallbacks(EntityCallbacks entityCallbacks) voidsetRefreshPolicy(RefreshPolicy refreshPolicy) protected static String[]<T> UpdateResponseupdate(T entity) Partially update a document by the given entity.<T> UpdateResponseupdate(T entity, IndexCoordinates index) Partially update a document by the given entity.protected <T> TupdateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) protected voidupdateIndexedObjectsWithQueries(List<?> queries, List<IndexedObjectInformation> indexedObjectInformationList) withRouting(RoutingResolver routingResolver) Returns a copy of this instance with the same configuration, but that uses a differentRoutingResolverto obtain routing information.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.elasticsearch.core.DocumentOperations
bulkIndex, bulkUpdate, bulkUpdate, delete, get, multiGet, reindex, submitReindex, update, updateByQueryMethods inherited from interface org.springframework.data.elasticsearch.core.ElasticsearchOperations
cluster, convertId, indexOps, indexOps, stringIdRepresentationMethods inherited from interface org.springframework.data.elasticsearch.core.SearchOperations
count, count, idsQuery, matchAllQuery, multiSearch, multiSearch, multiSearch, multiSearch, openPointInTime, search, searchOne, searchOne
-
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:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-
setEntityCallbacks
Set theEntityCallbacksinstance to use when invokingcallbackslike 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:DocumentOperationsSaves an entity to the index specified in the entity's Document annotation- Specified by:
savein 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:DocumentOperationsSaves an entity to the index specified in the entity's Document annotation- Specified by:
savein 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:DocumentOperationssaves the given entities to the index retrieved from the entities' Document annotation- Specified by:
savein interfaceDocumentOperations- Type Parameters:
T- the entity type- Parameters:
entities- must not be null- Returns:
- the saved entites
-
save
Description copied from interface:DocumentOperationssaves the given entities to the given index- Specified by:
savein 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:DocumentOperationssaves the given entities to the index retrieved from the entities' Document annotation- Specified by:
savein interfaceDocumentOperations- Type Parameters:
T- the entity type- Parameters:
entities- must not be null- Returns:
- the saved entities as Iterable
-
index
Description copied from interface:DocumentOperationsIndex an object. Will do save or update.- Specified by:
indexin 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:DocumentOperationsRetrieves an object from the index specified in the entity's Document annotation.- Specified by:
getin 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:DocumentOperationsExecute a multiGet against elasticsearch for the given ids.- Specified by:
multiGetin interfaceDocumentOperations- Parameters:
query- the query defining the ids of the objects to getclazz- the type of the object to be returned- Returns:
- list of
MultiGetItems - See Also:
-
exists
Description copied from interface:DocumentOperationsCheck if an entity with given id exists.- Specified by:
existsin 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:DocumentOperationsCheck if an entity with given id exists.- Specified by:
existsin 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:DocumentOperationsDelete the one object with provided id.- Specified by:
deletein interfaceDocumentOperations- Parameters:
id- the document ot deleteentityType- must not be null.- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperationsDelete all records matching the query.- Specified by:
deletein interfaceDocumentOperations- Parameters:
query- query defining the objectsclazz- The entity class, must be annotated withDocument- Returns:
- response with detailed information
-
delete
Description copied from interface:DocumentOperationsDeletes the given entity- Specified by:
deletein interfaceDocumentOperations- Parameters:
entity- the entity to delete- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperationsDeletes the given entity- Specified by:
deletein interfaceDocumentOperations- Parameters:
entity- the entity to deleteindex- the index from which to delete- Returns:
- documentId of the document deleted
-
delete
Description copied from interface:DocumentOperationsDelete the one object with provided id.- Specified by:
deletein 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:DocumentOperationsBulk index all objects. Will do save or update.- Specified by:
bulkIndexin 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:DocumentOperationsBulk index all objects. Will do save or update.- Specified by:
bulkIndexin 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:DocumentOperationsBulk index all objects. Will do save or update.- Specified by:
bulkIndexin 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:DocumentOperationsBulk update all objects. Will do update.- Specified by:
bulkUpdatein 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:DocumentOperationsPartially update a document by the given entity.- Specified by:
updatein 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:DocumentOperationsPartially update a document by the given entity.- Specified by:
updatein 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
-
updateIndexedObject
-
count
Description copied from interface:SearchOperationsreturn number of elements found by given query- Specified by:
countin 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:SearchOperationsExecutes the givenQueryagainst elasticsearch and return result asSearchHitsIterator.- Specified by:
searchForStreamin 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
SearchHitsIteratorthat 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:SearchOperationsExecutes the givenQueryagainst elasticsearch and return result asSearchHitsIterator.- Specified by:
searchForStreamin 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
SearchHitsIteratorthat 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:SearchOperationsmore like this query to search for documents that are "like" a specific document.- Specified by:
searchin 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:SearchOperationsmore like this query to search for documents that are "like" a specific document.- Specified by:
searchin 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:SearchOperationsExecute the multi search query against elasticsearch and return result asListofSearchHits.- Specified by:
multiSearchin 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:SearchOperationsExecute the criteria query against elasticsearch and return result asSearchHits- Specified by:
searchin 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:SearchOperationsOpens a point in time (pit) in Elasticsearch.- Specified by:
openPointInTimein 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:SearchOperationsCloses a point in time- Specified by:
closePointInTimein interfaceSearchOperations- Parameters:
pit- the pit identifier as returned bySearchOperations.openPointInTime(IndexCoordinates, Duration, Boolean)- Returns:
- true on success
-
getElasticsearchConverter
- Specified by:
getElasticsearchConverterin interfaceElasticsearchOperations
-
toArray
-
getIndexCoordinatesFor
- Specified by:
getIndexCoordinatesForin interfaceElasticsearchOperations- Parameters:
clazz- the entity class- Returns:
- the IndexCoordinates defined on the entity.
- Since:
- 4.0
-
getEntityRouting
Description copied from interface:ElasticsearchOperationsgets the routing for an entity which might be defined by a join-type relation- Specified by:
getEntityRoutingin 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:ScriptOperationsStores the given script in the Elasticsearch cluster.- Specified by:
putScriptin interfaceScriptOperations- Returns:
- {true if successful
-
getScript
Description copied from interface:ScriptOperationsGest the script with the given name.- Specified by:
getScriptin 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:ScriptOperationsDeletes the script with the given name- Specified by:
deleteScriptin interfaceScriptOperations- Parameters:
name- the name of the script.- Returns:
- true if the request was acknowledged by the cluster.
-
withRouting
Description copied from interface:ElasticsearchOperationsReturns a copy of this instance with the same configuration, but that uses a differentRoutingResolverto obtain routing information.- Specified by:
withRoutingin interfaceElasticsearchOperations- Parameters:
routingResolver- theRoutingResolvervalue, must not be null.- Returns:
- DocumentOperations instance
-