Skip to content

Commit 6865fa1

Browse files
committed
Add job Id filter
1 parent 770c334 commit 6865fa1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ private <T> String[] removeReadOnlyIndices(
251251
*/
252252
public void deleteResultsFromTime(long cutoffEpochMs, ActionListener<Boolean> listener) {
253253
QueryBuilder query = QueryBuilders.boolQuery()
254+
.filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))
254255
.filter(
255256
QueryBuilders.termsQuery(
256257
Result.RESULT_TYPE.getPreferredName(),
@@ -262,6 +263,7 @@ public void deleteResultsFromTime(long cutoffEpochMs, ActionListener<Boolean> li
262263
)
263264
)
264265
.filter(QueryBuilders.rangeQuery(Result.TIMESTAMP.getPreferredName()).gte(cutoffEpochMs));
266+
265267
String[] indicesToQuery = removeReadOnlyIndices(
266268
List.of(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)),
267269
listener,

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public void setUpTests() {
5858

5959
@After
6060
public void verifyNoMoreInteractionsWithClient() {
61-
verify(client, times(2)).threadPool();
6261
verifyNoMoreInteractions(client);
6362
}
6463

@@ -85,6 +84,7 @@ public void testDeleteAllAnnotations() {
8584
assertThat(dbqQueryString, containsString("_xpack"));
8685
}
8786
});
87+
verify(client, times(2)).threadPool();
8888
}
8989

9090
public void testDeleteAnnotations_TimestampFiltering() {
@@ -115,6 +115,7 @@ public void testDeleteAnnotations_TimestampFiltering() {
115115
assertThat(dbqQueryString, containsString("_xpack"));
116116
}
117117
});
118+
verify(client, times(2)).threadPool();
118119
}
119120

120121
public void testDeleteAnnotations_EventFiltering() {
@@ -145,6 +146,22 @@ public void testDeleteAnnotations_EventFiltering() {
145146
assertThat(dbqQueryString, containsString("_xpack"));
146147
}
147148
});
149+
verify(client, times(2)).threadPool();
150+
}
151+
152+
public void testDeleteResultsFromTime() {
153+
MockWritableIndexExpander.create(true);
154+
long fromEpochMs = randomNonNegativeLong();
155+
JobDataDeleter jobDataDeleter = new JobDataDeleter(client, JOB_ID, randomBoolean());
156+
jobDataDeleter.deleteResultsFromTime(fromEpochMs, ActionTestUtils.assertNoFailureListener(deleteResponse -> {}));
157+
158+
verify(client).execute(eq(DeleteByQueryAction.INSTANCE), deleteRequestCaptor.capture(), any());
159+
160+
DeleteByQueryRequest deleteRequest = deleteRequestCaptor.getValue();
161+
assertThat(deleteRequest.indices(), is(arrayContaining(".ml-anomalies-my-job-id")));
162+
String dbqQueryString = Strings.toString(deleteRequest.getSearchRequest().source().query());
163+
assertThat(dbqQueryString, containsString("{\"term\":{\"job_id\":{\"value\":\"my-job-id\"}}"));
164+
verify(client, times(1)).threadPool();
148165
}
149166

150167
public void testDeleteDatafeedTimingStats() {
@@ -162,6 +179,7 @@ public void testDeleteDatafeedTimingStats() {
162179
DeleteByQueryRequest deleteRequest = deleteRequestCaptor.getValue();
163180
assertThat(deleteRequest.indices(), is(arrayContaining(AnomalyDetectorsIndex.jobResultsAliasedName(JOB_ID))));
164181
});
182+
verify(client, times(2)).threadPool();
165183
}
166184

167185
public void testDeleteDatafeedTimingStats_WhenIndexReadOnly_ShouldNotDeleteAnything() {
@@ -178,5 +196,6 @@ public void testDeleteDatafeedTimingStats_WhenIndexReadOnly_ShouldNotDeleteAnyth
178196
client.threadPool();
179197
}
180198
});
199+
verify(client, times(2)).threadPool();
181200
}
182201
}

0 commit comments

Comments
 (0)