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
  • Field Details

    • elasticsearchConverter

      protected ElasticsearchConverter elasticsearchConverter
    • entityOperations

      protected EntityOperations entityOperations
    • entityCallbacks

      protected @Nullable EntityCallbacks entityCallbacks
    • refreshPolicy

      protected @Nullable RefreshPolicy refreshPolicy
    • routingResolver

      protected RoutingResolver routingResolver
    • observationRegistry

      protected io.micrometer.observation.ObservationRegistry observationRegistry
  • Constructor Details

    • AbstractElasticsearchTemplate

      public AbstractElasticsearchTemplate()
    • AbstractElasticsearchTemplate

      public AbstractElasticsearchTemplate(@Nullable ElasticsearchConverter elasticsearchConverter)
  • Method Details

    • doCopy

      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.
    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Throws:
      BeansException
    • setEntityCallbacks

      public void setEntityCallbacks(EntityCallbacks entityCallbacks)
      Set the EntityCallbacks instance to use when invoking callbacks like the BeforeConvertCallback. Overrides potentially existing EntityCallbacks.
      Parameters:
      entityCallbacks - must not be null.
      Throws:
      IllegalArgumentException - if the given instance is null.
      Since:
      4.0
    • setRefreshPolicy

      public void setRefreshPolicy(@Nullable RefreshPolicy refreshPolicy)
    • getRefreshPolicy

      public @Nullable RefreshPolicy getRefreshPolicy()
    • setObservationRegistry

      public void setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
      Set the ObservationRegistry to use for recording observations.
      Parameters:
      observationRegistry - must not be null.
      Since:
      6.1
    • customizeCopy

      protected void customizeCopy(AbstractElasticsearchTemplate copy)
      Hook for subclasses to copy additional state during copy(). Called after all common fields have been copied. The default implementation does nothing.
      Parameters:
      copy - the new template instance to customize
    • 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 interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entity - the entity to save, must not be null
      Returns:
      the saved entity
    • save

      public <T> T save(T entity, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Saves an entity to the index specified in the entity's Document annotation
      Specified by:
      save in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entity - the entity to save, must not be null
      index - the index to save the entity in, must not be null
      Returns:
      the saved entity
    • save

      public <T> Iterable<T> save(Iterable<T> entities)
      Description copied from interface: DocumentOperations
      saves the given entities to the index retrieved from the entities' Document annotation
      Specified by:
      save in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entities - must not be null
      Returns:
      the saved entites
    • save

      public <T> Iterable<T> save(Iterable<T> entities, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      saves the given entities to the given index
      Specified by:
      save in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entities - must not be null
      index - the index to save the entities in, must not be null
      Returns:
      the saved entities
    • save

      @SafeVarargs public final <T> Iterable<T> save(T... entities)
      Description copied from interface: DocumentOperations
      saves the given entities to the index retrieved from the entities' Document annotation
      Specified by:
      save in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entities - must not be null
      Returns:
      the saved entities as Iterable
    • index

      public String index(IndexQuery query, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Index an object. Will do save or update.
      Specified by:
      index in interface DocumentOperations
      Parameters:
      query - the query defining the object
      index - the index where the object is stored.
      Returns:
      returns the document id
    • doIndex

      public abstract String doIndex(IndexQuery query, IndexCoordinates indexCoordinates)
    • get

      public <T> @Nullable T get(String id, Class<T> clazz)
      Description copied from interface: DocumentOperations
      Retrieves an object from the index specified in the entity's Document annotation.
      Specified by:
      get in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      id - the id of the object
      clazz - the entity class,
      Returns:
      the entity
    • multiGet

      public <T> List<MultiGetItem<T>> multiGet(Query query, Class<T> clazz)
      Description copied from interface: DocumentOperations
      Execute a multiGet against elasticsearch for the given ids.
      Specified by:
      multiGet in interface DocumentOperations
      Parameters:
      query - the query defining the ids of the objects to get
      clazz - the type of the object to be returned
      Returns:
      list of MultiGetItems
      See Also:
    • exists

      public boolean exists(String id, Class<?> clazz)
      Description copied from interface: DocumentOperations
      Check if an entity with given id exists.
      Specified by:
      exists in interface DocumentOperations
      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

      public boolean exists(String id, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Check if an entity with given id exists.
      Specified by:
      exists in interface DocumentOperations
      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

      protected abstract boolean doExists(String id, IndexCoordinates index)
    • delete

      public String delete(String id, Class<?> entityType)
      Description copied from interface: DocumentOperations
      Delete the one object with provided id.
      Specified by:
      delete in interface DocumentOperations
      Parameters:
      id - the document ot delete
      entityType - must not be null.
      Returns:
      documentId of the document deleted
    • delete

      public String delete(Object entity)
      Description copied from interface: DocumentOperations
      Deletes the given entity
      Specified by:
      delete in interface DocumentOperations
      Parameters:
      entity - the entity to delete
      Returns:
      documentId of the document deleted
    • delete

      public String delete(Object entity, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Deletes the given entity
      Specified by:
      delete in interface DocumentOperations
      Parameters:
      entity - the entity to delete
      index - the index from which to delete
      Returns:
      documentId of the document deleted
    • delete

      public String delete(String id, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Delete the one object with provided id.
      Specified by:
      delete in interface DocumentOperations
      Parameters:
      id - the document to delete
      index - the index from which to delete
      Returns:
      documentId of the document deleted
    • doDelete

      protected abstract String doDelete(String id, @Nullable String routing, IndexCoordinates index)
    • bulkIndex

      public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, Class<?> clazz)
      Description copied from interface: DocumentOperations
      Bulk index all objects. Will do save or update.
      Specified by:
      bulkIndex in interface DocumentOperations
      Parameters:
      queries - the queries to execute in bulk
      clazz - 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 interface DocumentOperations
      Parameters:
      queries - the queries to execute in bulk
      bulkOptions - options to be added to the bulk request
      clazz - 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 interface DocumentOperations
      Parameters:
      queries - the queries to execute in bulk
      bulkOptions - options to be added to the bulk request
      Returns:
      the information about of the indexed objects
    • bulkUpdate

      public void bulkUpdate(List<UpdateQuery> queries, Class<?> clazz)
      Description copied from interface: DocumentOperations
      Bulk update all objects. Will do update.
      Specified by:
      bulkUpdate in interface DocumentOperations
      Parameters:
      queries - the queries to execute in bulk
      clazz - 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

      public <T> UpdateResponse update(T entity)
      Description copied from interface: DocumentOperations
      Partially update a document by the given entity.
      Specified by:
      update in interface DocumentOperations
      Type Parameters:
      T - the entity type
      Parameters:
      entity - the entity to update partially, must not be null.
      Returns:
      the update response
    • update

      public <T> UpdateResponse update(T entity, IndexCoordinates index)
      Description copied from interface: DocumentOperations
      Partially update a document by the given entity.
      Specified by:
      update in interface DocumentOperations
      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

      protected <T> UpdateQuery buildUpdateQueryByEntity(T entity)
    • count

      public long count(Query query, Class<?> clazz)
      Description copied from interface: SearchOperations
      return number of elements found by given query
      Specified by:
      count in interface SearchOperations
      Parameters:
      query - the query to execute
      clazz - the entity clazz used for property mapping and index name extraction
      Returns:
      count
    • searchForStream

      public <T> SearchHitsIterator<T> searchForStream(Query query, Class<T> clazz)
      Description copied from interface: SearchOperations
      Executes the given Query against elasticsearch and return result as SearchHitsIterator.
      Specified by:
      searchForStream in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      query - the query to execute
      clazz - 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 given Query against elasticsearch and return result as SearchHitsIterator.
      Specified by:
      searchForStream in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      query - the query to execute
      clazz - the entity clazz used for property mapping
      index - 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

      public <T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz)
      Description copied from interface: SearchOperations
      more like this query to search for documents that are "like" a specific document.
      Specified by:
      search in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      query - the query to execute
      clazz - the entity clazz used for property mapping and index name extraction
      Returns:
      SearchHits containing the list of found objects
    • search

      public <T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index)
      Description copied from interface: SearchOperations
      more like this query to search for documents that are "like" a specific document.
      Specified by:
      search in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      query - the query to execute
      clazz - the entity clazz used for property mapping
      index - 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

      public <T> List<SearchHits<T>> multiSearch(List<? extends Query> queries, Class<T> clazz)
      Description copied from interface: SearchOperations
      Execute the multi search query against elasticsearch and return result as List of SearchHits.
      Specified by:
      multiSearch in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      queries - the queries to execute
      clazz - the entity clazz
      Returns:
      list of SearchHits
    • search

      public <T> SearchHits<T> search(Query query, Class<T> clazz)
      Description copied from interface: SearchOperations
      Execute the criteria query against elasticsearch and return result as SearchHits
      Specified by:
      search in interface SearchOperations
      Type Parameters:
      T - element return type
      Parameters:
      query - the query to execute
      clazz - 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

      public void searchScrollClear(String scrollId)
    • searchScrollClear

      public abstract void searchScrollClear(List<String> scrollIds)
    • 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 interface SearchOperations
      Parameters:
      index - the index name(s) to use
      keepAlive - the duration the pit shoult be kept alive
      ignoreUnavailable - if {$literal true} the call will fail if any of the indices is missing or closed
      Returns:
      the pit identifier
    • closePointInTime

      public Boolean closePointInTime(String pit)
      Description copied from interface: SearchOperations
      Closes a point in time
      Specified by:
      closePointInTime in interface SearchOperations
      Parameters:
      pit - the pit identifier as returned by SearchOperations.openPointInTime(IndexCoordinates, Duration, Boolean)
      Returns:
      true on success
    • getElasticsearchConverter

      public ElasticsearchConverter getElasticsearchConverter()
      Specified by:
      getElasticsearchConverter in interface ElasticsearchOperations
    • toArray

      protected static String[] toArray(List<String> values)
    • getIndexCoordinatesFor

      public IndexCoordinates getIndexCoordinatesFor(Class<?> clazz)
      Specified by:
      getIndexCoordinatesFor in interface ElasticsearchOperations
      Parameters:
      clazz - the entity class
      Returns:
      the IndexCoordinates defined on the entity.
      Since:
      4.0
    • getEntityRouting

      public @Nullable String getEntityRouting(Object entity)
      Description copied from interface: ElasticsearchOperations
      gets the routing for an entity which might be defined by a join-type relation
      Specified by:
      getEntityRouting in interface ElasticsearchOperations
      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

      public abstract @Nullable String getClusterVersion()
      tries to extract the version of the Elasticsearch cluster
      Returns:
      the version as string if it can be retrieved
    • getVendor

      public abstract String getVendor()
      Returns:
      the vendor name of the used cluster and client library
      Since:
      4.3
    • getRuntimeLibraryVersion

      public abstract String getRuntimeLibraryVersion()
      Returns:
      the version of the used client runtime library.
      Since:
      4.3
    • maybeCallbackBeforeConvert

      protected <T> T maybeCallbackBeforeConvert(T entity, IndexCoordinates index)
    • maybeCallbackBeforeConvertWithQuery

      protected void maybeCallbackBeforeConvertWithQuery(Object query, IndexCoordinates index)
    • maybeCallbackBeforeConvertWithQueries

      protected void maybeCallbackBeforeConvertWithQueries(List<?> queries, IndexCoordinates index)
    • maybeCallbackAfterSave

      protected <T> T maybeCallbackAfterSave(T entity, IndexCoordinates index)
    • maybeCallbackAfterSaveWithQuery

      protected void maybeCallbackAfterSaveWithQuery(Object query, IndexCoordinates index)
    • maybeCallbackAfterSaveWithQueries

      protected void maybeCallbackAfterSaveWithQueries(List<?> queries, IndexCoordinates index)
    • maybeCallbackAfterConvert

      protected <T> T maybeCallbackAfterConvert(T entity, Document document, IndexCoordinates index)
    • maybeCallbackAfterLoad

      protected <T> Document maybeCallbackAfterLoad(Document document, Class<T> type, IndexCoordinates indexCoordinates)
    • updateIndexedObjectsWithQueries

      protected void updateIndexedObjectsWithQueries(List<?> queries, List<IndexedObjectInformation> indexedObjectInformationList)
    • putScript

      public boolean putScript(Script script)
      Description copied from interface: ScriptOperations
      Stores the given script in the Elasticsearch cluster.
      Specified by:
      putScript in interface ScriptOperations
      Returns:
      {true if successful
    • getScript

      public @Nullable Script getScript(String name)
      Description copied from interface: ScriptOperations
      Gest the script with the given name.
      Specified by:
      getScript in interface ScriptOperations
      Parameters:
      name - the name of the script
      Returns:
      Script or null when a script with this name does not exist.
    • deleteScript

      public boolean deleteScript(String name)
      Description copied from interface: ScriptOperations
      Deletes the script with the given name
      Specified by:
      deleteScript in interface ScriptOperations
      Parameters:
      name - the name of the script.
      Returns:
      true if the request was acknowledged by the cluster.
    • withRouting

      public ElasticsearchOperations withRouting(RoutingResolver routingResolver)
      Description copied from interface: ElasticsearchOperations
      Returns a copy of this instance with the same configuration, but that uses a different RoutingResolver to obtain routing information.
      Specified by:
      withRouting in interface ElasticsearchOperations
      Parameters:
      routingResolver - the RoutingResolver value, must not be null.
      Returns:
      ElasticsearchOperations instance
    • withRefreshPolicy

      public ElasticsearchOperations withRefreshPolicy(@Nullable RefreshPolicy refreshPolicy)
      Description copied from interface: ElasticsearchOperations
      Returns a copy of this instance with the same configuration, but that uses a different RefreshPolicy.
      Specified by:
      withRefreshPolicy in interface ElasticsearchOperations
      Parameters:
      refreshPolicy - the RefreshPolicy value.
      Returns:
      ElasticsearchOperations instance.