Search in sources :

Example 16 with GetRequest

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)));
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) ParseUtils.listEqualsWithoutConsideringOrder(org.opensearch.ad.util.ParseUtils.listEqualsWithoutConsideringOrder) IndexResponse(org.opensearch.action.index.IndexResponse) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) FAIL_TO_FIND_DETECTOR_MSG(org.opensearch.ad.constant.CommonErrorMessages.FAIL_TO_FIND_DETECTOR_MSG) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) XContentParser(org.opensearch.common.xcontent.XContentParser) WriteRequest(org.opensearch.action.support.WriteRequest) Locale(java.util.Locale) Map(java.util.Map) ParseUtils.parseAggregators(org.opensearch.ad.util.ParseUtils.parseAggregators) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) GetResponse(org.opensearch.action.get.GetResponse) Client(org.opensearch.client.Client) NumericSetting(org.opensearch.ad.settings.NumericSetting) TimeValue(org.opensearch.common.unit.TimeValue) Feature(org.opensearch.ad.model.Feature) Set(java.util.Set) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) GetFieldMappingsRequest(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Instant(java.time.Instant) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) TransportService(org.opensearch.transport.TransportService) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) GetFieldMappingsAction(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsAction) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) List(java.util.List) Logger(org.apache.logging.log4j.Logger) QueryBuilder(org.opensearch.index.query.QueryBuilder) GetFieldMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) Optional(java.util.Optional) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) RestHandlerUtils(org.opensearch.ad.util.RestHandlerUtils) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) AggregatorFactories(org.opensearch.search.aggregations.AggregatorFactories) IndicesOptions(org.opensearch.action.support.IndicesOptions) ArrayList(java.util.ArrayList) XCONTENT_WITH_TYPE(org.opensearch.ad.util.RestHandlerUtils.XCONTENT_WITH_TYPE) HashSet(java.util.HashSet) IndexAnomalyDetectorResponse(org.opensearch.ad.transport.IndexAnomalyDetectorResponse) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) MergeableList(org.opensearch.ad.model.MergeableList) RestHandlerUtils.isExceptionCausedByInvalidQuery(org.opensearch.ad.util.RestHandlerUtils.isExceptionCausedByInvalidQuery) ValidationAspect(org.opensearch.ad.model.ValidationAspect) ValidateAnomalyDetectorResponse(org.opensearch.ad.transport.ValidateAnomalyDetectorResponse) HISTORICAL_DETECTOR_TASK_TYPES(org.opensearch.ad.model.ADTaskType.HISTORICAL_DETECTOR_TASK_TYPES) QueryBuilders(org.opensearch.index.query.QueryBuilders) RestValidateAnomalyDetectorAction(org.opensearch.ad.rest.RestValidateAnomalyDetectorAction) CommonName(org.opensearch.ad.constant.CommonName) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) RestRequest(org.opensearch.rest.RestRequest) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) User(org.opensearch.commons.authuser.User) DetectorValidationIssueType(org.opensearch.ad.model.DetectorValidationIssueType) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) Clock(java.time.Clock) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) GetRequest(org.opensearch.action.get.GetRequest)

Example 17 with GetRequest

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);
        }
    };
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) Task(org.opensearch.tasks.Task) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) ClusterSettings(org.opensearch.common.settings.ClusterSettings) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) GetRequest(org.opensearch.action.get.GetRequest) ClusterName(org.opensearch.cluster.ClusterName) SearchHits(org.opensearch.search.SearchHits) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ClusterState(org.opensearch.cluster.ClusterState) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Before(org.junit.Before)

Example 18 with GetRequest

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);
}
Also used : ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) GetResponse(org.opensearch.action.get.GetResponse)

Example 19 with GetRequest

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();
            }
        }
    });
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) GetResponse(org.opensearch.action.get.GetResponse) TimeValue(org.opensearch.common.unit.TimeValue)

Example 20 with GetRequest

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);
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) SneakyThrows(lombok.SneakyThrows)

Aggregations

GetRequest (org.opensearch.action.get.GetRequest)81 GetResponse (org.opensearch.action.get.GetResponse)50 ActionListener (org.opensearch.action.ActionListener)48 IOException (java.io.IOException)34 Client (org.opensearch.client.Client)32 IndexRequest (org.opensearch.action.index.IndexRequest)25 XContentParser (org.opensearch.common.xcontent.XContentParser)24 SearchRequest (org.opensearch.action.search.SearchRequest)22 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)21 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)21 LogManager (org.apache.logging.log4j.LogManager)20 Logger (org.apache.logging.log4j.Logger)20 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)19 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)19 List (java.util.List)18 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)17 Optional (java.util.Optional)15 Set (java.util.Set)15 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)15 TransportService (org.opensearch.transport.TransportService)15