Class AbstractElasticsearchIndex

java.lang.Object
org.opencastproject.elasticsearch.impl.AbstractElasticsearchIndex
All Implemented Interfaces:
SearchIndex
Direct Known Subclasses:
ElasticsearchIndex

public abstract class AbstractElasticsearchIndex extends Object implements SearchIndex
A search index implementation based on ElasticSearch.
  • Field Details

    • ELASTICSEARCH_SERVER_HOSTNAME_KEY

      public static final String ELASTICSEARCH_SERVER_HOSTNAME_KEY
      Configuration key defining the hostname of an external Elasticsearch server
      See Also:
    • ELASTICSEARCH_SERVER_SCHEME_KEY

      public static final String ELASTICSEARCH_SERVER_SCHEME_KEY
      Configuration key defining the scheme (http/https) of an external Elasticsearch server
      See Also:
    • ELASTICSEARCH_SERVER_PORT_KEY

      public static final String ELASTICSEARCH_SERVER_PORT_KEY
      Configuration key defining the port of an external Elasticsearch server
      See Also:
    • ELASTICSEARCH_USERNAME_KEY

      public static final String ELASTICSEARCH_USERNAME_KEY
      Configuration key defining the username of an external Elasticsearch server
      See Also:
    • ELASTICSEARCH_PASSWORD_KEY

      public static final String ELASTICSEARCH_PASSWORD_KEY
      Configuration key defining the password of an external Elasticsearch server
      See Also:
    • indexSettingsPath

      protected String indexSettingsPath
      The path to the index settings
  • Constructor Details

    • AbstractElasticsearchIndex

      public AbstractElasticsearchIndex()
  • Method Details

    • getDocumentTypes

      public abstract String[] getDocumentTypes()
      Returns an array of document types for the index. For every one of these, the corresponding document type definition will be loaded.
      Returns:
      the document types
    • activate

      public void activate(Map<String,Object> properties, org.osgi.framework.BundleContext bundleContext) throws org.osgi.service.component.ComponentException
      OSGi callback to activate this component instance.
      Parameters:
      properties - The configuration
      bundleContext - the bundle context
      Throws:
      org.osgi.service.component.ComponentException - if the search index cannot be initialized
    • modified

      public void modified(Map<String,Object> properties)
      OSGi callback for configuration changes.
      Parameters:
      properties - The configuration
    • getIndexVersion

      public int getIndexVersion()
      Description copied from interface: SearchIndex
      Returns the index's version number. If that number is different from SearchIndex.INDEX_VERSION, a reindex is needed, since the index's structure could have changed significantly.
      Specified by:
      getIndexVersion in interface SearchIndex
      Returns:
      the index version
    • clear

      public void clear() throws IOException
      Description copied from interface: SearchIndex
      Clears the search index.
      Specified by:
      clear in interface SearchIndex
      Throws:
      IOException - if clearing the index fails
    • update

      protected org.elasticsearch.action.index.IndexResponse update(int maxRetryAttempts, int retryWaitingPeriod, ElasticsearchDocument document) throws IOException, InterruptedException
      Posts the input document to the search index.
      Parameters:
      maxRetryAttempts - How often to retry update in case of ElasticsearchStatusException
      retryWaitingPeriod - How long to wait (in ms) between retries
      document - The Elasticsearch document
      Returns:
      the query response
      Throws:
      IOException - If updating the index fails
      InterruptedException - If waiting during retry is interrupted
    • bulkUpdate

      protected org.elasticsearch.action.bulk.BulkResponse bulkUpdate(int maxRetryAttempts, int retryWaitingPeriod, List<ElasticsearchDocument> documents) throws IOException, InterruptedException
      Posts the input documents to the search index.
      Parameters:
      maxRetryAttempts - How often to retry update in case of ElasticsearchStatusException
      retryWaitingPeriod - How long to wait (in ms) between retries
      documents - The Elasticsearch documents
      Returns:
      the query response
      Throws:
      IOException - If updating the index fails
      InterruptedException - If waiting during retry is interrupted
    • delete

      protected org.elasticsearch.action.delete.DeleteResponse delete(String type, String id, int maxRetryAttempts, int retryWaitingPeriod) throws IOException, InterruptedException
      Delete document from index.
      Parameters:
      type - The type of document we want to delete
      id - The identifier of the document
      Returns:
      The delete response
      Throws:
      IOException - If deleting from the index fails
      InterruptedException - If waiting during retry is interrupted
    • init

      protected void init(int version) throws IOException, IllegalArgumentException, SearchIndexException
      Initializes an Elasticsearch node for the given index.
      Parameters:
      version - the index version
      Throws:
      SearchIndexException - if the index configuration cannot be loaded
      IOException - if loading of settings fails
      IllegalArgumentException - if the index identifier is blank.
    • close

      protected void close() throws IOException
      Closes the client.
      Throws:
      IOException - if stopping the Elasticsearch node fails
    • getSearchRequest

      protected org.elasticsearch.action.search.SearchRequest getSearchRequest(SearchQuery query, org.elasticsearch.index.query.QueryBuilder queryBuilder)
      Creates a request for a search query based on the properties known by the search query.

      Once this query builder has been created, support for ordering needs to be configured as needed.

      Parameters:
      query - the search query
      Returns:
      the request builder
    • getIndexName

      public String getIndexName()
      Returns the name of this index.
      Returns:
      the index name
    • getTotalHits

      protected long getTotalHits(org.elasticsearch.search.SearchHits hits)
    • getSubIndexIdentifier

      protected String getSubIndexIdentifier(String type)
      Returns the name of the sub index for the given type.
      Parameters:
      type - The type to get the sub index for.
      Returns:
      the index name
    • getClient

      public org.elasticsearch.client.RestHighLevelClient getClient()
    • executeQuery

      protected <T> SearchResult<T> executeQuery(SearchQuery query, org.elasticsearch.action.search.SearchRequest request, Function<SearchMetadataCollection,T> toSearchResult, int maxRetryAttempts, int retryWaitingPeriod) throws IOException, InterruptedException
      Execute a query on the index.
      Parameters:
      query - The query to use to find the results
      request - The builder to use to create the query.
      toSearchResult - The function to convert the results to a SearchResult
      maxRetryAttempts - How often to retry query in case of ElasticsearchStatusException
      retryWaitingPeriod - How long to wait (in ms) between retries
      Returns:
      A SearchResult containing the relevant objects.
      Throws:
      IOException - If querying the index fails
      InterruptedException - If waiting during retry is interrupted
    • getTermsForField

      public List<String> getTermsForField(String field, String type)
      Returns all the known terms for a field (aka facets).
      Parameters:
      field - the field name
      type - the document type
      Returns:
      the list of terms