-
public final class AsyncIndexRepository<T extends Object>
Repository abstraction that allows you to work with indices asynchronously via co-routines.
You should create a Repository for each index you work with. You need to specify a ModelReaderAndWriter for serialization and deserialization.
-
-
Field Summary
Fields Modifier and Type Field Description private final String
indexName
private final RestHighLevelClient
client
private final String
type
private final String
indexWriteAlias
private final String
indexReadAlias
-
Constructor Summary
Constructors Constructor Description AsyncIndexRepository(String indexName, RestHighLevelClient client, ModelReaderAndWriter<T> modelReaderAndWriter, Boolean refreshAllowed, String type, String indexWriteAlias, String indexReadAlias, FetchSourceContext fetchSourceContext, RequestOptions defaultRequestOptions)
-
Method Summary
Modifier and Type Method Description final Unit
createIndex(RequestOptions requestOptions, ActiveShardCount waitForActiveShards, SuspendFunction1<CreateIndexRequest, Unit> block)
Create the index. final Boolean
deleteIndex(RequestOptions requestOptions)
Delete the index associated with the repository. final Set<AliasMetadata>
currentAliases(RequestOptions requestOptions)
Returns a set of the current AliasMetaData
associated with theindexName
.final Unit
index(String id, T obj, Boolean create, Long seqNo, Long primaryTerm, RequestOptions requestOptions)
Index a document with a given id
.final Unit
update(String id, Integer maxUpdateTries, RequestOptions requestOptions, SuspendFunction1<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.final Unit
delete(String id, RequestOptions requestOptions)
Deletes the object object identified by id
.final T
get(String id)
Returns the deserialized T for the document identified by id
.final Pair<T, GetResponse>
getWithGetResponse(String id, RequestOptions requestOptions)
Returns a Pair
of the deserialized T and theGetResponse
with all the relevant metadata.final Unit
bulk(Integer bulkSize, Integer retryConflictingUpdates, WriteRequest.RefreshPolicy refreshPolicy, SuspendFunction2<AsyncBulkOperation<T>, BulkItemResponse, Unit> itemCallback, CoroutineDispatcher bulkDispatcher, SuspendFunction1<AsyncBulkIndexingSession<T>, Unit> operationsBlock)
Create a BulkIndexingSession and use it with the operationsBlock. final Unit
refresh()
Call the refresh API on elasticsearch. final AsyncSearchResults<T>
search(Boolean scrolling, Long scrollTtlInMinutes, RequestOptions requestOptions, Function1<SearchRequest, Unit> block)
Perform an asynchronous search against your index. final Long
count(RequestOptions requestOptions, Function1<CountRequest, Unit> block)
final String
getIndexName()
final RestHighLevelClient
getClient()
final String
getType()
final String
getIndexWriteAlias()
final String
getIndexReadAlias()
-
-
Constructor Detail
-
AsyncIndexRepository
AsyncIndexRepository(String indexName, RestHighLevelClient client, ModelReaderAndWriter<T> modelReaderAndWriter, Boolean refreshAllowed, String type, String indexWriteAlias, String indexReadAlias, FetchSourceContext fetchSourceContext, RequestOptions defaultRequestOptions)
- Parameters:
indexName
- name of the indexmodelReaderAndWriter
- 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, SuspendFunction1<CreateIndexRequest, Unit> block)
Create the index.
- Parameters:
block
- customize theCreateIndexRequest
{ 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 theindexName
.
-
index
final Unit index(String id, T obj, Boolean create, Long seqNo, Long primaryTerm, RequestOptions requestOptions)
Index a document with a given
id
. Setcreate
tofalse
for upserts. Otherwise it fails on creating documents that already exist.You can optionally specify
seqNo
andprimaryTerm
to implement optimistic locking. However, you should use update which does this for you.
-
update
final Unit update(String id, Integer maxUpdateTries, RequestOptions requestOptions, SuspendFunction1<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
.
-
getWithGetResponse
final Pair<T, GetResponse> getWithGetResponse(String id, RequestOptions requestOptions)
Returns a
Pair
of the deserialized T and theGetResponse
with all the relevant metadata.
-
bulk
final Unit bulk(Integer bulkSize, Integer retryConflictingUpdates, WriteRequest.RefreshPolicy refreshPolicy, SuspendFunction2<AsyncBulkOperation<T>, BulkItemResponse, Unit> itemCallback, CoroutineDispatcher bulkDispatcher, SuspendFunction1<AsyncBulkIndexingSession<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 thisnull
, 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 AsyncSearchResults<T> search(Boolean scrolling, Long scrollTtlInMinutes, RequestOptions requestOptions, Function1<SearchRequest, Unit> block)
Perform an asynchronous search against your index. Works similar to the synchronous version, except it returns
AsyncSearchResults
with a flow of responses that get mapped toT
.Similar to the synchronous version, it supports scrolling.
-
getIndexName
final String getIndexName()
-
getClient
final RestHighLevelClient getClient()
-
getIndexWriteAlias
final String getIndexWriteAlias()
-
getIndexReadAlias
final String getIndexReadAlias()
-
-
-
-