Search in sources :

Example 46 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project ff4j by ff4j.

the class ElasticQueryBuilder method addOptionalFilters.

public void addOptionalFilters(BoolQueryBuilder booleanQuery, Set<String> filters, String field) {
    if (!filters.isEmpty()) {
        BoolQueryBuilder subQuery = new BoolQueryBuilder();
        for (String filter : filters) {
            subQuery.must(QueryBuilders.matchQuery(field, filter));
        }
        booleanQuery.must(subQuery);
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 47 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project ff4j by ff4j.

the class ElasticQueryBuilder method queryGetEventQueryDefinition.

public Search queryGetEventQueryDefinition(EventQueryDefinition query, String action) {
    BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
    // Optional constant for action filter
    if (action != null) {
        query.getActionFilters().add(action);
    }
    QueryBuilder typeQuery = QueryBuilders.termQuery("type", EventConstants.TARGET_FEATURE);
    // Timestamp filter
    RangeQueryBuilder timestampFilter = // 
    QueryBuilders.rangeQuery("timestamp").gt(// 
    query.getFrom().longValue()).lt(// 
    query.getTo().longValue()).includeLower(// 
    false).includeUpper(false);
    booleanQuery.must(typeQuery);
    booleanQuery.must(timestampFilter);
    // Optional filters
    addOptionalFilters(booleanQuery, query.getActionFilters(), "action");
    addOptionalFilters(booleanQuery, query.getHostFilters(), "hostName");
    addOptionalFilters(booleanQuery, query.getNamesFilter(), "name");
    addOptionalFilters(booleanQuery, query.getSourceFilters(), "source");
    // Warning : default size is set to 10 results, that's why it's
    // overridden
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(100);
    Search searchQuery = // 
    new Search.Builder(searchSourceBuilder.query(booleanQuery.toString()).toString()).addIndex(// 
    connection.getIndexName()).addType(// 
    ElasticConstants.TYPE_EVENT).build();
    return searchQuery;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Search(io.searchbox.core.Search) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 48 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project incubator-skywalking by apache.

the class ApplicationEsCacheDAO method getApplicationIdByCode.

@Override
public int getApplicationIdByCode(String applicationCode) {
    ElasticSearchClient client = getClient();
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ApplicationTable.TABLE);
    searchRequestBuilder.setTypes(ApplicationTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must().add(QueryBuilders.termQuery(ApplicationTable.COLUMN_APPLICATION_CODE, applicationCode));
    boolQueryBuilder.must().add(QueryBuilders.termQuery(ApplicationTable.COLUMN_IS_ADDRESS, BooleanUtils.FALSE));
    searchRequestBuilder.setQuery(boolQueryBuilder);
    searchRequestBuilder.setSize(1);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    if (searchResponse.getHits().totalHits > 0) {
        SearchHit searchHit = searchResponse.getHits().iterator().next();
        return (int) searchHit.getSource().get(ApplicationTable.COLUMN_APPLICATION_ID);
    }
    return 0;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ElasticSearchClient(org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 49 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project incubator-skywalking by apache.

the class ApplicationAlarmEsUIDAO method loadAlarmList.

@Override
public Alarm loadAlarmList(String keyword, long startTimeBucket, long endTimeBucket, int limit, int from) throws ParseException {
    SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(ApplicationAlarmTable.TABLE);
    searchRequestBuilder.setTypes(ApplicationAlarmTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must().add(QueryBuilders.rangeQuery(ApplicationAlarmTable.COLUMN_LAST_TIME_BUCKET).gte(startTimeBucket).lte(endTimeBucket));
    if (StringUtils.isNotEmpty(keyword)) {
        boolQueryBuilder.must().add(QueryBuilders.matchQuery(ApplicationAlarmTable.COLUMN_ALARM_CONTENT, keyword));
    }
    searchRequestBuilder.setQuery(boolQueryBuilder);
    searchRequestBuilder.setSize(limit);
    searchRequestBuilder.setFrom(from);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    SearchHit[] searchHits = searchResponse.getHits().getHits();
    Alarm alarm = new Alarm();
    alarm.setTotal((int) searchResponse.getHits().getTotalHits());
    for (SearchHit searchHit : searchHits) {
        AlarmItem alarmItem = new AlarmItem();
        alarmItem.setId(((Number) searchHit.getSource().get(ApplicationAlarmTable.COLUMN_APPLICATION_ID)).intValue());
        alarmItem.setContent((String) searchHit.getSource().get(ApplicationAlarmTable.COLUMN_ALARM_CONTENT));
        long lastTimeBucket = ((Number) searchHit.getSource().get(ApplicationAlarmTable.COLUMN_LAST_TIME_BUCKET)).longValue();
        alarmItem.setStartTime(TimeBucketUtils.INSTANCE.formatMinuteTimeBucket(lastTimeBucket));
        alarmItem.setAlarmType(AlarmType.APPLICATION);
        int alarmType = ((Number) searchHit.getSource().get(ApplicationAlarmTable.COLUMN_ALARM_TYPE)).intValue();
        if (org.apache.skywalking.apm.collector.storage.table.alarm.AlarmType.SLOW_RTT.getValue() == alarmType) {
            alarmItem.setCauseType(CauseType.SLOW_RESPONSE);
        } else if (org.apache.skywalking.apm.collector.storage.table.alarm.AlarmType.ERROR_RATE.getValue() == alarmType) {
            alarmItem.setCauseType(CauseType.LOW_SUCCESS_RATE);
        }
        alarm.getItems().add(alarmItem);
    }
    return alarm;
}
Also used : AlarmItem(org.apache.skywalking.apm.collector.storage.ui.alarm.AlarmItem) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Alarm(org.apache.skywalking.apm.collector.storage.ui.alarm.Alarm) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 50 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project incubator-skywalking by apache.

the class ApplicationMetricEsUIDAO method getTopNApplicationThroughput.

@Override
public List<ApplicationTPS> getTopNApplicationThroughput(Step step, long startTimeBucket, long endTimeBucket, int betweenSecond, int topN, MetricSource metricSource) {
    String tableName = TimePyramidTableNameBuilder.build(step, ApplicationMetricTable.TABLE);
    SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(tableName);
    searchRequestBuilder.setTypes(ApplicationMetricTable.TABLE_TYPE);
    searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.must().add(QueryBuilders.rangeQuery(ApplicationMetricTable.COLUMN_TIME_BUCKET).gte(startTimeBucket).lte(endTimeBucket));
    boolQuery.must().add(QueryBuilders.termQuery(ApplicationMetricTable.COLUMN_SOURCE_VALUE, metricSource.getValue()));
    searchRequestBuilder.setQuery(boolQuery);
    searchRequestBuilder.setSize(0);
    TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms(ApplicationMetricTable.COLUMN_APPLICATION_ID).field(ApplicationMetricTable.COLUMN_APPLICATION_ID).size(2000);
    aggregationBuilder.subAggregation(AggregationBuilders.sum(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS).field(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS));
    searchRequestBuilder.addAggregation(aggregationBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    List<ApplicationTPS> applicationTPSs = new LinkedList<>();
    Terms applicationIdTerms = searchResponse.getAggregations().get(ApplicationMetricTable.COLUMN_APPLICATION_ID);
    applicationIdTerms.getBuckets().forEach(applicationIdTerm -> {
        int applicationId = applicationIdTerm.getKeyAsNumber().intValue();
        Sum callSum = applicationIdTerm.getAggregations().get(ApplicationMetricTable.COLUMN_TRANSACTION_CALLS);
        long calls = (long) callSum.getValue();
        int callsPerSec = (int) (betweenSecond == 0 ? 0 : calls / betweenSecond);
        ApplicationTPS applicationTPS = new ApplicationTPS();
        applicationTPS.setApplicationId(applicationId);
        applicationTPS.setCallsPerSec(callsPerSec);
        applicationTPSs.add(applicationTPS);
    });
    applicationTPSs.sort((first, second) -> first.getCallsPerSec() > second.getCallsPerSec() ? -1 : 1);
    if (applicationTPSs.size() <= topN) {
        return applicationTPSs;
    } else {
        List<ApplicationTPS> newCollection = new LinkedList<>();
        for (int i = 0; i < topN; i++) {
            newCollection.add(applicationTPSs.get(i));
        }
        return newCollection;
    }
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ApplicationTPS(org.apache.skywalking.apm.collector.storage.ui.overview.ApplicationTPS) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) LinkedList(java.util.LinkedList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)201 SearchResponse (org.elasticsearch.action.search.SearchResponse)73 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)58 SearchHit (org.elasticsearch.search.SearchHit)41 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)38 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)30 Map (java.util.Map)29 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 List (java.util.List)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)21 Test (org.junit.Test)18 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)16 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LinkedList (java.util.LinkedList)11 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)11