use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class AbstractAnomalyDetectorActionHandler method updateAnomalyDetector.
protected void updateAnomalyDetector(String detectorId, boolean indexingDryRun) {
GetRequest request = new GetRequest(ANOMALY_DETECTORS_INDEX, detectorId);
client.get(request, ActionListener.wrap(response -> onGetAnomalyDetectorResponse(response, indexingDryRun, detectorId), exception -> listener.onFailure(exception)));
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorTransportActionTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
clusterService = mock(ClusterService.class);
clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
ClusterName clusterName = new ClusterName("test");
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
final Settings.Builder existingSettings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test2UUID");
IndexMetadata indexMetaData = IndexMetadata.builder(AnomalyDetector.ANOMALY_DETECTORS_INDEX).settings(existingSettings).build();
final ImmutableOpenMap<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder().fPut(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexMetaData).build();
ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().indices(indices).build()).build();
when(clusterService.state()).thenReturn(clusterState);
adTaskManager = mock(ADTaskManager.class);
searchFeatureDao = mock(SearchFeatureDao.class);
action = new IndexAnomalyDetectorTransportAction(mock(TransportService.class), mock(ActionFilters.class), client(), clusterService, indexSettings(), mock(AnomalyDetectionIndices.class), xContentRegistry(), adTaskManager, searchFeatureDao);
task = mock(Task.class);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
GetResponse getDetectorResponse = TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assertTrue(String.format("The size of args is %d. Its content is %s", args.length, Arrays.toString(args)), args.length == 2);
assertTrue(args[0] instanceof GetRequest);
assertTrue(args[1] instanceof ActionListener);
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
listener.onResponse(getDetectorResponse);
return null;
}).when(client).get(any(GetRequest.class), any());
SearchHits hits = new SearchHits(new SearchHit[] {}, null, Float.NaN);
SearchResponseSections searchSections = new SearchResponseSections(hits, null, null, false, false, null, 1);
SearchResponse searchResponse = new SearchResponse(searchSections, null, 1, 1, 0, 30, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assertTrue(String.format("The size of args is %d. Its content is %s", args.length, Arrays.toString(args)), args.length == 2);
assertTrue(args[0] instanceof SearchRequest);
assertTrue(args[1] instanceof ActionListener);
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(searchResponse);
return null;
}).when(client).search(any(SearchRequest.class), any());
request = new IndexAnomalyDetectorRequest("1234", 4567, 7890, WriteRequest.RefreshPolicy.IMMEDIATE, detector, RestRequest.Method.PUT, TimeValue.timeValueSeconds(60), 1000, 10, 5);
response = new ActionListener<IndexAnomalyDetectorResponse>() {
@Override
public void onResponse(IndexAnomalyDetectorResponse indexResponse) {
// onResponse will not be called as we do not have the AD index
Assert.assertTrue(false);
}
@Override
public void onFailure(Exception e) {
Assert.assertTrue(true);
}
};
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class GetAnomalyDetectorTests method testValidRequest.
@SuppressWarnings("unchecked")
public void testValidRequest() throws IOException {
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(null);
}
return null;
}).when(client).get(any(), any());
typeStr = "entity_info,init_progress";
rawPath = "_opendistro/_anomaly_detection/detectors/T4c3dXUBj-2IZN7itix_/_profile";
request = new GetAnomalyDetectorRequest(detectorId, 0L, false, false, typeStr, rawPath, false, entity);
future = new PlainActionFuture<>();
action.doExecute(null, request, future);
assertException(future, OpenSearchStatusException.class, CommonErrorMessages.FAIL_TO_FIND_DETECTOR_MSG);
}
use of org.opensearch.action.get.GetRequest in project asynchronous-search by opensearch-project.
the class TestClientUtils method getResponseFromIndex.
public static void getResponseFromIndex(Client client, String id, CountDownLatch latch, Iterator<TimeValue> backoff) {
client.get(new GetRequest(INDEX).refresh(true).id(id), new ActionListener<GetResponse>() {
@Override
public void onResponse(GetResponse getResponse) {
if (getResponse.isExists()) {
latch.countDown();
} else {
onFailure(new Exception("Get Response doesn't exist."));
}
}
@Override
public void onFailure(Exception e) {
try {
if (backoff.hasNext() == false) {
latch.countDown();
Assert.fail("Failed to persist asynchronous search response");
} else {
TimeValue wait = backoff.next();
Thread.sleep(wait.getMillis());
getResponseFromIndex(client, id, latch, backoff);
}
} catch (InterruptedException ex) {
Assert.fail();
latch.countDown();
}
}
});
}
use of org.opensearch.action.get.GetRequest in project veilarbportefolje by navikt.
the class OpensearchTestClient method fetchDocument.
@SneakyThrows
public GetResponse fetchDocument(AktorId aktoerId) {
GetRequest getRequest = new GetRequest();
getRequest.index(indexName.getValue());
getRequest.id(aktoerId.toString());
return restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
}
Aggregations