Search in sources :

Example 1 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchMetaAlertDao method addAlertsToMetaAlert.

protected boolean addAlertsToMetaAlert(Document metaAlert, Iterable<Document> alerts) {
    boolean alertAdded = false;
    List<Map<String, Object>> currentAlerts = (List<Map<String, Object>>) metaAlert.getDocument().get(ALERT_FIELD);
    Set<String> currentAlertGuids = currentAlerts.stream().map(currentAlert -> (String) currentAlert.get(GUID)).collect(Collectors.toSet());
    for (Document alert : alerts) {
        String alertGuid = alert.getGuid();
        // Only add an alert if it isn't already in the meta alert
        if (!currentAlertGuids.contains(alertGuid)) {
            currentAlerts.add(alert.getDocument());
            alertAdded = true;
        }
    }
    return alertAdded;
}
Also used : InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) GetResponse(org.elasticsearch.action.get.GetResponse) PatchRequest(org.apache.metron.indexing.dao.update.PatchRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilders.nestedQuery(org.elasticsearch.index.query.QueryBuilders.nestedQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) GroupResponse(org.apache.metron.indexing.dao.search.GroupResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchResult(org.apache.metron.indexing.dao.search.SearchResult) MetaAlertCreateRequest(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) SearchHit(org.elasticsearch.search.SearchHit) Document(org.apache.metron.indexing.dao.update.Document) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) MetaAlertDao(org.apache.metron.indexing.dao.MetaAlertDao) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) IndexDao(org.apache.metron.indexing.dao.IndexDao) MetaScores(org.apache.metron.indexing.dao.metaalert.MetaScores) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) ScoreMode(org.apache.lucene.search.join.ScoreMode) Collectors(java.util.stream.Collectors) Item(org.elasticsearch.action.get.MultiGetRequest.Item) OriginalNotFoundException(org.apache.metron.indexing.dao.update.OriginalNotFoundException) InvalidCreateException(org.apache.metron.indexing.dao.search.InvalidCreateException) Entry(java.util.Map.Entry) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) java.util(java.util) FieldType(org.apache.metron.indexing.dao.search.FieldType) MetaAlertStatus(org.apache.metron.indexing.dao.metaalert.MetaAlertStatus) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) GUID(org.apache.metron.common.Constants.GUID) MultiIndexDao(org.apache.metron.indexing.dao.MultiIndexDao) SetUtils(org.apache.commons.collections4.SetUtils) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) GroupRequest(org.apache.metron.indexing.dao.search.GroupRequest) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Constants(org.apache.metron.common.Constants) MetaAlertCreateResponse(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ConversionUtils(org.apache.metron.stellar.common.utils.ConversionUtils) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) QueryBuilders.existsQuery(org.elasticsearch.index.query.QueryBuilders.existsQuery) Document(org.apache.metron.indexing.dao.update.Document)

Example 2 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchMetaAlertDao method update.

@Override
public void update(Document update, Optional<String> index) throws IOException {
    if (METAALERT_TYPE.equals(update.getSensorType())) {
        // We've been passed an update to the meta alert.
        throw new UnsupportedOperationException("Meta alerts cannot be directly updated");
    } else {
        Map<Document, Optional<String>> updates = new HashMap<>();
        updates.put(update, index);
        // We need to update an alert itself.  Only that portion of the update can be delegated.
        // We still need to get meta alerts potentially associated with it and update.
        Collection<Document> metaAlerts = getMetaAlertsForAlert(update.getGuid()).getResults().stream().map(searchResult -> new Document(searchResult.getSource(), searchResult.getId(), METAALERT_TYPE, 0L)).collect(Collectors.toList());
        // Each meta alert needs to be updated with the new alert
        for (Document metaAlert : metaAlerts) {
            replaceAlertInMetaAlert(metaAlert, update);
            updates.put(metaAlert, Optional.of(METAALERTS_INDEX));
        }
        // Run the alert's update
        indexDao.batchUpdate(updates);
    }
}
Also used : InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) GetResponse(org.elasticsearch.action.get.GetResponse) PatchRequest(org.apache.metron.indexing.dao.update.PatchRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilders.nestedQuery(org.elasticsearch.index.query.QueryBuilders.nestedQuery) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) GroupResponse(org.apache.metron.indexing.dao.search.GroupResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchResult(org.apache.metron.indexing.dao.search.SearchResult) MetaAlertCreateRequest(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) SearchHit(org.elasticsearch.search.SearchHit) Document(org.apache.metron.indexing.dao.update.Document) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) MetaAlertDao(org.apache.metron.indexing.dao.MetaAlertDao) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) IndexDao(org.apache.metron.indexing.dao.IndexDao) MetaScores(org.apache.metron.indexing.dao.metaalert.MetaScores) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) ScoreMode(org.apache.lucene.search.join.ScoreMode) Collectors(java.util.stream.Collectors) Item(org.elasticsearch.action.get.MultiGetRequest.Item) OriginalNotFoundException(org.apache.metron.indexing.dao.update.OriginalNotFoundException) InvalidCreateException(org.apache.metron.indexing.dao.search.InvalidCreateException) Entry(java.util.Map.Entry) QueryBuilders.constantScoreQuery(org.elasticsearch.index.query.QueryBuilders.constantScoreQuery) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) java.util(java.util) FieldType(org.apache.metron.indexing.dao.search.FieldType) MetaAlertStatus(org.apache.metron.indexing.dao.metaalert.MetaAlertStatus) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) GUID(org.apache.metron.common.Constants.GUID) MultiIndexDao(org.apache.metron.indexing.dao.MultiIndexDao) SetUtils(org.apache.commons.collections4.SetUtils) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) GroupRequest(org.apache.metron.indexing.dao.search.GroupRequest) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IOException(java.io.IOException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Constants(org.apache.metron.common.Constants) MetaAlertCreateResponse(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ConversionUtils(org.apache.metron.stellar.common.utils.ConversionUtils) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) QueryBuilders.existsQuery(org.elasticsearch.index.query.QueryBuilders.existsQuery) Document(org.apache.metron.indexing.dao.update.Document)

Example 3 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchMetaAlertDao method removeAlertsFromMetaAlert.

@Override
public boolean removeAlertsFromMetaAlert(String metaAlertGuid, List<GetRequest> alertRequests) throws IOException {
    Map<Document, Optional<String>> updates = new HashMap<>();
    Document metaAlert = indexDao.getLatest(metaAlertGuid, METAALERT_TYPE);
    if (MetaAlertStatus.ACTIVE.getStatusString().equals(metaAlert.getDocument().get(STATUS_FIELD))) {
        Iterable<Document> alerts = indexDao.getAllLatest(alertRequests);
        Collection<String> alertGuids = alertRequests.stream().map(GetRequest::getGuid).collect(Collectors.toList());
        boolean metaAlertUpdated = removeAlertsFromMetaAlert(metaAlert, alertGuids);
        if (metaAlertUpdated) {
            calculateMetaScores(metaAlert);
            updates.put(metaAlert, Optional.of(index));
            for (Document alert : alerts) {
                if (removeMetaAlertFromAlert(metaAlert.getGuid(), alert)) {
                    updates.put(alert, Optional.empty());
                }
            }
            indexDaoUpdate(updates);
        }
        return metaAlertUpdated;
    } else {
        throw new IllegalStateException("Removing alerts from an INACTIVE meta alert is not allowed");
    }
}
Also used : Document(org.apache.metron.indexing.dao.update.Document)

Example 4 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchMetaAlertDao method getAllAlertsForMetaAlert.

@SuppressWarnings("unchecked")
protected List<Map<String, Object>> getAllAlertsForMetaAlert(Document update) throws IOException {
    Document latest = indexDao.getLatest(update.getGuid(), MetaAlertDao.METAALERT_TYPE);
    if (latest == null) {
        return new ArrayList<>();
    }
    List<String> guids = new ArrayList<>();
    List<Map<String, Object>> latestAlerts = (List<Map<String, Object>>) latest.getDocument().get(MetaAlertDao.ALERT_FIELD);
    for (Map<String, Object> alert : latestAlerts) {
        guids.add((String) alert.get(Constants.GUID));
    }
    List<Map<String, Object>> alerts = new ArrayList<>();
    QueryBuilder query = QueryBuilders.idsQuery().addIds(guids.toArray(new String[0]));
    SearchRequestBuilder request = elasticsearchDao.getClient().prepareSearch().setQuery(query);
    org.elasticsearch.action.search.SearchResponse response = request.get();
    for (SearchHit hit : response.getHits().getHits()) {
        alerts.add(hit.sourceAsMap());
    }
    return alerts;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.apache.metron.indexing.dao.update.Document)

Example 5 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchMetaAlertDao method addAlertsToMetaAlert.

@Override
public boolean addAlertsToMetaAlert(String metaAlertGuid, List<GetRequest> alertRequests) throws IOException {
    Map<Document, Optional<String>> updates = new HashMap<>();
    Document metaAlert = indexDao.getLatest(metaAlertGuid, METAALERT_TYPE);
    if (MetaAlertStatus.ACTIVE.getStatusString().equals(metaAlert.getDocument().get(STATUS_FIELD))) {
        Iterable<Document> alerts = indexDao.getAllLatest(alertRequests);
        boolean metaAlertUpdated = addAlertsToMetaAlert(metaAlert, alerts);
        if (metaAlertUpdated) {
            calculateMetaScores(metaAlert);
            updates.put(metaAlert, Optional.of(index));
            for (Document alert : alerts) {
                if (addMetaAlertToAlert(metaAlert.getGuid(), alert)) {
                    updates.put(alert, Optional.empty());
                }
            }
            indexDaoUpdate(updates);
        }
        return metaAlertUpdated;
    } else {
        throw new IllegalStateException("Adding alerts to an INACTIVE meta alert is not allowed");
    }
}
Also used : Document(org.apache.metron.indexing.dao.update.Document)

Aggregations

Document (org.apache.metron.indexing.dao.update.Document)31 ArrayList (java.util.ArrayList)13 GetRequest (org.apache.metron.indexing.dao.search.GetRequest)12 Map (java.util.Map)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 List (java.util.List)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)5 AccessConfig (org.apache.metron.indexing.dao.AccessConfig)5 IndexDao (org.apache.metron.indexing.dao.IndexDao)5 MetaAlertCreateResponse (org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse)4 FieldType (org.apache.metron.indexing.dao.search.FieldType)4 GroupRequest (org.apache.metron.indexing.dao.search.GroupRequest)4 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)4 java.util (java.util)3 Constants (org.apache.metron.common.Constants)3 MockHBaseTableProvider (org.apache.metron.hbase.mock.MockHBaseTableProvider)3 MetaAlertCreateRequest (org.apache.metron.indexing.dao.metaalert.MetaAlertCreateRequest)3