Skip to content

Commit

Permalink
[ML] Replace use of rest_total_hits_as_int with track_total_hits (ela…
Browse files Browse the repository at this point in the history
…stic#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
  • Loading branch information
peteharverson committed Sep 30, 2020
1 parent 9d9344e commit 686f7e0
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,10 @@ export function getTestUrl(job, customUrl) {
return new Promise((resolve, reject) => {
ml.results
.anomalySearch({
rest_total_hits_as_int: true,
body,
})
.then((resp) => {
if (resp.hits.total > 0) {
if (resp.hits.total.value > 0) {
const record = resp.hits.hits[0]._source;
testUrl = replaceTokensInUrlValue(customUrl, bucketSpanSecs, record, 'timestamp');
resolve(testUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
ml.results
.anomalySearch({
size: maxResults,
rest_total_hits_as_int: true,
body: {
query: {
bool: {
Expand All @@ -63,7 +62,7 @@ function getForecastsSummary(job, query, earliestMs, maxResults) {
},
})
.then((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
obj.forecasts = resp.hits.hits.map((hit) => hit._source);
}

Expand Down Expand Up @@ -346,7 +345,6 @@ function getForecastRequestStats(job, forecastId) {
ml.results
.anomalySearch({
size: 1,
rest_total_hits_as_int: true,
body: {
query: {
bool: {
Expand All @@ -356,7 +354,7 @@ function getForecastRequestStats(job, forecastId) {
},
})
.then((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
obj.stats = resp.hits.hits[0]._source;
}
resolve(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
return mlApiServices.results
.anomalySearch$({
index: ML_RESULTS_INDEX_PATTERN,
rest_total_hits_as_int: true,
size: maxResults !== undefined ? maxResults : 100,
body: {
query: {
Expand All @@ -428,7 +427,7 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
})
.pipe(
map((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
each(resp.hits.hits, (hit: any) => {
obj.records.push(hit._source);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ export function resultsServiceProvider(mlApiServices) {
mlApiServices.results
.anomalySearch({
size: maxResults !== undefined ? maxResults : 100,
rest_total_hits_as_int: true,
body: {
_source: ['job_id', 'detector_index', 'influencers', 'record_score'],
query: {
Expand All @@ -750,7 +749,7 @@ export function resultsServiceProvider(mlApiServices) {
},
})
.then((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
each(resp.hits.hits, (hit) => {
obj.records.push(hit._source);
});
Expand Down Expand Up @@ -858,7 +857,6 @@ export function resultsServiceProvider(mlApiServices) {
mlApiServices.results
.anomalySearch({
size: maxResults !== undefined ? maxResults : 100,
rest_total_hits_as_int: true,
body: {
query: {
bool: {
Expand All @@ -881,7 +879,7 @@ export function resultsServiceProvider(mlApiServices) {
},
})
.then((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
each(resp.hits.hits, (hit) => {
obj.records.push(hit._source);
});
Expand Down Expand Up @@ -983,7 +981,6 @@ export function resultsServiceProvider(mlApiServices) {
mlApiServices.results
.anomalySearch({
size: maxResults !== undefined ? maxResults : 100,
rest_total_hits_as_int: true,
body: {
query: {
bool: {
Expand All @@ -1006,7 +1003,7 @@ export function resultsServiceProvider(mlApiServices) {
},
})
.then((resp) => {
if (resp.hits.total !== 0) {
if (resp.hits.total.value > 0) {
each(resp.hits.hits, (hit) => {
obj.records.push(hit._source);
});
Expand Down Expand Up @@ -1059,7 +1056,6 @@ export function resultsServiceProvider(mlApiServices) {
mlApiServices
.esSearch({
index,
rest_total_hits_as_int: true,
size: 0,
body: {
query: {
Expand Down Expand Up @@ -1091,7 +1087,7 @@ export function resultsServiceProvider(mlApiServices) {
const time = dataForTime.key;
obj.results[time] = dataForTime.doc_count;
});
obj.total = resp.hits.total;
obj.total = resp.hits.total.value;

resolve(obj);
})
Expand Down Expand Up @@ -1228,13 +1224,13 @@ export function resultsServiceProvider(mlApiServices) {
.esSearch({
index,
body,
rest_total_hits_as_int: true,
track_total_hits: true,
})
.then((resp) => {
// Because of the sampling, results of metricFunctions which use sum or count
// can be significantly skewed. Taking into account totalHits we calculate a
// a factor to normalize results for these metricFunctions.
const totalHits = get(resp, ['hits', 'total'], 0);
const totalHits = resp.hits.total.value;
const successfulShards = get(resp, ['_shards', 'successful'], 0);

let normalizeFactor = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export function analyticsAuditMessagesProvider({ asInternalUser }: IScopedCluste
const { body } = await asInternalUser.search({
index: ML_NOTIFICATION_INDEX_PATTERN,
ignore_unavailable: true,
rest_total_hits_as_int: true,
size: SIZE,
body: {
sort: [{ timestamp: { order: 'desc' } }, { job_id: { order: 'asc' } }],
Expand All @@ -80,7 +79,7 @@ export function analyticsAuditMessagesProvider({ asInternalUser }: IScopedCluste
});

let messages = [];
if (body.hits.total !== 0) {
if (body.hits.total.value > 0) {
messages = body.hits.hits.map((hit: Message) => hit._source);
messages.reverse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,11 @@ export class DataRecognizer {

const { body } = await this._asCurrentUser.search({
index,
rest_total_hits_as_int: true,
size,
body: searchBody,
});

return body.hits.total !== 0;
return body.hits.total.value > 0;
}

async listModules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,12 @@ export class DataVisualizer {

const { body } = await this._asCurrentUser.search({
index,
rest_total_hits_as_int: true,
track_total_hits: true,
size,
body: searchBody,
});
const aggregations = body.aggregations;
const totalCount = get(body, ['hits', 'total'], 0);
const totalCount = body.hits.total.value;
const stats = {
totalCount,
aggregatableExistsFields: [] as FieldData[],
Expand Down Expand Up @@ -697,11 +697,10 @@ export class DataVisualizer {

const { body } = await this._asCurrentUser.search({
index,
rest_total_hits_as_int: true,
size,
body: searchBody,
});
return body.hits.total > 0;
return body.hits.total.value > 0;
}

async getDocumentCountStats(
Expand Down Expand Up @@ -1167,15 +1166,14 @@ export class DataVisualizer {

const { body } = await this._asCurrentUser.search({
index,
rest_total_hits_as_int: true,
size,
body: searchBody,
});
const stats = {
fieldName: field,
examples: [] as any[],
};
if (body.hits.total !== 0) {
if (body.hits.total.value > 0) {
const hits = body.hits.hits;
for (let i = 0; i < hits.length; i++) {
// Look in the _source for the field value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
const { body } = await asInternalUser.search({
index: ML_NOTIFICATION_INDEX_PATTERN,
ignore_unavailable: true,
rest_total_hits_as_int: true,
size: SIZE,
body: {
sort: [{ timestamp: { order: 'desc' } }, { job_id: { order: 'asc' } }],
Expand All @@ -111,7 +110,7 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
});

let messages = [];
if (body.hits.total !== 0) {
if (body.hits.total.value > 0) {
messages = body.hits.hits.map((hit) => hit._source);
}
return messages;
Expand Down Expand Up @@ -153,7 +152,6 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
const { body } = await asInternalUser.search({
index: ML_NOTIFICATION_INDEX_PATTERN,
ignore_unavailable: true,
rest_total_hits_as_int: true,
size: 0,
body: {
query,
Expand Down Expand Up @@ -196,7 +194,7 @@ export function jobAuditMessagesProvider({ asInternalUser }) {
let messagesPerJob = [];
const jobMessages = [];
if (
body.hits.total !== 0 &&
body.hits.total.value > 0 &&
body.aggregations &&
body.aggregations.levelsPerJob &&
body.aggregations.levelsPerJob.buckets &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function processSearchResults(resp: any, fields: string[]): ProcessedResults {
return {
success: true,
results: tempResults,
totalResults: resp.hits.total,
totalResults: resp.hits.total.value,
};
}

Expand All @@ -107,7 +107,7 @@ function getSearchJsonFromConfig(
const json = {
index: indexPatternTitle,
size: 0,
rest_total_hits_as_int: true,
track_total_hits: true,
body: {
query: {},
aggs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function processSearchResults(resp: any, fields: string[]): ProcessedResults {
return {
success: true,
results: tempResults,
totalResults: resp.hits.total,
totalResults: resp.hits.total.value,
};
}

Expand All @@ -135,7 +135,7 @@ function getPopulationSearchJsonFromConfig(
const json = {
index: indexPatternTitle,
size: 0,
rest_total_hits_as_int: true,
track_total_hits: true,
body: {
query: {},
aggs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"took": 0,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits": { "total": 86274, "max_score": 0, "hits": [] },
"aggregations": { "airline_cardinality": { "value": 19 }, "airline_count": { "doc_count": 86274 } }
"hits": { "total": { "value": 86274, "relation": "eq" }, "max_score": 0, "hits": [] },
"aggregations": {
"airline_cardinality": { "value": 19 },
"airline_count": { "doc_count": 86274 }
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 686f7e0

Please sign in to comment.