use of org.graylog.shaded.elasticsearch6.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);
}
}
use of org.graylog.shaded.elasticsearch6.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;
}
use of org.graylog.shaded.elasticsearch6.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;
}
use of org.graylog.shaded.elasticsearch6.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;
}
use of org.graylog.shaded.elasticsearch6.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;
}
}
Aggregations