Package 

Class IndexRepository


  • 
    public final class IndexRepository<T extends Object>
    
                        

    Repository abstraction that allows you to work with indices.

    You should create a Repository for each index you work with. You need to specify a ModelReaderAndWriter for serialization and deserialization.

    • Constructor Detail

      • IndexRepository

        IndexRepository(String indexName, RestHighLevelClient client, ModelReaderAndWriter<T> modelReaderAndWriter, Boolean refreshAllowed, String type, String indexWriteAlias, String indexReadAlias, FetchSourceContext fetchSourceContext, RequestOptions defaultRequestOptions)
        Parameters:
        indexName - name of the index
        modelReaderAndWriter - serialization of your model class.
        refreshAllowed - if false, the refresh will throw an exception.
        type - the type of the documents in the index; defaults to null.
        indexWriteAlias - Alias used for write operations.
        indexReadAlias - Alias used for read operations.
        fetchSourceContext - if not null, will be passed to Get and Search requests.
        defaultRequestOptions - passed on all API calls.
    • Method Detail

      • createIndex

         final Unit createIndex(RequestOptions requestOptions, ActiveShardCount waitForActiveShards, Function1<CreateIndexRequest, Unit> block)

        Create the index.

        Parameters:
        block - customize the CreateIndexRequest
        {
          source(settings, XContentType.JSON)
        }
      • deleteIndex

         final Boolean deleteIndex(RequestOptions requestOptions)

        Delete the index associated with the repository. Returns true if successful or false if the index did not exist

      • currentAliases

         final Set<AliasMetadata> currentAliases(RequestOptions requestOptions)

        Returns a set of the current AliasMetaData associated with the indexName.

      • index

         final IndexResponse index(String id, T obj, Boolean create, Long seqNo, Long primaryTerm, RequestOptions requestOptions)

        Index a document with a given id. Set create to false for upserts. Otherwise it fails on creating documents that already exist.

        The id is nullable and elasticsearch will generate an id for you if you set it to null.

        You can optionally specify seqNo and primaryTerm to implement optimistic locking. However, you should use update which does this for you.

      • update

         final IndexResponse update(String id, Integer maxUpdateTries, RequestOptions requestOptions, Function1<T, T> transformFunction)

        Updates document identified by id by fetching the current version with get and then applying the transformFunction to produce the updated version.

        if maxUpdateTries 0, it will deal with version conflicts (e.g. due to concurrent updates) by retrying with the latest version.

      • delete

         final Unit delete(String id, RequestOptions requestOptions)

        Deletes the object object identified by id.

      • get

         final T get(String id)

        Returns the deserialized T for the document identified by id.

      • getWithGetResponse

         final Pair<T, GetResponse> getWithGetResponse(String id, RequestOptions requestOptions)

        Returns a Pair of the deserialized T and the GetResponse with all the relevant metadata.

      • bulk

         final Unit bulk(Integer bulkSize, Integer retryConflictingUpdates, WriteRequest.RefreshPolicy refreshPolicy, Function2<BulkOperation<T>, BulkItemResponse, Unit> itemCallback, Function2<BulkIndexingSession<T>, BulkIndexingSession<T>, Unit> operationsBlock)

        Create a BulkIndexingSession and use it with the operationsBlock. Inside the block you can call operations like index and other functions exposed by BulkIndexingSession. The resulting bulk operations are automatically grouped in bulk requests of size bulkSize and sent off to Elasticsearch. For each operation there will be a call to the itemCallback. This allows you to keep track of failures, do logging, or implement retries. If you leave this null, the default callback implementation defined in BulkIndexingSession is used.

        See BulkIndexingSession for the meaning of the other parameters.

      • refresh

         final Unit refresh()

        Call the refresh API on elasticsearch. You should not use this other than in tests. E.g. when testing search queries, you often want to refresh after indexing before calling search

        Throws UnsupportedOperationException if you do not explicitly opt in to this by setting the refreshAllowed to true when creating the repository.

      • search

         final SearchResults<T> search(Boolean scrolling, Long scrollTtlInMinutes, RequestOptions requestOptions, Function1<SearchRequest, Unit> block)

        Perform a search against your index. This creates a SearchRequest that is passed into the block so you can customize it. Inside the block you can manipulate the request to set a source and other parameters.

        Returns a SearchResults instance that you can use to get the deserialized results or the raw response.

        If you want to perform a scrolling search, all you have to do is set scrolling to true (default is false). You can also set a scrollTtlInMinutes if you want something else than the default of 1 minute.

      • count

         final Long count(RequestOptions requestOptions, Function1<CountRequest, Unit> block)