Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Exclude detector with input detector id when searching detector with …
Browse files Browse the repository at this point in the history
…duplicate name
  • Loading branch information
yizheliu-amazon committed May 22, 2020
1 parent 49f30a2 commit 6f1c83c
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.BytesRestResponse;
Expand All @@ -44,13 +45,13 @@
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestResponseListener;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX;
import static com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils.XCONTENT_WITH_TYPE;
Expand Down Expand Up @@ -228,10 +229,12 @@ private void onSearchAdInputIndicesResponse(SearchResponse response, String dete
}

private void checkADNameExists(String detectorId) throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
// src/main/resources/mappings/anomaly-detectors.json#L14
.query(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName()))
.timeout(requestTimeout);
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// src/main/resources/mappings/anomaly-detectors.json#L14
boolQueryBuilder.must(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName()));
// _id field does not allow "", but allows " "
boolQueryBuilder.mustNot(QueryBuilders.termQuery(RestHandlerUtils._ID, StringUtils.isBlank(detectorId) ? " " : detectorId));

This comment has been minimized.

Copy link
@wnbts

wnbts May 23, 2020

Contributor

if new detector id is null, this condition is not needed.

This comment has been minimized.

Copy link
@yizheliu-amazon

yizheliu-amazon May 23, 2020

Author Contributor

Yeah. changed this in new commit: 64d7f0e#diff-9c2862967c8c25fead0856eee36f73edR235-R237

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder).timeout(requestTimeout);
SearchRequest searchRequest = new SearchRequest(ANOMALY_DETECTORS_INDEX).source(searchSourceBuilder);

client
Expand All @@ -246,21 +249,14 @@ private void checkADNameExists(String detectorId) throws IOException {
}

private void onSearchADNameResponse(SearchResponse response, String detectorId, String name) throws IOException {
boolean hasDuplicateName = false;
String existingDetectorId = null;
if (response.getHits().getTotalHits().value > 0) {
for (SearchHit hit : response.getHits()) {
if (!hit.getId().equals(detectorId)) {
hasDuplicateName = true;
existingDetectorId = hit.getId();
break;
}
}
}

if (hasDuplicateName) {
String errorMsg = String.format("Cannot create anomaly detector with name[%s] used by detectorId %s", name, existingDetectorId);
logger.error(errorMsg);
String errorMsg = String
.format(
"Cannot create anomaly detector with name[%s] used by detectorId %s",
name,
Arrays.stream(response.getHits().getHits()).map(hit -> hit.getId()).collect(Collectors.toList())
);
logger.warn(errorMsg);
onFailure(new IllegalArgumentException(errorMsg));
} else {
indexAnomalyDetector(detectorId);
Expand Down

0 comments on commit 6f1c83c

Please sign in to comment.