Skip to content

Commit 56cb430

Browse files
[ML] Replace use of rest_total_hits_as_int with track_total_hits (#78423) (#78909)
* [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 06dfb08 commit 56cb430

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
@@ -297,11 +297,10 @@ export function getTestUrl(job, customUrl) {
297297
return new Promise((resolve, reject) => {
298298
ml.results
299299
.anomalySearch({
300-
rest_total_hits_as_int: true,
301300
body,
302301
})
303302
.then((resp) => {
304-
if (resp.hits.total > 0) {
303+
if (resp.hits.total.value > 0) {
305304
const record = resp.hits.hits[0]._source;
306305
testUrl = replaceTokensInUrlValue(customUrl, bucketSpanSecs, record, 'timestamp');
307306
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
@@ -52,7 +52,6 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
5252
ml.results
5353
.anomalySearch({
5454
size: maxResults,
55-
rest_total_hits_as_int: true,
5655
body: {
5756
query: {
5857
bool: {
@@ -63,7 +62,7 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
6362
},
6463
})
6564
.then((resp) => {
66-
if (resp.hits.total !== 0) {
65+
if (resp.hits.total.value > 0) {
6766
obj.forecasts = resp.hits.hits.map((hit) => hit._source);
6867
}
6968

@@ -346,7 +345,6 @@ function getForecastRequestStats(job, forecastId) {
346345
ml.results
347346
.anomalySearch({
348347
size: 1,
349-
rest_total_hits_as_int: true,
350348
body: {
351349
query: {
352350
bool: {
@@ -356,7 +354,7 @@ function getForecastRequestStats(job, forecastId) {
356354
},
357355
})
358356
.then((resp) => {
359-
if (resp.hits.total !== 0) {
357+
if (resp.hits.total.value > 0) {
360358
obj.stats = resp.hits.hits[0]._source;
361359
}
362360
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
@@ -403,7 +403,6 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
403403
return mlApiServices.results
404404
.anomalySearch$({
405405
index: ML_RESULTS_INDEX_PATTERN,
406-
rest_total_hits_as_int: true,
407406
size: maxResults !== undefined ? maxResults : 100,
408407
body: {
409408
query: {
@@ -428,7 +427,7 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
428427
})
429428
.pipe(
430429
map((resp) => {
431-
if (resp.hits.total !== 0) {
430+
if (resp.hits.total.value > 0) {
432431
each(resp.hits.hits, (hit: any) => {
433432
obj.records.push(hit._source);
434433
});

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
@@ -726,7 +726,6 @@ export function resultsServiceProvider(mlApiServices) {
726726
mlApiServices.results
727727
.anomalySearch({
728728
size: maxResults !== undefined ? maxResults : 100,
729-
rest_total_hits_as_int: true,
730729
body: {
731730
_source: ['job_id', 'detector_index', 'influencers', 'record_score'],
732731
query: {
@@ -750,7 +749,7 @@ export function resultsServiceProvider(mlApiServices) {
750749
},
751750
})
752751
.then((resp) => {
753-
if (resp.hits.total !== 0) {
752+
if (resp.hits.total.value > 0) {
754753
each(resp.hits.hits, (hit) => {
755754
obj.records.push(hit._source);
756755
});
@@ -858,7 +857,6 @@ export function resultsServiceProvider(mlApiServices) {
858857
mlApiServices.results
859858
.anomalySearch({
860859
size: maxResults !== undefined ? maxResults : 100,
861-
rest_total_hits_as_int: true,
862860
body: {
863861
query: {
864862
bool: {
@@ -881,7 +879,7 @@ export function resultsServiceProvider(mlApiServices) {
881879
},
882880
})
883881
.then((resp) => {
884-
if (resp.hits.total !== 0) {
882+
if (resp.hits.total.value > 0) {
885883
each(resp.hits.hits, (hit) => {
886884
obj.records.push(hit._source);
887885
});
@@ -983,7 +981,6 @@ export function resultsServiceProvider(mlApiServices) {
983981
mlApiServices.results
984982
.anomalySearch({
985983
size: maxResults !== undefined ? maxResults : 100,
986-
rest_total_hits_as_int: true,
987984
body: {
988985
query: {
989986
bool: {
@@ -1006,7 +1003,7 @@ export function resultsServiceProvider(mlApiServices) {
10061003
},
10071004
})
10081005
.then((resp) => {
1009-
if (resp.hits.total !== 0) {
1006+
if (resp.hits.total.value > 0) {
10101007
each(resp.hits.hits, (hit) => {
10111008
obj.records.push(hit._source);
10121009
});
@@ -1059,7 +1056,6 @@ export function resultsServiceProvider(mlApiServices) {
10591056
mlApiServices
10601057
.esSearch({
10611058
index,
1062-
rest_total_hits_as_int: true,
10631059
size: 0,
10641060
body: {
10651061
query: {
@@ -1091,7 +1087,7 @@ export function resultsServiceProvider(mlApiServices) {
10911087
const time = dataForTime.key;
10921088
obj.results[time] = dataForTime.doc_count;
10931089
});
1094-
obj.total = resp.hits.total;
1090+
obj.total = resp.hits.total.value;
10951091

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

12401236
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
@@ -630,12 +630,12 @@ export class DataVisualizer {
630630

631631
const { body } = await this._asCurrentUser.search({
632632
index,
633-
rest_total_hits_as_int: true,
633+
track_total_hits: true,
634634
size,
635635
body: searchBody,
636636
});
637637
const aggregations = body.aggregations;
638-
const totalCount = get(body, ['hits', 'total'], 0);
638+
const totalCount = body.hits.total.value;
639639
const stats = {
640640
totalCount,
641641
aggregatableExistsFields: [] as FieldData[],
@@ -697,11 +697,10 @@ export class DataVisualizer {
697697

698698
const { body } = await this._asCurrentUser.search({
699699
index,
700-
rest_total_hits_as_int: true,
701700
size,
702701
body: searchBody,
703702
});
704-
return body.hits.total > 0;
703+
return body.hits.total.value > 0;
705704
}
706705

707706
async getDocumentCountStats(
@@ -1167,15 +1166,14 @@ export class DataVisualizer {
11671166

11681167
const { body } = await this._asCurrentUser.search({
11691168
index,
1170-
rest_total_hits_as_int: true,
11711169
size,
11721170
body: searchBody,
11731171
});
11741172
const stats = {
11751173
fieldName: field,
11761174
examples: [] as any[],
11771175
};
1178-
if (body.hits.total !== 0) {
1176+
if (body.hits.total.value > 0) {
11791177
const hits = body.hits.hits;
11801178
for (let i = 0; i < hits.length; i++) {
11811179
// 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)