-
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.
-
-
Field Summary
Fields Modifier and Type Field Description private final String
indexName
private final String
type
private final String
indexWriteAlias
private final String
indexReadAlias
-
Constructor Summary
Constructors Constructor Description IndexRepository(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, Function1<CreateIndexRequest, Unit> block)
Create the index. final GetSettingsResponse
getSettings()
final GetMappingsResponse
getMappings()
final GetIndexResponse
getMappingsAndSettings()
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 IndexResponse
index(String id, T obj, Boolean create, Long seqNo, Long primaryTerm, RequestOptions requestOptions)
Index a document with a given id
.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.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, Function2<BulkOperation<T>, BulkItemResponse, Unit> itemCallback, Function2<BulkIndexingSession<T>, BulkIndexingSession<T>, Unit> operationsBlock)
Create a BulkIndexingSession and use it with the operationsBlock. final BulkIndexingSession<T>
bulkIndexer(Integer bulkSize, Integer retryConflictingUpdates, WriteRequest.RefreshPolicy refreshPolicy, Function2<BulkOperation<T>, BulkItemResponse, Unit> itemCallback, RequestOptions requestOptions)
Returns a BulkIndexingSession for this repository. final Unit
refresh()
Call the refresh API on elasticsearch. final SearchResults<T>
search(Boolean scrolling, Long scrollTtlInMinutes, RequestOptions requestOptions, Function1<SearchRequest, Unit> block)
Perform a search against your index. final Long
count(RequestOptions requestOptions, Function1<CountRequest, Unit> block)
final String
getIndexName()
final String
getType()
final String
getIndexWriteAlias()
final String
getIndexReadAlias()
-
-
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 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, Function1<CreateIndexRequest, Unit> block)
Create the index.
- Parameters:
block
- customize theCreateIndexRequest
{ source(settings, XContentType.JSON) }
-
getSettings
final GetSettingsResponse getSettings()
-
getMappings
final GetMappingsResponse getMappings()
-
getMappingsAndSettings
final GetIndexResponse getMappingsAndSettings()
-
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 IndexResponse 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.The id is nullable and elasticsearch will generate an id for you if you set it to null.
You can optionally specify
seqNo
andprimaryTerm
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
.
-
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, 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 thisnull
, the default callback implementation defined in BulkIndexingSession is used.See BulkIndexingSession for the meaning of the other parameters.
-
bulkIndexer
final BulkIndexingSession<T> bulkIndexer(Integer bulkSize, Integer retryConflictingUpdates, WriteRequest.RefreshPolicy refreshPolicy, Function2<BulkOperation<T>, BulkItemResponse, Unit> itemCallback, RequestOptions requestOptions)
Returns a BulkIndexingSession for this repository. 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 asource
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.
-
getIndexName
final String getIndexName()
-
getIndexWriteAlias
final String getIndexWriteAlias()
-
getIndexReadAlias
final String getIndexReadAlias()
-
-
-
-