Class AbstractElasticsearchIndex

java.lang.Object
org.opencastproject.elasticsearch.impl.AbstractElasticsearchIndex
All Implemented Interfaces:
org.opencastproject.elasticsearch.api.SearchIndex

public abstract class AbstractElasticsearchIndex extends Object implements org.opencastproject.elasticsearch.api.SearchIndex
A search index implementation based on ElasticSearch.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Configuration key defining the password of an external Elasticsearch server
    static final String
    Configuration key defining the hostname of an external Elasticsearch server
    static final String
    Configuration key defining the port of an external Elasticsearch server
    static final String
    Configuration key defining the scheme (http/https) of an external Elasticsearch server
    static final String
    Configuration key defining the username of an external Elasticsearch server
    protected String
    The path to the index settings

    Fields inherited from interface org.opencastproject.elasticsearch.api.SearchIndex

    INDEX_VERSION
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    activate(Map<String,Object> properties, org.osgi.framework.BundleContext bundleContext)
    OSGi callback to activate this component instance.
    protected org.elasticsearch.action.bulk.BulkResponse
    bulkUpdate(int maxRetryAttempts, int retryWaitingPeriod, List<ElasticsearchDocument> documents)
    Posts the input documents to the search index.
    void
     
    protected void
    Closes the client.
    protected org.elasticsearch.action.delete.DeleteResponse
    delete(String type, String id, int maxRetryAttempts, int retryWaitingPeriod)
    Delete document from index.
    protected <T> org.opencastproject.elasticsearch.api.SearchResult<T>
    executeQuery(org.opencastproject.elasticsearch.api.SearchQuery query, org.elasticsearch.action.search.SearchRequest request, Function<SearchMetadataCollection,T> toSearchResult, int maxRetryAttempts, int retryWaitingPeriod)
    Execute a query on the index.
    org.elasticsearch.client.RestHighLevelClient
     
    abstract String[]
    Returns an array of document types for the index.
    Returns the name of this index.
    int
     
    protected org.elasticsearch.action.search.SearchRequest
    getSearchRequest(org.opencastproject.elasticsearch.api.SearchQuery query, org.elasticsearch.index.query.QueryBuilder queryBuilder)
    Creates a request for a search query based on the properties known by the search query.
    protected String
    Returns the name of the sub index for the given type.
    Returns all the known terms for a field (aka facets).
    protected long
    getTotalHits(org.elasticsearch.search.SearchHits hits)
     
    protected void
    init(int version)
    Initializes an Elasticsearch node for the given index.
    void
    modified(Map<String,Object> properties)
    OSGi callback for configuration changes.
    protected org.elasticsearch.action.index.IndexResponse
    update(int maxRetryAttempts, int retryWaitingPeriod, ElasticsearchDocument document)
    Posts the input document to the search index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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()
      Specified by:
      getIndexVersion in interface org.opencastproject.elasticsearch.api.SearchIndex
    • clear

      public void clear() throws IOException
      Specified by:
      clear in interface org.opencastproject.elasticsearch.api.SearchIndex
      Throws:
      IOException
    • 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, org.opencastproject.elasticsearch.api.SearchIndexException
      Initializes an Elasticsearch node for the given index.
      Parameters:
      version - the index version
      Throws:
      org.opencastproject.elasticsearch.api.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(org.opencastproject.elasticsearch.api.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> org.opencastproject.elasticsearch.api.SearchResult<T> executeQuery(org.opencastproject.elasticsearch.api.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