use of org.opensearch.cluster.routing.Preference in project anomaly-detection by opensearch-project.
the class EntityProfileRunner method validateEntity.
/**
* Verify if the input entity exists or not in case of typos.
*
* If a user deletes the entity after job start, then we will not be able to
* get this entity in the index. For this case, we will not return a profile
* for this entity even if it's running on some data node. the entity's model
* will be deleted by another entity or by maintenance due to long inactivity.
*
* @param entity Entity accessor
* @param categoryFields category fields defined for a detector
* @param detectorId Detector Id
* @param profilesToCollect Profile to collect from the input
* @param detector Detector config accessor
* @param listener Callback to send responses.
*/
private void validateEntity(Entity entity, List<String> categoryFields, String detectorId, Set<EntityProfileName> profilesToCollect, AnomalyDetector detector, ActionListener<EntityProfile> listener) {
Map<String, String> attributes = entity.getAttributes();
if (attributes == null || attributes.size() != categoryFields.size()) {
listener.onFailure(new IllegalArgumentException(EMPTY_ENTITY_ATTRIBUTES));
return;
}
for (String field : categoryFields) {
if (false == attributes.containsKey(field)) {
listener.onFailure(new IllegalArgumentException("Cannot find " + field));
return;
}
}
BoolQueryBuilder internalFilterQuery = QueryBuilders.boolQuery().filter(detector.getFilterQuery());
for (TermQueryBuilder term : entity.getTermQueryBuilders()) {
internalFilterQuery.filter(term);
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(internalFilterQuery).size(1);
SearchRequest searchRequest = new SearchRequest(detector.getIndices().toArray(new String[0]), searchSourceBuilder).preference(Preference.LOCAL.toString());
client.search(searchRequest, ActionListener.wrap(searchResponse -> {
try {
if (searchResponse.getHits().getHits().length == 0) {
listener.onFailure(new IllegalArgumentException(NO_ENTITY));
return;
}
prepareEntityProfile(listener, detectorId, entity, profilesToCollect, detector, categoryFields.get(0));
} catch (Exception e) {
listener.onFailure(new IllegalArgumentException(NO_ENTITY));
return;
}
}, e -> listener.onFailure(new IllegalArgumentException(NO_ENTITY))));
}
Aggregations