SearchClient

class SearchClient(val restClient: RestClient = KtorRestClient(), val json: Json = DEFAULT_JSON) : Closeable

Search client that you can use to talk to Elasticsearch or Opensearch.

Most client api functions are implemented as extension functions.

Parameters

restClient

rest client configured to talk to your search engine. Defaults to KtorRestClient configured to talk to localhost:9200.

json

kotlinx.serialization Json used to deserialize responses. Defaults to DEFAULT_JSON which is configured with some sane defaults.

Constructors

Link copied to clipboard
constructor(restClient: RestClient = KtorRestClient(), json: Json = DEFAULT_JSON)

Properties

Link copied to clipboard
val json: Json
Link copied to clipboard

Functions

Link copied to clipboard
suspend fun SearchClient.analyze(target: String? = null, block: AnalyzeRequest.() -> Unit): AnalyzeResponse
Link copied to clipboard
suspend fun SearchClient.bulk(payload: String, target: String? = null, pipeline: String? = null, refresh: Refresh? = null, routing: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, requireAlias: Boolean? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, extraParameters: Map<String, String>? = null): <Error class: unknown class>

Send a single bulk request. Consider using the variant that creates a BulkSession.

suspend fun SearchClient.bulk(bulkSize: Int = 100, target: String? = null, pipeline: String? = null, refresh: Refresh? = Refresh.WaitFor, routing: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, requireAlias: Boolean? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, failOnFirstError: Boolean = false, callBack: BulkItemCallBack? = null, closeOnRequestError: Boolean = true, extraParameters: Map<String, String>? = null, block: suspend BulkSession.() -> Unit)

Creates a bulk session that allows you to index, create, or delete items via the convenient kotlin DSL.

Link copied to clipboard
fun SearchClient.bulkSession(bulkSize: Int = 100, target: String? = null, pipeline: String? = null, refresh: Refresh? = null, routing: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, requireAlias: Boolean? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, failOnFirstError: Boolean = false, callBack: BulkItemCallBack? = null, closeOnRequestError: Boolean = true, extraParameters: Map<String, String>? = null): BulkSession
Link copied to clipboard
suspend fun SearchClient.cancelTask(id: String, waitForCompletion: Boolean = false, timeout: Duration = 1.minutes): JsonObject
Link copied to clipboard
open override fun close()
Link copied to clipboard
suspend fun SearchClient.clusterHealth(extraParameters: Map<String, String>? = null): ClusterHealthResponse
Link copied to clipboard
suspend fun SearchClient.count(target: String? = null, query: ESQuery, allowNoIndices: Boolean? = null, expandWildcards: ExpandWildCards? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, minScore: Double? = null, preference: String? = null, routing: String? = null, terminateAfter: Int? = null): CountResponse

Variant of the _count api that allows you to pass in an ESQuery object. It will be set as the query on the json body that is sent to _count.

suspend fun SearchClient.count(target: String? = null, allowNoIndices: Boolean? = null, expandWildcards: ExpandWildCards? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, minScore: Double? = null, preference: String? = null, routing: String? = null, terminateAfter: Int? = null, block: SearchDSL.() -> Unit): CountResponse

Variant of the _count api that takes a search dsl block so you can set the query. Note, not all parts of the search dsl are supported by _count. E.g. adding sorting would be an error.

suspend fun SearchClient.count(target: String? = null, rawJson: String? = null, allowNoIndices: Boolean? = null, expandWildcards: ExpandWildCards? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, minScore: Double? = null, preference: String? = null, routing: String? = null, terminateAfter: Int? = null): CountResponse

Variant of the _count api that takes an optional rawBody. Leaving the body empty means doing a match_all search.

Link copied to clipboard
suspend fun SearchClient.createDataStream(name: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.createIndex(name: String, mapping: IndexSettingsAndMappingsDSL, waitForActiveShards: Int? = null, masterTimeOut: Duration? = null, timeout: Duration? = null, extraParameters: Map<String, String>? = null): IndexCreateResponse
suspend fun SearchClient.createIndex(name: String, waitForActiveShards: Int? = null, masterTimeOut: Duration? = null, timeout: Duration? = null, extraParameters: Map<String, String>? = null, block: IndexSettingsAndMappingsDSL.() -> Unit? = null): IndexCreateResponse
suspend fun SearchClient.createIndex(name: String, mappingAndSettings: String, waitForActiveShards: Int? = null, masterTimeOut: Duration? = null, timeout: Duration? = null, extraParameters: Map<String, String>? = null): IndexCreateResponse
Link copied to clipboard
suspend fun SearchClient.createIndexTemplate(templateId: String, block: IndexTemplate.() -> Unit): JsonObject
Link copied to clipboard
suspend fun SearchClient.createPointInTime(name: String, keepAlive: Duration): String

Create a point in time for use with e.g. search_after.

Link copied to clipboard
suspend fun SearchClient.deleteByQuery(target: String?, dsl: SearchDSL, allowNoIndices: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, conflicts: String? = null, defaultOperator: SearchOperator? = null, df: String? = null, expandWildcards: ExpandWildCards? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxDocs: Int? = null, preference: String? = null, q: String? = null, refresh: Boolean? = null, requestCache: Boolean? = null, requestsPerSecond: Int? = null, routing: String? = null, scroll: String? = null, scrollSize: Int? = null, searchType: SearchType? = null, searchTimeout: String? = null, slices: Int? = null, sort: String? = null, stats: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, version: Boolean? = null, waitForExtraShards: String? = null, extraParameters: Map<String, String>? = null): DeleteByQueryResponse
suspend fun SearchClient.deleteByQuery(target: String?, allowNoIndices: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, conflicts: String? = null, defaultOperator: SearchOperator? = null, df: String? = null, expandWildcards: ExpandWildCards? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxDocs: Int? = null, preference: String? = null, q: String? = null, refresh: Boolean? = null, requestCache: Boolean? = null, requestsPerSecond: Int? = null, routing: String? = null, scroll: String? = null, scrollSize: Int? = null, searchType: SearchType? = null, searchTimeout: String? = null, slices: Int? = null, sort: String? = null, stats: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, version: Boolean? = null, waitForExtraShards: String? = null, extraParameters: Map<String, String>? = null, block: SearchDSL.() -> Unit): DeleteByQueryResponse
suspend fun SearchClient.deleteByQuery(target: String?, rawJson: String?, allowNoIndices: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, conflicts: String? = null, defaultOperator: SearchOperator? = null, df: String? = null, expandWildcards: ExpandWildCards? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxDocs: Int? = null, preference: String? = null, q: String? = null, refresh: Boolean? = null, requestCache: Boolean? = null, requestsPerSecond: Int? = null, routing: String? = null, scroll: String? = null, scrollSize: Int? = null, searchType: SearchType? = null, searchTimeout: String? = null, slices: Int? = null, sort: String? = null, stats: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, version: Boolean? = null, waitForExtraShards: String? = null, extraParameters: Map<String, String>? = null): DeleteByQueryResponse
Link copied to clipboard
suspend fun SearchClient.deleteComponentTemplate(templateId: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteDataStream(name: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteDocument(target: String, id: String, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, refresh: Refresh? = null, routing: String? = null, timeout: Duration? = null, version: Long? = null, versionType: VersionType? = null, waitForActiveShards: String? = null, extraParameters: Map<String, String>? = null): DocumentIndexResponse
Link copied to clipboard
suspend fun SearchClient.deleteIlmPolicy(policyId: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteIndex(target: String, masterTimeOut: Duration? = null, timeout: Duration? = null, ignoreUnavailable: Boolean? = null, extraParameters: Map<String, String>? = null): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteIndexTemplate(templateId: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.deletePointInTime(id: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteScroll(scrollId: String?): JsonObject

Delete a scroll by id.

Link copied to clipboard
suspend fun SearchClient.deleteSnapshot(repositoryName: String, snapshotName: String, waitForCompletion: Boolean = false, timeout: Duration = 1.minutes): JsonObject
Link copied to clipboard
suspend fun SearchClient.deleteSnapshotRepository(repositoryName: String): JsonObject
Link copied to clipboard

Cheap way to access the version information returned by root

Link copied to clipboard
suspend fun SearchClient.exists(name: String): Boolean
Link copied to clipboard
suspend fun SearchClient.getAliases(target: String? = null): Map<String, AliasResponse>
Link copied to clipboard
suspend fun SearchClient.getDocument(target: String, id: String, preference: String? = null, realtime: Boolean? = null, refresh: Refresh? = null, routing: String? = null, storedFields: String? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, version: Long? = null, versionType: VersionType? = null, extraParameters: Map<String, String>? = null): GetDocumentResponse
Link copied to clipboard
suspend fun SearchClient.getIlmPolicy(policyId: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.getIndex(name: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.getIndexMappings(name: String): JsonObject
Link copied to clipboard
suspend fun SearchClient.getSnapshotRepository(repositoryName: String?): JsonObject
Link copied to clipboard
suspend fun SearchClient.getTask(id: String?, waitForCompletion: Boolean = false, timeout: Duration = 1.minutes): JsonObject
Link copied to clipboard
suspend fun SearchClient.indexDocument(target: String, serializedJson: String, id: String? = null, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, opType: OperationType? = null, pipeline: String? = null, refresh: Refresh? = null, routing: String? = null, timeout: Duration? = null, version: Long? = null, versionType: VersionType? = null, waitForActiveShards: String? = null, requireAlias: Boolean? = null, extraParameters: Map<String, String>? = null): DocumentIndexResponse
inline suspend fun <T> SearchClient.indexDocument(target: String, document: T, id: String? = null, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, opType: OperationType? = null, pipeline: String? = null, refresh: Refresh? = null, routing: String? = null, timeout: Duration? = null, version: Long? = null, versionType: VersionType? = null, waitForActiveShards: String? = null, requireAlias: Boolean? = null, extraParameters: Map<String, String>? = null, json: Json = DEFAULT_JSON): DocumentIndexResponse
Link copied to clipboard
fun <T : Any> SearchClient.ktorModelSerializer(serializer: KSerializer<T>, customJson: Json? = null): KotlinxSerializationModelSerializationStrategy<T>
Link copied to clipboard
suspend fun SearchClient.listSnapshots(repositoryName: String, pattern: String = "_all"): JsonObject
Link copied to clipboard
suspend fun SearchClient.mGet(index: String? = null, preference: String? = null, realtime: Boolean? = null, refresh: Refresh? = null, routing: String? = null, storedFields: String? = null, source: String? = null, block: MGetRequest.() -> Unit): MGetResponse
Link copied to clipboard
suspend fun SearchClient.msearch(target: String?, allowNoIndices: Boolean? = null, cssMinimizeRoundtrips: Boolean? = null, expandWildcards: ExpandWildCards? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, maxConcurrentSearches: Int? = null, maxConcurrentShardRequests: Int? = null, preFilterShardSize: Int? = null, routing: String? = null, searchType: SearchType? = null, typedKeys: Boolean? = null, block: MsearchRequest.() -> Unit): MultiSearchResponse
suspend fun SearchClient.msearch(target: String? = null, body: String?, allowNoIndices: Boolean? = null, cssMinimizeRoundtrips: Boolean? = null, expandWildcards: ExpandWildCards? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, maxConcurrentSearches: Int? = null, maxConcurrentShardRequests: Int? = null, preFilterShardSize: Int? = null, routing: String? = null, searchType: SearchType? = null, typedKeys: Boolean? = null): MultiSearchResponse
Link copied to clipboard
suspend fun SearchClient.registerSnapshotRepository(repositoryName: String, repository: SnapshotRepository): JsonObject
suspend fun SearchClient.registerSnapshotRepository(repositoryName: String, block: SnapshotRepository.() -> Unit)
Link copied to clipboard
suspend fun SearchClient.reindex(refresh: Boolean? = null, timeout: Duration? = null, waitForActiveShards: String? = null, requestsPerSecond: Int? = null, requireAlias: Boolean? = null, scroll: Duration? = null, slices: Int? = null, maxDocs: Int? = null, block: ReindexDSL.() -> Unit): ReindexResponse
Link copied to clipboard
suspend fun SearchClient.reindexAsync(refresh: Boolean? = null, timeout: Duration? = null, waitForActiveShards: String? = null, requestsPerSecond: Int? = null, requireAlias: Boolean? = null, scroll: Duration? = null, slices: Int? = null, maxDocs: Int? = null, block: ReindexDSL.() -> Unit): TaskId
Link copied to clipboard
fun <T : Any> SearchClient.repository(indexName: String, serializer: KSerializer<T>, indexWriteAlias: String = indexName, indexReadAlias: String = indexWriteAlias, defaultParameters: Map<String, String>? = null): IndexRepository<T>
Link copied to clipboard
suspend fun SearchClient.restoreSnapshot(repositoryName: String, snapshotName: String, waitForCompletion: Boolean = false, timeout: Duration = 1.minutes): JsonObject
Link copied to clipboard

Http GET to /

Link copied to clipboard

Scroll through search results for a scrolling search.

suspend fun SearchClient.scroll(scrollId: String, scroll: Duration = 60.seconds): SearchResponse
Link copied to clipboard
suspend fun SearchClient.search(target: String?, allowNoIndices: Boolean? = null, allowPartialSearchResults: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, batchedReduceSize: Int? = null, ccsMinimizeRoundtrips: Boolean? = null, defaultOperator: SearchOperator? = null, df: String? = null, docvalueFields: String? = null, expandWildcards: ExpandWildCards? = null, explain: Boolean? = null, from: Int? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxConcurrentShardRequests: Int? = null, preFilterShardSize: Int? = null, preference: String? = null, q: String? = null, requestCache: Boolean? = null, restTotalHitsAsInt: Boolean? = null, routing: String? = null, scroll: String? = null, searchType: SearchType? = null, seqNoPrimaryTerm: Boolean? = null, size: Int? = null, sort: String? = null, _source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, stats: String? = null, storedFields: String? = null, suggestField: String? = null, suggestMode: SuggestMode? = null, suggestSize: Int? = null, suggestText: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, trackScores: Boolean? = null, trackTotalHits: Boolean? = null, typedKeys: Boolean? = null, version: Boolean? = null, extraParameters: Map<String, String>? = null, block: SearchDSL.() -> Unit? = null): SearchResponse
suspend fun SearchClient.search(target: String?, dsl: SearchDSL, allowNoIndices: Boolean? = null, allowPartialSearchResults: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, batchedReduceSize: Int? = null, ccsMinimizeRoundtrips: Boolean? = null, defaultOperator: SearchOperator? = null, df: String? = null, docvalueFields: String? = null, expandWildcards: ExpandWildCards? = null, explain: Boolean? = null, from: Int? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxConcurrentShardRequests: Int? = null, preFilterShardSize: Int? = null, preference: String? = null, q: String? = null, requestCache: Boolean? = null, restTotalHitsAsInt: Boolean? = null, routing: String? = null, scroll: String? = null, searchType: SearchType? = null, seqNoPrimaryTerm: Boolean? = null, size: Int? = null, sort: String? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, stats: String? = null, storedFields: String? = null, suggestField: String? = null, suggestMode: SuggestMode? = null, suggestSize: Int? = null, suggestText: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, trackScores: Boolean? = null, trackTotalHits: Boolean? = null, typedKeys: Boolean? = null, version: Boolean? = null, extraParameters: Map<String, String>? = null, retries: Int = 3, retryDelay: Duration = 2.seconds): SearchResponse
suspend fun SearchClient.search(target: String?, rawJson: String?, allowNoIndices: Boolean? = null, allowPartialSearchResults: Boolean? = null, analyzer: String? = null, analyzeWildcard: Boolean? = null, batchedReduceSize: Int? = null, ccsMinimizeRoundtrips: Boolean? = null, defaultOperator: SearchOperator? = null, df: String? = null, docValueFields: String? = null, expandWildcards: ExpandWildCards? = null, explain: Boolean? = null, from: Int? = null, ignoreThrottled: Boolean? = null, ignoreUnavailable: Boolean? = null, lenient: Boolean? = null, maxConcurrentShardRequests: Int? = null, preFilterShardSize: Int? = null, preference: String? = null, q: String? = null, requestCache: Boolean? = null, restTotalHitsAsInt: Boolean? = null, routing: String? = null, scroll: String? = null, searchType: SearchType? = null, seqNoPrimaryTerm: Boolean? = null, size: Int? = null, sort: String? = null, source: String? = null, sourceExcludes: String? = null, sourceIncludes: String? = null, stats: String? = null, storedFields: String? = null, suggestField: String? = null, suggestMode: SuggestMode? = null, suggestSize: Int? = null, suggestText: String? = null, terminateAfter: Int? = null, timeout: Duration? = null, trackScores: Boolean? = null, trackTotalHits: Boolean? = null, typedKeys: Boolean? = null, version: Boolean? = null, extraParameters: Map<String, String>? = null, retries: Int = 3, retryDelay: Duration = 2.seconds): SearchResponse
Link copied to clipboard
suspend fun SearchClient.searchAfter(target: String, keepAlive: Duration = 1.minutes, optInToCustomSort: Boolean = false, block: SearchDSL.() -> Unit? = null): Pair<SearchResponse, Flow<SearchResponse.Hit>>

suspend fun SearchClient.searchAfter(target: String, keepAlive: Duration, query: SearchDSL, optInToCustomSort: Boolean = false, retries: Int = 3, retryDelay: Duration = 2.seconds): Pair<SearchResponse, Flow<SearchResponse.Hit>>

Perform a deep paging search using point in time and search after.

Link copied to clipboard
suspend fun SearchClient.setIlmPolicy(policyId: String, block: IMLPhases.() -> Unit): AcknowledgedResponse
Link copied to clipboard
suspend fun SearchClient.takeSnapshot(repositoryName: String, snapshotName: String = formatTimestamp(), waitForCompletion: Boolean = false, timeout: Duration = 1.minutes): JsonObject
Link copied to clipboard
Link copied to clipboard
suspend fun SearchClient.updateComponentTemplate(templateId: String, block: IndexSettingsAndMappingsDSL.() -> Unit): JsonObject
Link copied to clipboard
suspend fun SearchClient.updateDocument(target: String, id: String, script: Script, upsertJson: String? = null, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, requireAlias: Boolean? = null, retryOnConflict: Int? = null, refresh: Refresh? = null, routing: String? = null, source: String? = null, sourceIncludes: String? = null, sourceExcludes: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, extraParameters: Map<String, String>? = null): DocumentUpdateResponse
suspend fun SearchClient.updateDocument(target: String, id: String, docJson: String, detectNoop: Boolean? = null, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, requireAlias: Boolean? = null, retryOnConflict: Int? = null, refresh: Refresh? = null, routing: String? = null, source: String? = null, sourceIncludes: String? = null, sourceExcludes: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, extraParameters: Map<String, String>? = null): DocumentUpdateResponse
inline suspend fun <T> SearchClient.updateDocument(target: String, id: String, doc: T, json: Json = DEFAULT_JSON, detectNoop: Boolean? = null, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, requireAlias: Boolean? = null, retryOnConflict: Int? = null, refresh: Refresh? = null, routing: String? = null, source: String? = null, sourceIncludes: String? = null, sourceExcludes: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, extraParameters: Map<String, String>? = null): DocumentUpdateResponse
inline suspend fun <T> SearchClient.updateDocument(target: String, id: String, script: Script, upsertJson: T, json: Json = DEFAULT_JSON, ifSeqNo: Int? = null, ifPrimaryTerm: Int? = null, requireAlias: Boolean? = null, retryOnConflict: Int? = null, refresh: Refresh? = null, routing: String? = null, source: String? = null, sourceIncludes: String? = null, sourceExcludes: String? = null, timeout: Duration? = null, waitForActiveShards: String? = null, extraParameters: Map<String, String>? = null): DocumentUpdateResponse
Link copied to clipboard
suspend fun validateEngine(message: String, vararg supportedVariants: SearchEngineVariant)
Link copied to clipboard
suspend fun SearchClient.verifySnapshotRepository(repositoryName: String): JsonObject