Java Code Examples for org.elasticsearch.search.builder.SearchSourceBuilder#size()

The following examples show how to use org.elasticsearch.search.builder.SearchSourceBuilder#size() . 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 vote down vote up
@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 2
Source File: MultiMatchQueryApidMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.multiMatchQuery("token","retdesc", "reqpkg");//查询有问题??

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(5);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example 3
Source File: ExistApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        ExistsQueryBuilder matchQueryBuilder = QueryBuilders.existsQuery("retcode");//查询某个字段存在的记录

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(1);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example 4
Source File: NumberRangeApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        RangeQueryBuilder matchQueryBuilder = QueryBuilders.rangeQuery("utm").from(9,false).to(11,false);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(5);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example 5
Source File: ProfileThreadSnapshotQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
protected int querySequenceWithAgg(AbstractAggregationBuilder aggregationBuilder, String segmentId, long start, long end) throws IOException {
    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.DUMP_TIME).gte(start).lte(end));
    sourceBuilder.size(0);

    sourceBuilder.aggregation(aggregationBuilder);
    SearchResponse response = getClient().search(ProfileThreadSnapshotRecord.INDEX_NAME, sourceBuilder);
    NumericMetricsAggregation.SingleValue agg = response.getAggregations().get(ProfileThreadSnapshotRecord.SEQUENCE);

    return (int) agg.value();
}
 
Example 6
Source File: ProfileThreadSnapshotQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: CountAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cmd", "get_fee_transfer_content_data");
        ValueCountAggregationBuilder aggregationBuilder = AggregationBuilders.count("cmd_count").field("cmd");

        SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchSourceBuilder.size(0);//不返回具体业务数据,只需要聚合结果
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

        //统计结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
            System.out.println(((ParsedValueCount)(each.getValue())).getValue());
        }

    }finally{
        HighLevelClient.close();
    }
}
 
Example 8
Source File: ScrollSpout.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
@Override
protected void populateBuffer() {
    if (hasFinished) {
        Utils.sleep(10);
        return;
    }

    // initial request
    if (scrollId == null) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.size(maxURLsPerBucket * maxBucketNum);
        SearchRequest searchRequest = new SearchRequest(indexName);
        searchRequest.source(searchSourceBuilder);
        searchRequest.scroll(TimeValue.timeValueMinutes(5L));

        if (shardID != -1) {
            searchRequest.preference("_shards:" + shardID);
        }

        isInQuery.set(true);
        client.searchAsync(searchRequest, RequestOptions.DEFAULT, this);

        // dump query to log
        LOG.debug("{} ES query {}", logIdprefix, searchRequest.toString());
        return;
    }

    SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
    scrollRequest.scroll(TimeValue.timeValueMinutes(5L));

    isInQuery.set(true);
    client.scrollAsync(scrollRequest, RequestOptions.DEFAULT, this);
    // dump query to log
    LOG.debug("{} ES query {}", logIdprefix, scrollRequest.toString());
}
 
Example 9
Source File: MaxMinAggregationMain.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
        RestHighLevelClient client = HighLevelClient.getInstance();
        try{
//            MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("utm").field("utm");
            MinAggregationBuilder aggregationBuilder = AggregationBuilders.min("utm").field("utm");

            SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index
            searchRequest.types("log");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(aggregationBuilder);
            searchSourceBuilder.size(0);//设置不需要文档数据,只需要返回聚合结果
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            //统计结果
            Aggregations aggregations = searchResponse.getAggregations();
            Map<String, Aggregation> aggregationMap = aggregations.asMap();
            for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){
//                System.out.println(((ParsedMax)(each.getValue())).getValue());
                System.out.println(((ParsedMin)(each.getValue())).getValue());
            }

        }finally{
            HighLevelClient.close();
        }
    }
 
Example 10
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public List<Call.CallDetail> loadInstanceRelationDetectedAtServerSide(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, ServiceInstanceRelationServerSideMetrics.INDEX_NAME, DetectPoint.SERVER);
}
 
Example 11
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: MetadataQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: ElasticSourceTask.java    From kafka-connect-elasticsearch-source with Apache License 2.0 4 votes vote down vote up
private void executeScroll(String index, String lastValue, List<SourceRecord> results) {
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.scroll(TimeValue.timeValueMinutes(1L));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(
            rangeQuery(incrementingField)
                    .from(lastValue, last.get(index)== null)
    ).sort(incrementingField, SortOrder.ASC); //TODO configure custom query
    searchSourceBuilder.size(1000);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    String scrollId = null;
    try {
        for (int i = 0; i < maxConnectionAttempts; ++i) {
            try {
                searchResponse = es.getClient().search(searchRequest);
                break;
            } catch (IOException e) {
                logger.error("error in scroll");
                Thread.sleep(connectionRetryBackoff);
            }
        }
        if (searchResponse == null) {
            throw new RuntimeException("connection failed");
        }
        scrollId = searchResponse.getScrollId();
        SearchHit[] searchHits = parseSearchResult(index, lastValue, results, searchResponse, scrollId);

        while (!stopping.get() && searchHits != null && searchHits.length > 0 && results.size() < size) {
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(TimeValue.timeValueMinutes(1L));
            searchResponse = es.getClient().searchScroll(scrollRequest);
            scrollId = searchResponse.getScrollId();
            searchHits = parseSearchResult(index, lastValue, results, searchResponse, scrollId);
        }
    } catch (Throwable t) {
        logger.error("error", t);
    } finally {
        closeScrollQuietly(scrollId);
    }


}
 
Example 15
Source File: ZipkinTraceQueryEsDAO.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@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 16
Source File: ElasticsearchHelperService.java    From xmfcn-spring-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * 组装搜索条件
 *
 * @param EsModel es
 *                keywords
 *                highlights
 * @return
 */
public Search searchCondition(EsModel es) {
    Search search = null;
    if (es == null) {
        return search;
    }
    JSONObject keywords = es.getKeyWord();
    JSONObject hightWord = es.getHightWord();
    EsPage esPage = es.getEsPage();
    String indexName = es.getIndex();
    String type = es.getType();
    boolean isVague = es.isVague();//是否模糊搜索
    boolean isAccuracySort = es.isAccuracySort();//是否根据精确度排序
    boolean isAndSearch = es.isAndSearch();//是否And搜索
    if (StringUtil.isBlank(indexName)) {
        return search;
    }
    if (StringUtil.isBlank(type)) {
        return search;
    }
    int pageNo = 1;
    int pageSize = 10;
    int startIndex = 0;
    if (esPage != null) {
        pageNo = esPage.getPageNo();
        pageSize = esPage.getPageSize();
        startIndex = esPage.getStartIndex();
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    AddKeyWords(keywords, searchSourceBuilder);
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    AddHighLigh(keywords, highlightBuilder);
    searchSourceBuilder.explain(isAccuracySort);
    searchSourceBuilder.from(startIndex);
    searchSourceBuilder.size(pageSize);
    Sort sort = new Sort("time", Sort.Sorting.DESC);
    searchSourceBuilder.highlighter(highlightBuilder);
    search = new Search.Builder(searchSourceBuilder.toString())
            .addIndex(indexName).addType(type).addSort(sort).build();

    return search;
}
 
Example 17
Source File: LogConsoleServiceImpl.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
/**
 * 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 18
Source File: PostServiceImpl.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
@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 19
Source File: LogQueryEs7DAO.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public Logs queryLogs(String metricName, int serviceId, int serviceInstanceId, String endpointId, String traceId,
                      LogState state, String stateCode, Pagination paging, int from, int limit, long startSecondTB,
                      long endSecondTB) 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(Record.TIME_BUCKET).gte(startSecondTB).lte(endSecondTB));
    }

    if (serviceId != Const.NONE) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(AbstractLogRecord.SERVICE_ID, serviceId));
    }
    if (serviceInstanceId != Const.NONE) {
        boolQueryBuilder.must()
                        .add(QueryBuilders.termQuery(AbstractLogRecord.SERVICE_INSTANCE_ID, serviceInstanceId));
    }
    if (StringUtil.isNotEmpty(endpointId)) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(AbstractLogRecord.ENDPOINT_ID, endpointId));
    }
    if (!Strings.isNullOrEmpty(stateCode)) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(AbstractLogRecord.STATUS_CODE, stateCode));
    }
    if (!Strings.isNullOrEmpty(traceId)) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(TRACE_ID, traceId));
    }
    if (LogState.ERROR.equals(state)) {
        boolQueryBuilder.must()
                        .add(
                            QueryBuilders.termQuery(AbstractLogRecord.IS_ERROR, BooleanUtils.booleanToValue(true)));
    } else if (LogState.SUCCESS.equals(state)) {
        boolQueryBuilder.must()
                        .add(QueryBuilders.termQuery(
                            AbstractLogRecord.IS_ERROR,
                            BooleanUtils.booleanToValue(false)
                        ));
    }

    sourceBuilder.size(limit);
    sourceBuilder.from(from);

    SearchResponse response = getClient().search(metricName, sourceBuilder);

    Logs logs = new Logs();
    logs.setTotal((int) response.getHits().getTotalHits().value);

    for (SearchHit searchHit : response.getHits().getHits()) {
        Log log = new Log();
        log.setServiceId((String) searchHit.getSourceAsMap().get(AbstractLogRecord.SERVICE_ID));
        log.setServiceInstanceId((String) searchHit.getSourceAsMap().get(AbstractLogRecord.SERVICE_INSTANCE_ID));
        log.setEndpointId((String) searchHit.getSourceAsMap().get(AbstractLogRecord.ENDPOINT_ID));
        log.setEndpointName((String) searchHit.getSourceAsMap().get(AbstractLogRecord.ENDPOINT_NAME));
        log.setError(BooleanUtils.valueToBoolean(((Number) searchHit.getSourceAsMap()
                                                                    .get(AbstractLogRecord.IS_ERROR)).intValue()));
        log.setStatusCode((String) searchHit.getSourceAsMap().get(AbstractLogRecord.STATUS_CODE));
        log.setContentType(ContentType.instanceOf(((Number) searchHit.getSourceAsMap()
                                                                     .get(
                                                                         AbstractLogRecord.CONTENT_TYPE)).intValue()));
        log.setContent((String) searchHit.getSourceAsMap().get(AbstractLogRecord.CONTENT));

        logs.getLogs().add(log);
    }

    return logs;
}
 
Example 20
Source File: EsUtil.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * @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;
}