This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from opendistro-for-elasticsearch/fgac-search…
…-apis Adding RestActions support for AD Search Rest API's
- Loading branch information
Showing
10 changed files
with
338 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
|
||
public class SearchAnomalyDetectorAction extends ActionType<SearchResponse> { | ||
public static final SearchAnomalyDetectorAction INSTANCE = new SearchAnomalyDetectorAction(); | ||
public static final String NAME = "cluster:admin/opendistro/ad/detector/search"; | ||
|
||
private SearchAnomalyDetectorAction() { | ||
super(NAME, SearchResponse::new); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorTransportAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
public class SearchAnomalyDetectorTransportAction extends HandledTransportAction<SearchRequest, SearchResponse> { | ||
|
||
private final Client client; | ||
|
||
@Inject | ||
public SearchAnomalyDetectorTransportAction(TransportService transportService, ActionFilters actionFilters, Client client) { | ||
super(SearchAnomalyDetectorAction.NAME, transportService, actionFilters, SearchRequest::new); | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) { | ||
client.search(request, new ActionListener<SearchResponse>() { | ||
@Override | ||
public void onResponse(SearchResponse searchResponse) { | ||
listener.onResponse(searchResponse); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
listener.onFailure(e); | ||
} | ||
}); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...in/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
|
||
public class SearchAnomalyResultAction extends ActionType<SearchResponse> { | ||
public static final SearchAnomalyResultAction INSTANCE = new SearchAnomalyResultAction(); | ||
public static final String NAME = "cluster:admin/opendistro/ad/result/search"; | ||
|
||
private SearchAnomalyResultAction() { | ||
super(NAME, SearchResponse::new); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...om/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultTransportAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
public class SearchAnomalyResultTransportAction extends HandledTransportAction<SearchRequest, SearchResponse> { | ||
|
||
private final Client client; | ||
|
||
@Inject | ||
public SearchAnomalyResultTransportAction(TransportService transportService, ActionFilters actionFilters, Client client) { | ||
super(SearchAnomalyResultAction.NAME, transportService, actionFilters, SearchRequest::new); | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) { | ||
client.search(request, new ActionListener<SearchResponse>() { | ||
@Override | ||
public void onResponse(SearchResponse searchResponse) { | ||
listener.onResponse(searchResponse); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
listener.onFailure(e); | ||
} | ||
}); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
.../com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorActionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import org.apache.lucene.index.IndexNotFoundException; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class SearchAnomalyDetectorActionTests extends ESIntegTestCase { | ||
private SearchAnomalyDetectorTransportAction action; | ||
private Task task; | ||
private ActionListener<SearchResponse> response; | ||
|
||
@Override | ||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
action = new SearchAnomalyDetectorTransportAction(mock(TransportService.class), mock(ActionFilters.class), client()); | ||
task = mock(Task.class); | ||
response = new ActionListener<SearchResponse>() { | ||
@Override | ||
public void onResponse(SearchResponse searchResponse) { | ||
Assert.assertEquals(searchResponse.getSuccessfulShards(), 5); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
Assert.assertFalse(IndexNotFoundException.class == e.getClass()); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testSearchResponse() { | ||
// Will call response.onResponse as Index exists | ||
Settings indexSettings = Settings.builder().put("number_of_shards", 5).put("number_of_replicas", 1).build(); | ||
CreateIndexRequest indexRequest = new CreateIndexRequest("my-test-index", indexSettings); | ||
client().admin().indices().create(indexRequest).actionGet(); | ||
SearchRequest searchRequest = new SearchRequest("my-test-index"); | ||
action.doExecute(task, searchRequest, response); | ||
} | ||
|
||
@Test | ||
public void testSearchDetectorAction() { | ||
Assert.assertNotNull(SearchAnomalyDetectorAction.INSTANCE.name()); | ||
Assert.assertEquals(SearchAnomalyDetectorAction.INSTANCE.name(), SearchAnomalyDetectorAction.NAME); | ||
} | ||
|
||
@Test | ||
public void testNoIndex() { | ||
// No Index, will call response.onFailure | ||
SearchRequest searchRequest = new SearchRequest("my-test-index"); | ||
action.doExecute(task, searchRequest, response); | ||
} | ||
} |
Oops, something went wrong.