Skip to content

Commit f47695f

Browse files
committed
Add configurable refresh per operation
- Add refresh parameter to ElasticsearchServiceParams - Create mergeESParamsWithRefresh utility function - Allow per-operation override of global refresh setting - Support false, true, and 'wait_for' refresh modes - Update all write methods to use refresh override
1 parent 6b12896 commit f47695f

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

src/methods/create.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { getDocDescriptor } from '../utils/index'
2-
import { prepareGetParams } from '../utils/params'
2+
import { prepareGetParams, mergeESParamsWithRefresh } from '../utils/params'
33
import { ElasticsearchServiceParams, ElasticAdapterInterface, DocDescriptor, IndexRequest } from '../types'
44
import { get } from './get'
55

6-
function getCreateParams(service: ElasticAdapterInterface, docDescriptor: DocDescriptor): IndexRequest {
6+
function getCreateParams(
7+
service: ElasticAdapterInterface,
8+
docDescriptor: DocDescriptor,
9+
requestParams: ElasticsearchServiceParams = {}
10+
): IndexRequest {
711
let { id, parent, routing, join, doc } = docDescriptor
812

913
if (join) {
@@ -19,25 +23,25 @@ function getCreateParams(service: ElasticAdapterInterface, docDescriptor: DocDes
1923
}
2024

2125
// Build params with required fields
22-
const params: IndexRequest = {
26+
const indexParams: IndexRequest = {
2327
index: service.index || '',
2428
document: doc
2529
}
2630

2731
// Only add id if it's defined
2832
if (id !== undefined) {
29-
params.id = id
33+
indexParams.id = id
3034
}
3135

3236
// Only add routing if it's defined
3337
if (routing !== undefined) {
34-
params.routing = routing
38+
indexParams.routing = routing
3539
}
3640

37-
// Merge esParams but exclude index if it's already set
38-
const cleanEsParams = service.esParams ? { ...service.esParams } : {}
39-
delete (cleanEsParams as Record<string, unknown>).index
40-
return Object.assign(params, cleanEsParams)
41+
// PERFORMANCE: Merge esParams with per-operation refresh override
42+
const cleanEsParams = mergeESParamsWithRefresh(service.esParams, requestParams)
43+
delete cleanEsParams.index
44+
return Object.assign(indexParams, cleanEsParams)
4145
}
4246

4347
export function create(
@@ -47,7 +51,7 @@ export function create(
4751
) {
4852
const docDescriptor = getDocDescriptor(service, data)
4953
const { id, routing } = docDescriptor
50-
const createParams = getCreateParams(service, docDescriptor)
54+
const createParams = getCreateParams(service, docDescriptor, params)
5155
const getParams = prepareGetParams(params, 'upsert')
5256

5357
// If we have routing (parent document), pass it in the query for the get operation

src/methods/patch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
import { getDocDescriptor, getQueryLength, mapPatch } from '../utils/index'
4+
import { mergeESParamsWithRefresh } from '../utils/params'
45
import { ElasticsearchServiceParams, ElasticAdapterInterface } from '../types'
56

67
export function patch(
@@ -13,12 +14,13 @@ export function patch(
1314
const { routing } = getDocDescriptor(service, query)
1415
const { doc } = getDocDescriptor(service, data)
1516

17+
// PERFORMANCE: Merge esParams with per-operation refresh override
1618
const updateParams: Record<string, unknown> = {
1719
index: filters.$index || service.index,
1820
id: String(id),
1921
body: { doc },
2022
_source: filters.$select || true,
21-
...service.esParams
23+
...mergeESParamsWithRefresh(service.esParams, params)
2224
}
2325

2426
// Add routing if specified

src/methods/remove.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
import { getDocDescriptor } from '../utils/index'
4+
import { mergeESParamsWithRefresh } from '../utils/params'
45
import { ElasticsearchServiceParams, ElasticAdapterInterface } from '../types'
56

67
export function remove(
@@ -10,12 +11,14 @@ export function remove(
1011
) {
1112
const { filters, query } = service.filterQuery(params)
1213
const { routing } = getDocDescriptor(service, query)
14+
15+
// PERFORMANCE: Merge esParams with per-operation refresh override
1316
const removeParams = Object.assign(
1417
{
1518
index: filters.$index || service.index,
1619
id: String(id)
1720
},
18-
service.esParams
21+
mergeESParamsWithRefresh(service.esParams, params)
1922
)
2023

2124
if (routing !== undefined) {

src/methods/update.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import { removeProps, getDocDescriptor } from '../utils/index'
2-
import { prepareGetParams } from '../utils/params'
2+
import { prepareGetParams, mergeESParamsWithRefresh } from '../utils/params'
33
import { ElasticsearchServiceParams, ElasticAdapterInterface, DocDescriptor } from '../types'
44

55
function getUpdateParams(
66
service: ElasticAdapterInterface,
77
docDescriptor: DocDescriptor,
8-
filters: Record<string, unknown>
8+
filters: Record<string, unknown>,
9+
params: ElasticsearchServiceParams = {}
910
) {
1011
const { id, routing, doc } = docDescriptor
1112

12-
const params: Record<string, unknown> = {
13+
const updateParams: Record<string, unknown> = {
1314
index: filters.$index || service.index,
1415
id: String(id),
1516
body: doc
1617
}
1718

1819
if (routing !== undefined) {
19-
params.routing = routing
20+
updateParams.routing = routing
2021
}
2122

22-
// Merge esParams but exclude index if it's already set
23-
const cleanEsParams = service.esParams ? { ...service.esParams } : {}
23+
// PERFORMANCE: Merge esParams with per-operation refresh override
24+
const cleanEsParams = mergeESParamsWithRefresh(service.esParams, params)
2425
delete cleanEsParams.index
25-
return Object.assign(params, cleanEsParams)
26+
return Object.assign(updateParams, cleanEsParams)
2627
}
2728

2829
export function update(
@@ -35,7 +36,7 @@ export function update(
3536
const docDescriptor = getDocDescriptor(service, data, query, {
3637
[service.id]: id
3738
})
38-
const updateParams = getUpdateParams(service, docDescriptor, filters)
39+
const updateParams = getUpdateParams(service, docDescriptor, filters, params)
3940

4041
if (params.upsert) {
4142
return service.Model.index(updateParams as never).then((result: unknown) =>

src/utils/params.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,24 @@ export function prepareRoutingParams(
5858
}
5959
return params
6060
}
61+
62+
/**
63+
* Merges ES params with per-operation overrides for refresh control
64+
* PERFORMANCE: Allows configurable refresh per operation instead of global setting
65+
* @param serviceEsParams - Service-level ES parameters
66+
* @param operationParams - Operation-specific parameters from request
67+
* @returns Merged parameters with refresh override if specified
68+
*/
69+
export function mergeESParamsWithRefresh(
70+
serviceEsParams: Record<string, unknown> = {},
71+
operationParams: ElasticsearchServiceParams = {}
72+
): Record<string, unknown> {
73+
const merged = { ...serviceEsParams }
74+
75+
// Allow per-operation refresh override
76+
if (operationParams.refresh !== undefined) {
77+
merged.refresh = operationParams.refresh
78+
}
79+
80+
return merged
81+
}

0 commit comments

Comments
 (0)