Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Remove all SearchResponse direct references from tests #102024

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ public static void assertCheckedResponse(
}
}

public static void assertCheckedResponse(
ActionFuture<SearchResponse> responseFuture,
CheckedConsumer<SearchResponse, IOException> consumer
) throws IOException, ExecutionException, InterruptedException {
var res = responseFuture.get();
try {
consumer.accept(res);
} finally {
res.decRef();
}
}

public static void assertNoFailures(SearchResponse searchResponse) {
assertThat(
"Unexpected ShardFailures: " + Arrays.toString(searchResponse.getShardFailures()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -43,6 +42,8 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertCheckedResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -303,13 +304,16 @@ public void testCategorizationStatePersistedOnSwitchToRealtime() throws Exceptio
// before closing the job to prove that it was persisted in the background at the
// end of lookback rather than when the job was closed.
assertBusy(() -> {
SearchResponse stateDocsResponse = prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()).setQuery(
QueryBuilders.idsQuery().addIds(CategorizerState.documentId(job.getId(), 1))
).get();

SearchHit[] hits = stateDocsResponse.getHits().getHits();
assertThat(hits, arrayWithSize(1));
assertThat(hits[0].getSourceAsMap(), hasKey("compressed"));
assertResponse(
prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()).setQuery(
QueryBuilders.idsQuery().addIds(CategorizerState.documentId(job.getId(), 1))
),
stateDocsResponse -> {
SearchHit[] hits = stateDocsResponse.getHits().getHits();
assertThat(hits, arrayWithSize(1));
assertThat(hits[0].getSourceAsMap(), hasKey("compressed"));
}
);
}, 30, TimeUnit.SECONDS);

stopDatafeed(datafeedId);
Expand Down Expand Up @@ -553,26 +557,28 @@ private static Job.Builder newJobBuilder(String id, List<String> categorizationF
}

private List<CategorizerStats> getCategorizerStats(String jobId) throws IOException {

SearchResponse searchResponse = prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).setQuery(
QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), CategorizerStats.RESULT_TYPE_VALUE))
.filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))
).setSize(1000).get();

List<CategorizerStats> stats = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
try (
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
hit.getSourceRef().streamInput()
)
) {
stats.add(CategorizerStats.LENIENT_PARSER.apply(parser, null).build());
assertCheckedResponse(
prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).setQuery(
QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), CategorizerStats.RESULT_TYPE_VALUE))
.filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))
).setSize(1000),
searchResponse -> {
for (SearchHit hit : searchResponse.getHits().getHits()) {
try (
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
hit.getSourceRef().streamInput()
)
) {
stats.add(CategorizerStats.LENIENT_PARSER.apply(parser, null).build());
}
}
}
}
);
return stats;
}
}
Loading