Java Code Examples for org.elasticsearch.search.builder.SearchSourceBuilder#sort()
The following examples show how to use
org.elasticsearch.search.builder.SearchSourceBuilder#sort() .
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: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 6 votes |
@Override public List<EventExecution> getEventExecutions(String event) { try { BoolQueryBuilder query = boolQueryBuilder("event='" + event + "'", "*"); // Create the searchObjectIdsViaExpression source SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(query); searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC)); // Generate the actual request to send to ES. String docType = StringUtils.isBlank(docTypeOverride) ? EVENT_DOC_TYPE : docTypeOverride; SearchRequest searchRequest = new SearchRequest(eventIndexPrefix + "*"); searchRequest.types(docType); searchRequest.source(searchSourceBuilder); SearchResponse response = elasticSearchClient.search(searchRequest); return mapEventExecutionsResponse(response); } catch (Exception e) { logger.error("Failed to get executions for event: {}", event, e); } return null; }
Example 2
Source File: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 6 votes |
@Override public List<Message> getMessages(String queue) { try { BoolQueryBuilder query = boolQueryBuilder("queue='" + queue + "'", "*"); // Create the searchObjectIdsViaExpression source SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(query); searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC)); // Generate the actual request to send to ES. String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride; SearchRequest searchRequest = new SearchRequest(messageIndexPrefix + "*"); searchRequest.types(docType); searchRequest.source(searchSourceBuilder); SearchResponse response = elasticSearchClient.search(searchRequest); return mapGetMessagesResponse(response); } catch (Exception e) { logger.error("Failed to get messages for queue: {}", queue, e); } 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: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 6 votes |
@Override public List<TaskExecLog> getTaskExecutionLogs(String taskId) { try { BoolQueryBuilder query = boolQueryBuilder("taskId='" + taskId + "'", "*"); // Create the searchObjectIdsViaExpression source SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(query); searchSourceBuilder.sort(new FieldSortBuilder("createdTime").order(SortOrder.ASC)); searchSourceBuilder.size(config.getElasticSearchTasklogLimit()); // Generate the actual request to send to ES. String docType = StringUtils.isBlank(docTypeOverride) ? LOG_DOC_TYPE : docTypeOverride; SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*"); searchRequest.types(docType); searchRequest.source(searchSourceBuilder); SearchResponse response = elasticSearchClient.search(searchRequest); return mapTaskExecLogsResponse(response); } catch (Exception e) { logger.error("Failed to get task execution logs for task: {}", taskId, e); } return null; }
Example 5
Source File: HighLevelRestController.java From ProjectStudy with MIT License | 5 votes |
/** * 列表查询 * * @param page * @param rows * @param keyword * @return com.example.common.ResponseBean * @throws * @author wliduo[[email protected]] * @date 2019/8/15 16:01 */ @GetMapping("/book") public ResponseBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, String keyword) throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 分页采用简单的from + size分页,适用数据量小的,了解更多分页方式可自行查阅资料 searchSourceBuilder.from((page - 1) * rows); searchSourceBuilder.size(rows); // 查询条件,只有查询关键字不为空才带查询条件 if (StringUtils.isNoneBlank(keyword)) { QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "content", "describe"); searchSourceBuilder.query(queryBuilder); } // 排序,根据ID倒叙 // searchSourceBuilder.sort("id", SortOrder.DESC); // 使用unmappedType,解决排序引起的all shards failed问题,详细请自行查阅资料 FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("id").order(SortOrder.DESC).unmappedType("text"); searchSourceBuilder.sort(fieldSortBuilder); // SearchRequest SearchRequest searchRequest = new SearchRequest(); searchRequest.source(searchSourceBuilder); // 查询ES SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); // 获取总数 Long total = hits.getTotalHits().value; // 遍历封装列表对象 List<BookDto> bookDtoList = new ArrayList<>(); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit : searchHits) { bookDtoList.add(JSON.parseObject(searchHit.getSourceAsString(), BookDto.class)); } // 封装Map参数返回 Map<String, Object> result = new HashMap<String, Object>(16); result.put("count", total); result.put("data", bookDtoList); return new ResponseBean(HttpStatus.OK.value(), "查询成功", result); }
Example 6
Source File: SearchQueryProcessor.java From occurrence with Apache License 2.0 | 5 votes |
/** * Creates a search query that contains the query parameter as the filter query value. */ private static SearchSourceBuilder createSearchQuery(String query) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); if (!Strings.isNullOrEmpty(query)) { searchSourceBuilder.query(QueryBuilders.wrapperQuery(query)); } //key is required since this runs in a distributed installations where the natural order can't be guaranteed searchSourceBuilder.sort(KEY_FIELD, SortOrder.DESC); return searchSourceBuilder; }
Example 7
Source File: TestElasticSearchRestDAOV5.java From conductor with Apache License 2.0 | 5 votes |
/** * Tries to find object ids for a given query in an index. * * @param indexName The name of the index. * @param queryBuilder The query to use for searching. * @param start The start to use. * @param size The total return size. * @param sortOptions A list of string options to sort in the form VALUE:ORDER; where ORDER is optional and can be either ASC OR DESC. * @param docType The document type to searchObjectIdsViaExpression for. * * @return The SearchResults which includes the count and IDs that were found. * @throws IOException If we cannot communicate with ES. */ private SearchResponse searchObjectIds(String indexName, QueryBuilder queryBuilder, int start, int size, List<String> sortOptions, String docType) throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(queryBuilder); searchSourceBuilder.from(start); searchSourceBuilder.size(size); if (sortOptions != null && !sortOptions.isEmpty()) { for (String sortOption : sortOptions) { SortOrder order = SortOrder.ASC; String field = sortOption; int index = sortOption.indexOf(":"); if (index > 0) { field = sortOption.substring(0, index); order = SortOrder.valueOf(sortOption.substring(index + 1)); } searchSourceBuilder.sort(new FieldSortBuilder(field).order(order)); } } // Generate the actual request to send to ES. SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.types(docType); searchRequest.source(searchSourceBuilder); return elasticSearchClient.search(searchRequest); }
Example 8
Source File: ElasticSearchRestDAOV5.java From conductor with Apache License 2.0 | 5 votes |
@Override public List<EventExecution> getEventExecutions(String event) { try { Expression expression = Expression.fromString("event='" + event + "'"); QueryBuilder queryBuilder = expression.getFilterBuilder(); BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery("*"); BoolQueryBuilder query = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); // Create the searchObjectIdsViaExpression source SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(query); searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC)); // Generate the actual request to send to ES. SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*"); searchRequest.types(EVENT_DOC_TYPE); searchRequest.source(searchSourceBuilder); SearchResponse response = elasticSearchClient.search(searchRequest); return mapEventExecutionsResponse(response); } catch (Exception e) { logger.error("Failed to get executions for event: {}", event, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, e.getMessage(), e); } }
Example 9
Source File: ElasticSearchRestDAOV5.java From conductor with Apache License 2.0 | 5 votes |
@Override public List<Message> getMessages(String queue) { try { Expression expression = Expression.fromString("queue='" + queue + "'"); QueryBuilder queryBuilder = expression.getFilterBuilder(); BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery("*"); BoolQueryBuilder query = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); // Create the searchObjectIdsViaExpression source SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(query); searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC)); // Generate the actual request to send to ES. SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*"); searchRequest.types(MSG_DOC_TYPE); searchRequest.source(searchSourceBuilder); SearchResponse response = elasticSearchClient.search(searchRequest); return mapGetMessagesResponse(response); } catch (Exception e) { logger.error("Failed to get messages for queue: {}", queue, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, e.getMessage(), e); } }
Example 10
Source File: EsSearchRequestBuilder.java From occurrence with Apache License 2.0 | 5 votes |
public static SearchRequest buildSearchRequest( OccurrenceSearchRequest searchRequest, boolean facetsEnabled, String index) { SearchRequest esRequest = new SearchRequest(); esRequest.indices(index); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); esRequest.source(searchSourceBuilder); // size and offset searchSourceBuilder.size(searchRequest.getLimit()); searchSourceBuilder.from((int) searchRequest.getOffset()); // sort if (Strings.isNullOrEmpty(searchRequest.getQ())) { searchSourceBuilder.sort(SortBuilders.fieldSort("year").order(SortOrder.DESC)); searchSourceBuilder.sort(SortBuilders.fieldSort("month").order(SortOrder.ASC)); searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.ASC)); } else { searchSourceBuilder.sort(SortBuilders.scoreSort()); } // group params GroupedParams groupedParams = groupParameters(searchRequest); // add query buildQuery(groupedParams.queryParams, searchRequest.getQ()) .ifPresent(searchSourceBuilder::query); // add aggs buildAggs(searchRequest, groupedParams.postFilterParams, facetsEnabled) .ifPresent(aggsList -> aggsList.forEach(searchSourceBuilder::aggregation)); // post-filter buildPostFilter(groupedParams.postFilterParams).ifPresent(searchSourceBuilder::postFilter); return esRequest; }
Example 11
Source File: QueryParamTranslator.java From jetlinks-community with Apache License 2.0 | 5 votes |
public static SearchSourceBuilder convertSearchSourceBuilder(QueryParam queryParam, ElasticSearchIndexMetadata metadata) { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); if (queryParam.isPaging()) { sourceBuilder.from(queryParam.getPageIndex() * queryParam.getPageSize()); sourceBuilder.size(queryParam.getPageSize()); } for (Sort sort : queryParam.getSorts()) { if (!StringUtils.isEmpty(sort.getName())) { sourceBuilder.sort(sort.getName(), SortOrder.fromString(sort.getOrder())); } } BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery(); Consumer<Term> paramConverter = doNotingParamConverter; if (metadata != null) { paramConverter = t -> { if (StringUtils.isEmpty(t.getColumn())) { return; } PropertyMetadata property = metadata.getProperty(t.getColumn()); if (null != property) { DataType type = property.getValueType(); converter.getOrDefault(type.getId(), defaultDataTypeConverter).accept(type, t); } }; } for (Term term : queryParam.getTerms()) { linkTypeParser.process(term, paramConverter, queryBuilders); } return sourceBuilder.query(queryBuilders); }
Example 12
Source File: HighLevelRestController.java From ProjectStudy with MIT License | 5 votes |
/** * 列表查询 * * @param page * @param rows * @param keyword * @return com.example.common.ResponseBean * @throws * @author wliduo[[email protected]] * @date 2019/8/15 16:01 */ @GetMapping("/book") public ResponseBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, String keyword) throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 分页采用简单的from + size分页,适用数据量小的,了解更多分页方式可自行查阅资料 searchSourceBuilder.from((page - 1) * rows); searchSourceBuilder.size(rows); // 查询条件,只有查询关键字不为空才带查询条件 if (StringUtils.isNoneBlank(keyword)) { QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "desc"); searchSourceBuilder.query(queryBuilder); } // 排序,根据ID倒叙 searchSourceBuilder.sort("id", SortOrder.DESC); // SearchRequest SearchRequest searchRequest = new SearchRequest(); searchRequest.source(searchSourceBuilder); // 查询ES SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); // 获取总数 Long total = hits.getTotalHits().value; // 遍历封装列表对象 List<BookDto> bookDtoList = new ArrayList<>(); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit : searchHits) { bookDtoList.add(JSON.parseObject(searchHit.getSourceAsString(), BookDto.class)); } // 封装Map参数返回 Map<String, Object> result = new HashMap<String, Object>(16); result.put("count", total); result.put("data", bookDtoList); return new ResponseBean(HttpStatus.OK.value(), "查询成功", result); }
Example 13
Source File: Test.java From AsuraFramework with Apache License 2.0 | 4 votes |
public static void main(String[] args) { /* QueryBuilder qb1 = QueryBuilders.matchQuery("a","b"); System.out.println(qb1.toString());*/ String json="{\"query\":{\"match_all\":{}},\"filter\":{\"geo_shape\":{\"geometry\":{\"relation\":\"CONTAINS\",\"shape\":{\"coordinates\":[116.402257,39.914548],\"type\":\"point\"}}}}}"; QueryBuilder qb= QueryBuilders.matchAllQuery(); //System.out.println(qb.toString()); SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder(); searchSourceBuilder.query(qb); // System.out.println(searchSourceBuilder.toString()); ShapeBuilder shapeBuilder= ShapeBuilder.newPoint(new Coordinate(116.402257,39.914548)); QueryBuilder qb2= QueryBuilders.geoShapeQuery("geometry",shapeBuilder, ShapeRelation.CONTAINS); System.out.println(qb2.toString()); //searchSourceBuilder.postFilter(qb2); QueryBuilder qb3= QueryBuilders.boolQuery().must(qb).filter(qb2); searchSourceBuilder.query(qb3); System.out.println(qb3.toString()); System.out.println(searchSourceBuilder.toString()); QueryBuilder qb4= QueryBuilders.boolQuery().must(qb).must(qb2); System.out.println(qb4.toString()); SortBuilder sort= SortBuilders.geoDistanceSort("pin.location") .point(40, -70). unit(DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString())).order(SortOrder.DESC); /* QueryBuilder qb5 = QueryBuilders.geoDistanceQuery("pin.location") .point(40, -70) .distance(400, DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString())) .geoDistance(GeoDistance.ARC); System.out.println(qb5.toString()); */ searchSourceBuilder.sort(sort); System.out.println(searchSourceBuilder.toString()); //QueryBuilder qb3=QueryBuilders.filteredQuery(null,qb2); //QueryBuilder qb4=QueryBuilders.filteredQuery(qb,qb2); //searchSourceBuilder.query(qb3.toString()); // searchSourceBuilder.query(qb4); // System.out.println(qb4.toString()); //System.out.println(searchSourceBuilder.toString()); // System.out.println(qb.toString()); /* QueryBuilder qb2 = QueryBuilders.geoBoundingBoxQuery("pin.location") .topLeft(40.73, -74.1) .bottomRight(40.717, -73.99); //String strstr= JSON.toJSONString(qb2); System.out.println(qb2.toString()); System.out.println("1111111");*/ }
Example 14
Source File: EsUtil.java From java-study with Apache License 2.0 | 4 votes |
/** * @return boolean * @Author pancm * @Description 根据条件查询 * @Date 2019/3/21 * @Param [] **/ public static List<Map<String, Object>> query(String index, String type, EsQueryCondition esQueryCondition, QueryBuilder... queryBuilders) throws IOException { if (index == null || type == null) { return null; } List<Map<String, Object>> list = new ArrayList<>(); try { // 查询指定的索引库 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); if (esQueryCondition != null) { Integer form = esQueryCondition.getIndex(); Integer pagesize = esQueryCondition.getPagesize(); if (form != null && form > 0 && pagesize != null && pagesize > 0) { form = (form - 1) * pagesize; pagesize = form + pagesize; // 设置起止和结束 sourceBuilder.from(form); sourceBuilder.size(pagesize); } String routing = esQueryCondition.getRouting(); if (routing != null && routing.length() > 0) { // 设置路由 searchRequest.routing(routing); } // 设置索引库表达式 searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); //设置排序 String order = esQueryCondition.getOrder(); if (order != null) { String[] orderField = esQueryCondition.getOrderField(); SortOrder order2 = order.equals(SortOrder.DESC) ? SortOrder.DESC : SortOrder.ASC; //如果设置了排序字段则用排序的字段进行排序,否则就默认排序 if (orderField != null) { for (String field : orderField) { sourceBuilder.sort(new FieldSortBuilder(field).order(order2)); } } else { sourceBuilder.sort(new ScoreSortBuilder().order(order2)); } } String[] includeFields = esQueryCondition.getIncludeFields(); String[] excludeFields = esQueryCondition.getExcludeFields(); if (includeFields != null && includeFields.length > 0 && excludeFields != null && excludeFields.length > 0) { sourceBuilder.fetchSource(includeFields, excludeFields); } sourceBuilder.fetchSource(esQueryCondition.isCloseSource()); } //设置条件 if (queryBuilders != null) { for (QueryBuilder queryBuilder : queryBuilders) { sourceBuilder.query(queryBuilder); } } searchRequest.source(sourceBuilder); // 同步查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); if(queryBuilders != null|| (esQueryCondition != null && esQueryCondition.isQueryData())){ // 结果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); list.add(map); }); } if(esQueryCondition != null && esQueryCondition.isNeedTotal()){ Map<String, Object> mapTotal = new HashMap<>(); mapTotal.put("total", searchResponse.getHits().getTotalHits()); list.add(mapTotal); } } finally { if (isAutoClose) { close(); } } return list; }
Example 15
Source File: LogConsoleServiceImpl.java From super-cloudops with Apache License 2.0 | 4 votes |
/** * Query log from ES documents * * @param index * @param startTime * @param endTime * @param from * @param limit * @param queryList * @param level * @return * @throws Exception */ protected List<Log> queryLogFromESDocuments(String index, Long startTime, Long endTime, Integer from, Integer limit, List<Querycriteria> queryList, Integer level) throws Exception { // create bool query BoolQueryBuilder boolQueryBuilder = boolQuery(); // fix key match if (!CollectionUtils.isEmpty(queryList)) { queryList.forEach(u -> { String query = u.getValue().trim(); if (StringUtils.isEmpty(query)) { return; } // con = QueryParser.escape(con); query = "\"" + query + "\"";// for Special characters if (u.isEnable()) {// enable? must match // boolQueryBuilder.must(fuzzyQuery(KEY_DEFAULT_MSG, // con)); boolQueryBuilder.must(queryStringQuery(query).field(KEY_DEFAULT_MSG)); } else {// not enbale ? must not match // boolQueryBuilder.mustNot(fuzzyQuery(KEY_DEFAULT_MSG, // con)); boolQueryBuilder.mustNot(queryStringQuery(query).field(KEY_DEFAULT_MSG)); } }); } // fix log level match if (!Objects.isNull(level) && level > 0) { BoolQueryBuilder boolQueryBuilder1 = boolQuery(); for (int i = level - 1; i < LOG_LEVEL.size(); i++) { boolQueryBuilder1.should(matchQuery(KEY_DEFAULT_MSG, LOG_LEVEL.get(i))); } boolQueryBuilder.must(boolQueryBuilder1); } // fix time range if (null != startTime && null != endTime && (endTime != 0 || startTime != 0)) { RangeQueryBuilder rqb = rangeQuery("@timestamp").timeZone(DateTimeZone.UTC.toString()); if (null != startTime) { rqb.gte(DateUtils2.timeToUTC(startTime)); } if (null != endTime) { rqb.lt(DateUtils2.timeToUTC(endTime)); } boolQueryBuilder.filter(rqb); } SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(boolQueryBuilder); sourceBuilder.from(Objects.isNull(from) ? 0 : from);// from sourceBuilder.size(Objects.isNull(limit) ? 100 : limit);// limit // order by timestamp desc sourceBuilder.sort(new FieldSortBuilder("@timestamp").order(SortOrder.DESC)); SearchRequest searchRequest = new SearchRequest(index); // searchRequest.types("doc"); // useful searchRequest.source(sourceBuilder); List<Log> logList = logHandler.findAll(searchRequest); return logList; }
Example 16
Source File: PostServiceImpl.java From SENS with GNU General Public License v3.0 | 4 votes |
@Override public Page<Post> findPostsByEs(Map<String, Object> criteria, Page<Post> page) { //search request SearchRequest searchRequest = new SearchRequest("blog"); //search builder SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery(); for (String key : criteria.keySet()) { booleanQueryBuilder.must(QueryBuilders.matchQuery(key, criteria.get(key))); } sourceBuilder.query(booleanQueryBuilder); sourceBuilder.from(Integer.parseInt(String.valueOf((page.getCurrent() - 1) * page.getSize()))); sourceBuilder.size(Integer.parseInt(String.valueOf(page.getSize()))); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //sort sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); //highlight HighlightBuilder highlightBuilder = new HighlightBuilder(); HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("postTitle"); highlightTitle.preTags("<span class=\"highlight\">"); highlightTitle.postTags("</span>"); highlightBuilder.field(highlightTitle); sourceBuilder.highlighter(highlightBuilder); // add builder into request searchRequest.indices("blog"); searchRequest.source(sourceBuilder); //response SearchResponse searchResponse = null; List<Post> postList = new ArrayList<>(); try { searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); //search hits SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits(); SearchHit[] searchHits = hits.getHits(); page.setTotal((int) totalHits); for (SearchHit hit : searchHits) { String str = hit.getSourceAsString(); Post esPost = JSONObject.parseObject(str, Post.class); //高亮 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField highlight = highlightFields.get("postTitle"); if (highlight != null) { Text[] fragments = highlight.fragments(); String fragmentString = fragments[0].string(); esPost.setPostTitle(fragmentString); } postList.add(esPost); } } catch (Exception e) { e.printStackTrace(); log.error("ES未开启: {}", e); } return page.setRecords(postList); }
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: 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 19
Source File: ElasticsearchSearchDao.java From metron with Apache License 2.0 | 4 votes |
/** * Builds an Elasticsearch search request. * @param searchRequest The Metron search request. * @param queryBuilder * @return An Elasticsearch search request. */ private org.elasticsearch.action.search.SearchRequest buildSearchRequest( SearchRequest searchRequest, QueryBuilder queryBuilder) throws InvalidSearchException { if (LOG.isDebugEnabled()) { LOG.debug("Got search request; request={}", ElasticsearchUtils.toJSON(searchRequest).orElse("???")); } SearchSourceBuilder searchBuilder = new SearchSourceBuilder() .size(searchRequest.getSize()) .from(searchRequest.getFrom()) .query(queryBuilder) .trackScores(true); List<String> fields = searchRequest.getFields(); // column metadata needed to understand the type of each sort field Map<String, FieldType> meta; try { meta = columnMetadataDao.getColumnMetadata(searchRequest.getIndices()); } catch(IOException e) { throw new InvalidSearchException("Unable to get column metadata", e); } // handle sort fields for(SortField sortField : searchRequest.getSort()) { // what type is the sort field? FieldType sortFieldType = meta.getOrDefault(sortField.getField(), FieldType.OTHER); // sort order - if ascending missing values sorted last. otherwise, missing values sorted first org.elasticsearch.search.sort.SortOrder sortOrder = getElasticsearchSortOrder(sortField.getSortOrder()); String missingSortOrder; if(sortOrder == org.elasticsearch.search.sort.SortOrder.DESC) { missingSortOrder = SORT_MISSING_LAST; } else { missingSortOrder = SORT_MISSING_FIRST; } // sort by the field - missing fields always last FieldSortBuilder sortBy = new FieldSortBuilder(sortField.getField()) .order(sortOrder) .missing(missingSortOrder) .unmappedType(sortFieldType.getFieldType()); searchBuilder.sort(sortBy); } // handle search fields if (fields != null) { searchBuilder.fetchSource("*", null); } else { searchBuilder.fetchSource(true); } List<String> facetFields = searchRequest.getFacetFields(); // handle facet fields if (facetFields != null) { // https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html for(String field : facetFields) { String name = getFacetAggregationName(field); TermsAggregationBuilder terms = AggregationBuilders.terms( name).field(field); // new TermsBuilder(name).field(field); searchBuilder.aggregation(terms); } } // return the search request String[] indices = wildcardIndices(searchRequest.getIndices()); if (LOG.isDebugEnabled()) { LOG.debug("Built Elasticsearch request; indices={}, request={}", indices, searchBuilder.toString()); } return new org.elasticsearch.action.search.SearchRequest() .indices(indices) .source(searchBuilder); }
Example 20
Source File: TraceQueryEsDAO.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().totalHits); 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; }