Skip to content

Commit 80c004c

Browse files
[ML] Replace use of rest_total_hits_as_int with track_total_hits (#78423)
* [ML] Replace use of rest_total_hits_as_int with track_total_hits * [ML] Replace use of lodash get for obtaining hits total * [ML] Update mocks to use new total hits format
1 parent 8e523ed commit 80c004c

File tree

17 files changed

+37
-52
lines changed

17 files changed

+37
-52
lines changed

x-pack/plugins/ml/public/application/jobs/components/custom_url_editor/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,10 @@ export function getTestUrl(job, customUrl) {
296296
return new Promise((resolve, reject) => {
297297
ml.results
298298
.anomalySearch({
299-
rest_total_hits_as_int: true,
300299
body,
301300
})
302301
.then((resp) => {
303-
if (resp.hits.total > 0) {
302+
if (resp.hits.total.value > 0) {
304303
const record = resp.hits.hits[0]._source;
305304
testUrl = replaceTokensInUrlValue(customUrl, bucketSpanSecs, record, 'timestamp');
306305
resolve(testUrl);

x-pack/plugins/ml/public/application/services/forecast_service.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
5050
ml.results
5151
.anomalySearch({
5252
size: maxResults,
53-
rest_total_hits_as_int: true,
5453
body: {
5554
query: {
5655
bool: {
@@ -61,7 +60,7 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
6160
},
6261
})
6362
.then((resp) => {
64-
if (resp.hits.total !== 0) {
63+
if (resp.hits.total.value > 0) {
6564
obj.forecasts = resp.hits.hits.map((hit) => hit._source);
6665
}
6766

@@ -344,7 +343,6 @@ function getForecastRequestStats(job, forecastId) {
344343
ml.results
345344
.anomalySearch({
346345
size: 1,
347-
rest_total_hits_as_int: true,
348346
body: {
349347
query: {
350348
bool: {
@@ -354,7 +352,7 @@ function getForecastRequestStats(job, forecastId) {
354352
},
355353
})
356354
.then((resp) => {
357-
if (resp.hits.total !== 0) {
355+
if (resp.hits.total.value > 0) {
358356
obj.stats = resp.hits.hits[0]._source;
359357
}
360358
resolve(obj);

x-pack/plugins/ml/public/application/services/results_service/result_service_rx.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
402402
return mlApiServices.results
403403
.anomalySearch$({
404404
index: ML_RESULTS_INDEX_PATTERN,
405-
rest_total_hits_as_int: true,
406405
size: maxResults !== undefined ? maxResults : 100,
407406
body: {
408407
query: {
@@ -427,7 +426,7 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
427426
})
428427
.pipe(
429428
map((resp) => {
430-
if (resp.hits.total !== 0) {
429+
if (resp.hits.total.value > 0) {
431430
each(resp.hits.hits, (hit: any) => {
432431
obj.records.push(hit._source);
433432
});

x-pack/plugins/ml/public/application/services/results_service/results_service.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,6 @@ export function resultsServiceProvider(mlApiServices) {
725725
mlApiServices.results
726726
.anomalySearch({
727727
size: maxResults !== undefined ? maxResults : 100,
728-
rest_total_hits_as_int: true,
729728
body: {
730729
_source: ['job_id', 'detector_index', 'influencers', 'record_score'],
731730
query: {
@@ -749,7 +748,7 @@ export function resultsServiceProvider(mlApiServices) {
749748
},
750749
})
751750
.then((resp) => {
752-
if (resp.hits.total !== 0) {
751+
if (resp.hits.total.value > 0) {
753752
each(resp.hits.hits, (hit) => {
754753
obj.records.push(hit._source);
755754
});
@@ -857,7 +856,6 @@ export function resultsServiceProvider(mlApiServices) {
857856
mlApiServices.results
858857
.anomalySearch({
859858
size: maxResults !== undefined ? maxResults : 100,
860-
rest_total_hits_as_int: true,
861859
body: {
862860
query: {
863861
bool: {
@@ -880,7 +878,7 @@ export function resultsServiceProvider(mlApiServices) {
880878
},
881879
})
882880
.then((resp) => {
883-
if (resp.hits.total !== 0) {
881+
if (resp.hits.total.value > 0) {
884882
each(resp.hits.hits, (hit) => {
885883
obj.records.push(hit._source);
886884
});
@@ -982,7 +980,6 @@ export function resultsServiceProvider(mlApiServices) {
982980
mlApiServices.results
983981
.anomalySearch({
984982
size: maxResults !== undefined ? maxResults : 100,
985-
rest_total_hits_as_int: true,
986983
body: {
987984
query: {
988985
bool: {
@@ -1005,7 +1002,7 @@ export function resultsServiceProvider(mlApiServices) {
10051002
},
10061003
})
10071004
.then((resp) => {
1008-
if (resp.hits.total !== 0) {
1005+
if (resp.hits.total.value > 0) {
10091006
each(resp.hits.hits, (hit) => {
10101007
obj.records.push(hit._source);
10111008
});
@@ -1058,7 +1055,6 @@ export function resultsServiceProvider(mlApiServices) {
10581055
mlApiServices
10591056
.esSearch({
10601057
index,
1061-
rest_total_hits_as_int: true,
10621058
size: 0,
10631059
body: {
10641060
query: {
@@ -1090,7 +1086,7 @@ export function resultsServiceProvider(mlApiServices) {
10901086
const time = dataForTime.key;
10911087
obj.results[time] = dataForTime.doc_count;
10921088
});
1093-
obj.total = resp.hits.total;
1089+
obj.total = resp.hits.total.value;
10941090

10951091
resolve(obj);
10961092
})
@@ -1227,13 +1223,13 @@ export function resultsServiceProvider(mlApiServices) {
12271223
.esSearch({
12281224
index,
12291225
body,
1230-
rest_total_hits_as_int: true,
1226+
track_total_hits: true,
12311227
})
12321228
.then((resp) => {
12331229
// Because of the sampling, results of metricFunctions which use sum or count
12341230
// can be significantly skewed. Taking into account totalHits we calculate a
12351231
// a factor to normalize results for these metricFunctions.
1236-
const totalHits = get(resp, ['hits', 'total'], 0);
1232+
const totalHits = resp.hits.total.value;
12371233
const successfulShards = get(resp, ['_shards', 'successful'], 0);
12381234

12391235
let normalizeFactor = 1;

x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export function analyticsAuditMessagesProvider({ asInternalUser }: IScopedCluste
7171
const { body } = await asInternalUser.search({
7272
index: ML_NOTIFICATION_INDEX_PATTERN,
7373
ignore_unavailable: true,
74-
rest_total_hits_as_int: true,
7574
size: SIZE,
7675
body: {
7776
sort: [{ timestamp: { order: 'desc' } }, { job_id: { order: 'asc' } }],
@@ -80,7 +79,7 @@ export function analyticsAuditMessagesProvider({ asInternalUser }: IScopedCluste
8079
});
8180

8281
let messages = [];
83-
if (body.hits.total !== 0) {
82+
if (body.hits.total.value > 0) {
8483
messages = body.hits.hits.map((hit: Message) => hit._source);
8584
messages.reverse();
8685
}

x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,11 @@ export class DataRecognizer {
248248

249249
const { body } = await this._asCurrentUser.search({
250250
index,
251-
rest_total_hits_as_int: true,
252251
size,
253252
body: searchBody,
254253
});
255254

256-
return body.hits.total !== 0;
255+
return body.hits.total.value > 0;
257256
}
258257

259258
async listModules() {

x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,12 @@ export class DataVisualizer {
627627

628628
const { body } = await this._asCurrentUser.search({
629629
index,
630-
rest_total_hits_as_int: true,
630+
track_total_hits: true,
631631
size,
632632
body: searchBody,
633633
});
634634
const aggregations = body.aggregations;
635-
const totalCount = get(body, ['hits', 'total'], 0);
635+
const totalCount = body.hits.total.value;
636636
const stats = {
637637
totalCount,
638638
aggregatableExistsFields: [] as FieldData[],
@@ -694,11 +694,10 @@ export class DataVisualizer {
694694

695695
const { body } = await this._asCurrentUser.search({
696696
index,
697-
rest_total_hits_as_int: true,
698697
size,
699698
body: searchBody,
700699
});
701-
return body.hits.total > 0;
700+
return body.hits.total.value > 0;
702701
}
703702

704703
async getDocumentCountStats(
@@ -1164,15 +1163,14 @@ export class DataVisualizer {
11641163

11651164
const { body } = await this._asCurrentUser.search({
11661165
index,
1167-
rest_total_hits_as_int: true,
11681166
size,
11691167
body: searchBody,
11701168
});
11711169
const stats = {
11721170
fieldName: field,
11731171
examples: [] as any[],
11741172
};
1175-
if (body.hits.total !== 0) {
1173+
if (body.hits.total.value > 0) {
11761174
const hits = body.hits.hits;
11771175
for (let i = 0; i < hits.length; i++) {
11781176
// Look in the _source for the field value.

x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
102102
const { body } = await asInternalUser.search({
103103
index: ML_NOTIFICATION_INDEX_PATTERN,
104104
ignore_unavailable: true,
105-
rest_total_hits_as_int: true,
106105
size: SIZE,
107106
body: {
108107
sort: [{ timestamp: { order: 'desc' } }, { job_id: { order: 'asc' } }],
@@ -111,7 +110,7 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
111110
});
112111

113112
let messages = [];
114-
if (body.hits.total !== 0) {
113+
if (body.hits.total.value > 0) {
115114
messages = body.hits.hits.map((hit) => hit._source);
116115
}
117116
return messages;
@@ -153,7 +152,6 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
153152
const { body } = await asInternalUser.search({
154153
index: ML_NOTIFICATION_INDEX_PATTERN,
155154
ignore_unavailable: true,
156-
rest_total_hits_as_int: true,
157155
size: 0,
158156
body: {
159157
query,
@@ -196,7 +194,7 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
196194
let messagesPerJob = [];
197195
const jobMessages = [];
198196
if (
199-
body.hits.total !== 0 &&
197+
body.hits.total.value > 0 &&
200198
body.aggregations &&
201199
body.aggregations.levelsPerJob &&
202200
body.aggregations.levelsPerJob.buckets &&

x-pack/plugins/ml/server/models/job_service/new_job/line_chart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function processSearchResults(resp: any, fields: string[]): ProcessedResults {
8989
return {
9090
success: true,
9191
results: tempResults,
92-
totalResults: resp.hits.total,
92+
totalResults: resp.hits.total.value,
9393
};
9494
}
9595

@@ -107,7 +107,7 @@ function getSearchJsonFromConfig(
107107
const json = {
108108
index: indexPatternTitle,
109109
size: 0,
110-
rest_total_hits_as_int: true,
110+
track_total_hits: true,
111111
body: {
112112
query: {},
113113
aggs: {

x-pack/plugins/ml/server/models/job_service/new_job/population_chart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function processSearchResults(resp: any, fields: string[]): ProcessedResults {
118118
return {
119119
success: true,
120120
results: tempResults,
121-
totalResults: resp.hits.total,
121+
totalResults: resp.hits.total.value,
122122
};
123123
}
124124

@@ -135,7 +135,7 @@ function getPopulationSearchJsonFromConfig(
135135
const json = {
136136
index: indexPatternTitle,
137137
size: 0,
138-
rest_total_hits_as_int: true,
138+
track_total_hits: true,
139139
body: {
140140
query: {},
141141
aggs: {

0 commit comments

Comments
 (0)