Java Code Examples for org.elasticsearch.search.builder.SearchSourceBuilder#searchSource()
The following examples show how to use
org.elasticsearch.search.builder.SearchSourceBuilder#searchSource() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileThreadSnapshotQueryEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public List<ProfileThreadSnapshotRecord> queryRecords(String segmentId, int minSequence, int maxSequence) throws IOException { // search traces SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); sourceBuilder.query(boolQueryBuilder); List<QueryBuilder> mustQueryList = boolQueryBuilder.must(); mustQueryList.add(QueryBuilders.termQuery(ProfileThreadSnapshotRecord.SEGMENT_ID, segmentId)); mustQueryList.add(QueryBuilders.rangeQuery(ProfileThreadSnapshotRecord.SEQUENCE).gte(minSequence).lt(maxSequence)); sourceBuilder.size(maxSequence - minSequence); SearchResponse response = getClient().search(ProfileThreadSnapshotRecord.INDEX_NAME, sourceBuilder); List<ProfileThreadSnapshotRecord> result = new ArrayList<>(maxSequence - minSequence); for (SearchHit searchHit : response.getHits().getHits()) { ProfileThreadSnapshotRecord record = builder.map2Data(searchHit.getSourceAsMap()); result.add(record); } return result; }
Example 2
Source File: ProfileTaskQueryEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public ProfileTask getById(String id) throws IOException { if (StringUtil.isEmpty(id)) { return null; } SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.query(QueryBuilders.idsQuery().addIds(id)); sourceBuilder.size(1); final SearchResponse response = getClient().search(ProfileTaskRecord.INDEX_NAME, sourceBuilder); if (response.getHits().getHits().length > 0) { return parseTask(response.getHits().getHits()[0]); } return null; }
Example 3
Source File: ProfileTaskLogEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public List<ProfileTaskLog> getTaskLogList() throws IOException { final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); sourceBuilder.query(boolQueryBuilder); sourceBuilder.sort(ProfileTaskLogRecord.OPERATION_TIME, SortOrder.DESC); sourceBuilder.size(queryMaxSize); final SearchResponse response = getClient().search(ProfileTaskLogRecord.INDEX_NAME, sourceBuilder); final LinkedList<ProfileTaskLog> tasks = new LinkedList<>(); for (SearchHit searchHit : response.getHits().getHits()) { tasks.add(parseTaskLog(searchHit)); } return tasks; }
Example 4
Source File: UITemplateManagementEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public List<DashboardConfiguration> getAllTemplates(final Boolean includingDisabled) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if (!includingDisabled) { boolQueryBuilder.must().add(QueryBuilders.termQuery(UITemplate.DISABLED, BooleanUtils.booleanToValue(includingDisabled))); } sourceBuilder.query(boolQueryBuilder); //It is impossible we have 10000+ templates. sourceBuilder.size(10000); SearchResponse response = getClient().search(UITemplate.INDEX_NAME, sourceBuilder); List<DashboardConfiguration> configs = new ArrayList<>(); final UITemplate.Builder builder = new UITemplate.Builder(); for (SearchHit searchHit : response.getHits()) { Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); final UITemplate uiTemplate = builder.map2Data(sourceAsMap); configs.add(new DashboardConfiguration().fromEntity(uiTemplate)); } return configs; }
Example 5
Source File: TopologyQueryEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public List<Call.CallDetail> loadEndpointRelation(long startTB, long endTB, String destEndpointId) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.size(0); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); boolQuery.must() .add(QueryBuilders.rangeQuery(EndpointRelationServerSideMetrics.TIME_BUCKET).gte(startTB).lte(endTB)); BoolQueryBuilder serviceIdBoolQuery = QueryBuilders.boolQuery(); boolQuery.must().add(serviceIdBoolQuery); serviceIdBoolQuery.should() .add(QueryBuilders.termQuery( EndpointRelationServerSideMetrics.SOURCE_ENDPOINT, destEndpointId )); serviceIdBoolQuery.should() .add(QueryBuilders.termQuery( EndpointRelationServerSideMetrics.DEST_ENDPOINT, destEndpointId )); sourceBuilder.query(boolQuery); return loadEndpoint(sourceBuilder, EndpointRelationServerSideMetrics.INDEX_NAME, DetectPoint.SERVER); }
Example 6
Source File: MetadataQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Service> getAllServices(long startTimestamp, long endTimestamp) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.termQuery(ServiceTraffic.NODE_TYPE, NodeType.Normal.value())); sourceBuilder.query(boolQueryBuilder); sourceBuilder.size(queryMaxSize); SearchResponse response = getClient().search(ServiceTraffic.INDEX_NAME, sourceBuilder); return buildServices(response); }
Example 7
Source File: ProfileThreadSnapshotQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public SegmentRecord getProfiledSegment(String segmentId) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.query(QueryBuilders.termQuery(SegmentRecord.SEGMENT_ID, segmentId)); sourceBuilder.size(1); SearchResponse response = getClient().search(SegmentRecord.INDEX_NAME, sourceBuilder); if (response.getHits().getHits().length == 0) { return null; } SearchHit searchHit = response.getHits().getHits()[0]; SegmentRecord segmentRecord = new SegmentRecord(); segmentRecord.setSegmentId((String) searchHit.getSourceAsMap().get(SegmentRecord.SEGMENT_ID)); segmentRecord.setTraceId((String) searchHit.getSourceAsMap().get(SegmentRecord.TRACE_ID)); segmentRecord.setServiceId((String) searchHit.getSourceAsMap().get(SegmentRecord.SERVICE_ID)); segmentRecord.setEndpointName((String) searchHit.getSourceAsMap().get(SegmentRecord.ENDPOINT_NAME)); segmentRecord.setStartTime(((Number) searchHit.getSourceAsMap().get(SegmentRecord.START_TIME)).longValue()); segmentRecord.setEndTime(((Number) searchHit.getSourceAsMap().get(SegmentRecord.END_TIME)).longValue()); segmentRecord.setLatency(((Number) searchHit.getSourceAsMap().get(SegmentRecord.LATENCY)).intValue()); segmentRecord.setIsError(((Number) searchHit.getSourceAsMap().get(SegmentRecord.IS_ERROR)).intValue()); String dataBinaryBase64 = (String) searchHit.getSourceAsMap().get(SegmentRecord.DATA_BINARY); if (!Strings.isNullOrEmpty(dataBinaryBase64)) { segmentRecord.setDataBinary(Base64.getDecoder().decode(dataBinaryBase64)); } segmentRecord.setVersion(((Number) searchHit.getSourceAsMap().get(SegmentRecord.VERSION)).intValue()); return segmentRecord; }
Example 8
Source File: TopNRecordsQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<SelectedRecord> readSampledRecords(final TopNCondition condition, final String valueColumnName, final Duration duration) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.rangeQuery(TopN.TIME_BUCKET) .gte(duration.getStartTimeBucketInSec()) .lte(duration.getEndTimeBucketInSec())); if (StringUtil.isNotEmpty(condition.getParentService())) { final String serviceId = IDManager.ServiceID.buildId(condition.getParentService(), condition.isNormal()); boolQueryBuilder.must().add(QueryBuilders.termQuery(TopN.SERVICE_ID, serviceId)); } sourceBuilder.query(boolQueryBuilder); sourceBuilder.size(condition.getTopN()) .sort(valueColumnName, condition.getOrder().equals(Order.DES) ? SortOrder.DESC : SortOrder.ASC); SearchResponse response = getClient().search(condition.getName(), sourceBuilder); List<SelectedRecord> results = new ArrayList<>(condition.getTopN()); for (SearchHit searchHit : response.getHits().getHits()) { SelectedRecord record = new SelectedRecord(); final Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); record.setName((String) sourceAsMap.get(TopN.STATEMENT)); record.setRefId((String) sourceAsMap.get(TopN.TRACE_ID)); record.setId(record.getRefId()); record.setValue(((Number) sourceAsMap.get(valueColumnName)).toString()); results.add(record); } return results; }
Example 9
Source File: TraceQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<SegmentRecord> queryByTraceId(String traceId) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.query(QueryBuilders.termQuery(SegmentRecord.TRACE_ID, traceId)); sourceBuilder.size(segmentQueryMaxSize); SearchResponse response = getClient().search(SegmentRecord.INDEX_NAME, sourceBuilder); List<SegmentRecord> segmentRecords = new ArrayList<>(); for (SearchHit searchHit : response.getHits().getHits()) { SegmentRecord segmentRecord = new SegmentRecord(); segmentRecord.setSegmentId((String) searchHit.getSourceAsMap().get(SegmentRecord.SEGMENT_ID)); segmentRecord.setTraceId((String) searchHit.getSourceAsMap().get(SegmentRecord.TRACE_ID)); segmentRecord.setServiceId((String) searchHit.getSourceAsMap().get(SegmentRecord.SERVICE_ID)); segmentRecord.setEndpointName((String) searchHit.getSourceAsMap().get(SegmentRecord.ENDPOINT_NAME)); segmentRecord.setStartTime(((Number) searchHit.getSourceAsMap().get(SegmentRecord.START_TIME)).longValue()); segmentRecord.setEndTime(((Number) searchHit.getSourceAsMap().get(SegmentRecord.END_TIME)).longValue()); segmentRecord.setLatency(((Number) searchHit.getSourceAsMap().get(SegmentRecord.LATENCY)).intValue()); segmentRecord.setIsError(((Number) searchHit.getSourceAsMap().get(SegmentRecord.IS_ERROR)).intValue()); String dataBinaryBase64 = (String) searchHit.getSourceAsMap().get(SegmentRecord.DATA_BINARY); if (!Strings.isNullOrEmpty(dataBinaryBase64)) { segmentRecord.setDataBinary(Base64.getDecoder().decode(dataBinaryBase64)); } segmentRecord.setVersion(((Number) searchHit.getSourceAsMap().get(SegmentRecord.VERSION)).intValue()); segmentRecords.add(segmentRecord); } return segmentRecords; }
Example 10
Source File: TopologyQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Call.CallDetail> loadServiceRelationsDetectedAtServerSide(long startTB, long endTB, List<String> serviceIds) throws IOException { if (CollectionUtils.isEmpty(serviceIds)) { throw new UnexpectedException("Service id is empty"); } SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.size(0); setQueryCondition(sourceBuilder, startTB, endTB, serviceIds); return buildServiceRelation(sourceBuilder, ServiceRelationServerSideMetrics.INDEX_NAME, DetectPoint.SERVER); }
Example 11
Source File: MetadataQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Endpoint> searchEndpoint(String keyword, String serviceId, int limit) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.termQuery(EndpointTraffic.SERVICE_ID, serviceId)); if (!Strings.isNullOrEmpty(keyword)) { String matchCName = MatchCNameBuilder.INSTANCE.build(EndpointTraffic.NAME); boolQueryBuilder.must().add(QueryBuilders.matchQuery(matchCName, keyword)); } sourceBuilder.query(boolQueryBuilder); sourceBuilder.size(limit); SearchResponse response = getClient().search(EndpointTraffic.INDEX_NAME, sourceBuilder); List<Endpoint> endpoints = new ArrayList<>(); for (SearchHit searchHit : response.getHits()) { Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); final EndpointTraffic endpointTraffic = new EndpointTraffic.Builder().map2Data(sourceAsMap); Endpoint endpoint = new Endpoint(); endpoint.setId(endpointTraffic.id()); endpoint.setName((String) sourceAsMap.get(EndpointTraffic.NAME)); endpoints.add(endpoint); } return endpoints; }
Example 12
Source File: MetadataQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Service> getAllBrowserServices(long startTimestamp, long endTimestamp) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.termQuery(ServiceTraffic.NODE_TYPE, NodeType.Browser.value())); sourceBuilder.query(boolQueryBuilder); sourceBuilder.size(queryMaxSize); SearchResponse response = getClient().search(ServiceTraffic.INDEX_NAME, sourceBuilder); return buildServices(response); }
Example 13
Source File: TopologyQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Call.CallDetail> loadInstanceRelationDetectedAtClientSide(String clientServiceId, String serverServiceId, long startTB, long endTB) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.size(0); setInstanceQueryCondition(sourceBuilder, startTB, endTB, clientServiceId, serverServiceId); return buildInstanceRelation( sourceBuilder, ServiceInstanceRelationClientSideMetrics.INDEX_NAME, DetectPoint.CLIENT); }
Example 14
Source File: AlarmQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
public Alarms getAlarm(final Integer scopeId, final String keyword, final int limit, final int from, final long startTB, final long endTB) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.rangeQuery(AlarmRecord.TIME_BUCKET).gte(startTB).lte(endTB)); if (Objects.nonNull(scopeId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(AlarmRecord.SCOPE, scopeId.intValue())); } if (!Strings.isNullOrEmpty(keyword)) { String matchCName = MatchCNameBuilder.INSTANCE.build(AlarmRecord.ALARM_MESSAGE); boolQueryBuilder.must().add(QueryBuilders.matchPhraseQuery(matchCName, keyword)); } sourceBuilder.query(boolQueryBuilder).sort(AlarmRecord.START_TIME, SortOrder.DESC); sourceBuilder.size(limit); sourceBuilder.from(from); SearchResponse response = getClient().search(AlarmRecord.INDEX_NAME, sourceBuilder); Alarms alarms = new Alarms(); alarms.setTotal((int) response.getHits().totalHits); for (SearchHit searchHit : response.getHits().getHits()) { AlarmRecord.Builder builder = new AlarmRecord.Builder(); AlarmRecord alarmRecord = builder.map2Data(searchHit.getSourceAsMap()); AlarmMessage message = new AlarmMessage(); message.setId(String.valueOf(alarmRecord.getId0())); message.setMessage(alarmRecord.getAlarmMessage()); message.setStartTime(alarmRecord.getStartTime()); message.setScope(Scope.Finder.valueOf(alarmRecord.getScope())); message.setScopeId(alarmRecord.getScope()); alarms.getMsgs().add(message); } return alarms; }
Example 15
Source File: AlarmQueryEs7DAO.java From skywalking with Apache License 2.0 | 5 votes |
public Alarms getAlarm(final Integer scopeId, final String keyword, final int limit, final int from, final long startTB, final long endTB) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must().add(QueryBuilders.rangeQuery(AlarmRecord.TIME_BUCKET).gte(startTB).lte(endTB)); if (Objects.nonNull(scopeId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(AlarmRecord.SCOPE, scopeId.intValue())); } if (!Strings.isNullOrEmpty(keyword)) { String matchCName = MatchCNameBuilder.INSTANCE.build(AlarmRecord.ALARM_MESSAGE); boolQueryBuilder.must().add(QueryBuilders.matchPhraseQuery(matchCName, keyword)); } sourceBuilder.query(boolQueryBuilder).sort(AlarmRecord.START_TIME, SortOrder.DESC); sourceBuilder.size(limit); sourceBuilder.from(from); SearchResponse response = getClient().search(AlarmRecord.INDEX_NAME, sourceBuilder); Alarms alarms = new Alarms(); alarms.setTotal((int) response.getHits().getTotalHits().value); for (SearchHit searchHit : response.getHits().getHits()) { AlarmRecord.Builder builder = new AlarmRecord.Builder(); AlarmRecord alarmRecord = builder.map2Data(searchHit.getSourceAsMap()); AlarmMessage message = new AlarmMessage(); message.setId(String.valueOf(alarmRecord.getId0())); message.setMessage(alarmRecord.getAlarmMessage()); message.setStartTime(alarmRecord.getStartTime()); message.setScope(Scope.Finder.valueOf(alarmRecord.getScope())); message.setScopeId(alarmRecord.getScope()); alarms.getMsgs().add(message); } return alarms; }
Example 16
Source File: TopologyQueryEsDAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<Call.CallDetail> loadServiceRelationsDetectedAtServerSide(long startTB, long endTB) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.query(QueryBuilders.rangeQuery(ServiceRelationServerSideMetrics.TIME_BUCKET) .gte(startTB) .lte(endTB)); sourceBuilder.size(0); return buildServiceRelation(sourceBuilder, ServiceRelationServerSideMetrics.INDEX_NAME, DetectPoint.SERVER); }
Example 17
Source File: ZipkinTraceQueryEsDAO.java From skywalking with Apache License 2.0 | 4 votes |
@Override public List<org.apache.skywalking.oap.server.core.query.type.Span> doFlexibleTraceQuery( String traceId) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); sourceBuilder.query(QueryBuilders.termQuery(TRACE_ID, traceId)); sourceBuilder.sort(START_TIME, SortOrder.ASC); sourceBuilder.size(1000); SearchResponse response = getClient().search(ZipkinSpanRecord.INDEX_NAME, sourceBuilder); List<org.apache.skywalking.oap.server.core.query.type.Span> spanList = new ArrayList<>(); for (SearchHit searchHit : response.getHits().getHits()) { String serviceId = (String) searchHit.getSourceAsMap().get(SERVICE_ID); String dataBinaryBase64 = (String) searchHit.getSourceAsMap().get(SegmentRecord.DATA_BINARY); Span span = SpanBytesDecoder.PROTO3.decodeOne(Base64.getDecoder().decode(dataBinaryBase64)); org.apache.skywalking.oap.server.core.query.type.Span swSpan = new org.apache.skywalking.oap.server.core.query.type.Span(); swSpan.setTraceId(span.traceId()); swSpan.setEndpointName(span.name()); swSpan.setStartTime(span.timestamp() / 1000); swSpan.setEndTime(swSpan.getStartTime() + span.durationAsLong() / 1000); span.tags().forEach((key, value) -> { swSpan.getTags().add(new KeyValue(key, value)); }); span.annotations().forEach(annotation -> { LogEntity entity = new LogEntity(); entity.setTime(annotation.timestamp() / 1000); entity.getData().add(new KeyValue("annotation", annotation.value())); swSpan.getLogs().add(entity); }); if (StringUtil.isNotEmpty(serviceId)) { final IDManager.ServiceID.ServiceIDDefinition serviceIDDefinition = IDManager.ServiceID.analysisId( serviceId); swSpan.setServiceCode(serviceIDDefinition.getName()); } swSpan.setSpanId(0); swSpan.setParentSpanId(-1); swSpan.setSegmentSpanId(span.id()); swSpan.setSegmentId(span.id()); Span.Kind kind = span.kind(); switch (kind) { case CLIENT: case PRODUCER: swSpan.setType("Entry"); break; case SERVER: case CONSUMER: swSpan.setType("Exit"); break; default: swSpan.setType("Local"); } if (StringUtil.isEmpty(span.parentId())) { swSpan.setRoot(true); swSpan.setSegmentParentSpanId(""); } else { Ref ref = new Ref(); ref.setTraceId(span.traceId()); ref.setParentSegmentId(span.parentId()); ref.setType(RefType.CROSS_PROCESS); ref.setParentSpanId(0); swSpan.getRefs().add(ref); swSpan.setSegmentParentSpanId(span.parentId()); } spanList.add(swSpan); } return spanList; }
Example 18
Source File: MetadataQueryEsDAO.java From skywalking with Apache License 2.0 | 4 votes |
@Override public List<ServiceInstance> getServiceInstances(long startTimestamp, long endTimestamp, String serviceId) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); final long minuteTimeBucket = TimeBucket.getMinuteTimeBucket(startTimestamp); boolQueryBuilder.must() .add(QueryBuilders.rangeQuery(InstanceTraffic.LAST_PING_TIME_BUCKET).gte(minuteTimeBucket)); boolQueryBuilder.must().add(QueryBuilders.termQuery(InstanceTraffic.SERVICE_ID, serviceId)); sourceBuilder.query(boolQueryBuilder); sourceBuilder.size(queryMaxSize); SearchResponse response = getClient().search(InstanceTraffic.INDEX_NAME, sourceBuilder); List<ServiceInstance> serviceInstances = new ArrayList<>(); for (SearchHit searchHit : response.getHits()) { Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); final InstanceTraffic instanceTraffic = new InstanceTraffic.Builder().map2Data(sourceAsMap); ServiceInstance serviceInstance = new ServiceInstance(); serviceInstance.setId(instanceTraffic.id()); serviceInstance.setName(instanceTraffic.getName()); serviceInstance.setInstanceUUID(serviceInstance.getId()); JsonObject properties = instanceTraffic.getProperties(); if (properties != null) { for (Map.Entry<String, JsonElement> property : properties.entrySet()) { String key = property.getKey(); String value = property.getValue().getAsString(); if (key.equals(LANGUAGE)) { serviceInstance.setLanguage(Language.value(value)); } else { serviceInstance.getAttributes().add(new Attribute(key, value)); } } } else { serviceInstance.setLanguage(Language.UNKNOWN); } serviceInstances.add(serviceInstance); } return serviceInstances; }
Example 19
Source File: TraceQueryEs7DAO.java From skywalking with Apache License 2.0 | 4 votes |
@Override public TraceBrief queryBasicTraces(long startSecondTB, long endSecondTB, long minDuration, long maxDuration, String endpointName, String serviceId, String serviceInstanceId, String endpointId, String traceId, int limit, int from, TraceState traceState, QueryOrder queryOrder) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); sourceBuilder.query(boolQueryBuilder); List<QueryBuilder> mustQueryList = boolQueryBuilder.must(); if (startSecondTB != 0 && endSecondTB != 0) { mustQueryList.add(QueryBuilders.rangeQuery(SegmentRecord.TIME_BUCKET).gte(startSecondTB).lte(endSecondTB)); } if (minDuration != 0 || maxDuration != 0) { RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(SegmentRecord.LATENCY); if (minDuration != 0) { rangeQueryBuilder.gte(minDuration); } if (maxDuration != 0) { rangeQueryBuilder.lte(maxDuration); } boolQueryBuilder.must().add(rangeQueryBuilder); } if (!Strings.isNullOrEmpty(endpointName)) { String matchCName = MatchCNameBuilder.INSTANCE.build(SegmentRecord.ENDPOINT_NAME); mustQueryList.add(QueryBuilders.matchPhraseQuery(matchCName, endpointName)); } if (StringUtil.isNotEmpty(serviceId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(SegmentRecord.SERVICE_ID, serviceId)); } if (StringUtil.isNotEmpty(serviceInstanceId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(SegmentRecord.SERVICE_INSTANCE_ID, serviceInstanceId)); } if (!Strings.isNullOrEmpty(endpointId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(SegmentRecord.ENDPOINT_ID, endpointId)); } if (!Strings.isNullOrEmpty(traceId)) { boolQueryBuilder.must().add(QueryBuilders.termQuery(SegmentRecord.TRACE_ID, traceId)); } switch (traceState) { case ERROR: mustQueryList.add(QueryBuilders.matchQuery(SegmentRecord.IS_ERROR, BooleanUtils.TRUE)); break; case SUCCESS: mustQueryList.add(QueryBuilders.matchQuery(SegmentRecord.IS_ERROR, BooleanUtils.FALSE)); break; } switch (queryOrder) { case BY_START_TIME: sourceBuilder.sort(SegmentRecord.START_TIME, SortOrder.DESC); break; case BY_DURATION: sourceBuilder.sort(SegmentRecord.LATENCY, SortOrder.DESC); break; } sourceBuilder.size(limit); sourceBuilder.from(from); SearchResponse response = getClient().search(SegmentRecord.INDEX_NAME, sourceBuilder); TraceBrief traceBrief = new TraceBrief(); traceBrief.setTotal((int) response.getHits().getTotalHits().value); for (SearchHit searchHit : response.getHits().getHits()) { BasicTrace basicTrace = new BasicTrace(); basicTrace.setSegmentId((String) searchHit.getSourceAsMap().get(SegmentRecord.SEGMENT_ID)); basicTrace.setStart(String.valueOf(searchHit.getSourceAsMap().get(SegmentRecord.START_TIME))); basicTrace.getEndpointNames().add((String) searchHit.getSourceAsMap().get(SegmentRecord.ENDPOINT_NAME)); basicTrace.setDuration(((Number) searchHit.getSourceAsMap().get(SegmentRecord.LATENCY)).intValue()); basicTrace.setError(BooleanUtils.valueToBoolean(((Number) searchHit.getSourceAsMap() .get( SegmentRecord.IS_ERROR)).intValue())); basicTrace.getTraceIds().add((String) searchHit.getSourceAsMap().get(SegmentRecord.TRACE_ID)); traceBrief.getTraces().add(basicTrace); } return traceBrief; }
Example 20
Source File: AggregationQueryEs7DAO.java From skywalking with Apache License 2.0 | 4 votes |
@Override public List<SelectedRecord> sortMetrics(final TopNCondition condition, final String valueColumnName, final Duration duration, final List<KeyValue> additionalConditions) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); final RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery(Metrics.TIME_BUCKET) .lte(duration.getEndTimeBucket()) .gte(duration.getStartTimeBucket()); boolean asc = false; if (condition.getOrder().equals(Order.ASC)) { asc = true; } if (additionalConditions != null && additionalConditions.size() > 0) { BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); additionalConditions.forEach(additionalCondition -> { boolQuery.must() .add(QueryBuilders.termsQuery(additionalCondition.getKey(), additionalCondition.getValue())); }); boolQuery.must().add(queryBuilder); sourceBuilder.query(boolQuery); } else { sourceBuilder.query(queryBuilder); } sourceBuilder.aggregation( AggregationBuilders.terms(Metrics.ENTITY_ID) .field(Metrics.ENTITY_ID) .order(BucketOrder.aggregation(valueColumnName, asc)) .size(condition.getTopN()) .subAggregation(AggregationBuilders.avg(valueColumnName).field(valueColumnName)) ); SearchResponse response = getClient().search(condition.getName(), sourceBuilder); List<SelectedRecord> topNList = new ArrayList<>(); Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID); for (Terms.Bucket termsBucket : idTerms.getBuckets()) { SelectedRecord record = new SelectedRecord(); record.setId(termsBucket.getKeyAsString()); Avg value = termsBucket.getAggregations().get(valueColumnName); record.setValue(String.valueOf((long) value.getValue())); topNList.add(record); } return topNList; }