Search in sources :

Example 1 with TermOrBuilder

use of io.zulia.message.ZuliaBase.TermOrBuilder in project zuliasearch by zuliaio.

the class QueryController method getStandardResponse.

private String getStandardResponse(QueryResponse qr, boolean strict, boolean cursor, boolean truncate) throws InvalidProtocolBufferException {
    StringBuilder responseBuilder = new StringBuilder();
    responseBuilder.append("{");
    responseBuilder.append("\"totalHits\": ");
    responseBuilder.append(qr.getTotalHits());
    if (cursor) {
        responseBuilder.append(",");
        responseBuilder.append("\"cursor\": \"");
        responseBuilder.append(CursorHelper.getUniqueSortedCursor(qr.getLastResult()));
        responseBuilder.append("\"");
    }
    if (!qr.getAnalysisResultList().isEmpty()) {
        responseBuilder.append(",");
        responseBuilder.append("\"analysis\": [");
        boolean first = true;
        for (AnalysisResult analysisResult : qr.getAnalysisResultList()) {
            if (first) {
                first = false;
            } else {
                responseBuilder.append(",");
            }
            responseBuilder.append("{");
            responseBuilder.append("\"field\": \"");
            responseBuilder.append(analysisResult.getAnalysisRequest().getField());
            responseBuilder.append("\"");
            responseBuilder.append(",");
            responseBuilder.append("\"terms\": [");
            JsonFormat.Printer printer = JsonFormat.printer();
            boolean firstInner = true;
            for (TermOrBuilder term : analysisResult.getTermsOrBuilderList()) {
                if (firstInner) {
                    firstInner = false;
                } else {
                    responseBuilder.append(",");
                }
                responseBuilder.append(printer.print(term));
            }
            responseBuilder.append("]");
            responseBuilder.append("}");
        }
        responseBuilder.append("]");
    }
    if (!qr.getResultsList().isEmpty()) {
        JsonFormat.Printer printer = JsonFormat.printer();
        responseBuilder.append(",");
        responseBuilder.append("\"results\": [");
        boolean first = true;
        for (ScoredResult sr : qr.getResultsList()) {
            if (first) {
                first = false;
            } else {
                responseBuilder.append(",");
            }
            responseBuilder.append("{");
            responseBuilder.append("\"id\": ");
            responseBuilder.append("\"").append(sr.getUniqueId()).append("\"");
            responseBuilder.append(",");
            if (!Double.isNaN(sr.getScore())) {
                responseBuilder.append("\"score\": ");
                responseBuilder.append(sr.getScore());
                responseBuilder.append(",");
            }
            responseBuilder.append("\"indexName\": ");
            responseBuilder.append("\"").append(sr.getIndexName()).append("\"");
            if (sr.hasResultDocument()) {
                responseBuilder.append(",");
                Document document = ResultHelper.getDocumentFromResultDocument(sr.getResultDocument());
                if (document != null) {
                    responseBuilder.append("\"document\": ");
                    if (strict) {
                        responseBuilder.append(document.toJson());
                    } else {
                        if (truncate) {
                            truncateDocumentValues(document);
                        }
                        responseBuilder.append(document.toJson(JsonWriterSettings.builder().indent(true).build()));
                    }
                }
            }
            if (sr.getHighlightResultCount() > 0) {
                responseBuilder.append(",");
                responseBuilder.append("\"highlights\": [");
                boolean firstHighlightResult = true;
                for (HighlightResult hr : sr.getHighlightResultList()) {
                    if (firstHighlightResult) {
                        firstHighlightResult = false;
                    } else {
                        responseBuilder.append(",");
                    }
                    responseBuilder.append(printer.print(hr));
                }
                responseBuilder.append("]");
            }
            if (sr.getAnalysisResultCount() > 0) {
                responseBuilder.append(",");
                responseBuilder.append("\"analysis\": [");
                boolean firstAnalysisResult = true;
                for (AnalysisResult ar : sr.getAnalysisResultList()) {
                    if (firstAnalysisResult) {
                        firstAnalysisResult = false;
                    } else {
                        responseBuilder.append(",");
                    }
                    responseBuilder.append(printer.print(ar));
                }
                responseBuilder.append("]");
            }
            responseBuilder.append("}");
        }
        responseBuilder.append("]");
    }
    if (!qr.getFacetGroupList().isEmpty()) {
        responseBuilder.append(",");
        responseBuilder.append("\"facets\": [");
        boolean first = true;
        for (FacetGroup facetGroup : qr.getFacetGroupList()) {
            if (first) {
                first = false;
            } else {
                responseBuilder.append(",");
            }
            responseBuilder.append("{");
            responseBuilder.append("\"field\": \"");
            responseBuilder.append(facetGroup.getCountRequest().getFacetField().getLabel());
            responseBuilder.append("\"");
            if (facetGroup.getPossibleMissing()) {
                responseBuilder.append(",");
                responseBuilder.append("\"maxPossibleMissing\": ");
                responseBuilder.append(facetGroup.getMaxValuePossibleMissing());
            }
            responseBuilder.append(",");
            responseBuilder.append("\"values\": [");
            JsonFormat.Printer printer = JsonFormat.printer();
            boolean firstInner = true;
            for (FacetCount facetCount : facetGroup.getFacetCountList()) {
                if (firstInner) {
                    firstInner = false;
                } else {
                    responseBuilder.append(",");
                }
                responseBuilder.append(printer.print(facetCount));
            }
            responseBuilder.append("]");
            responseBuilder.append("}");
        }
        responseBuilder.append("]");
    }
    responseBuilder.append("}");
    return responseBuilder.toString();
}
Also used : JsonFormat(com.google.protobuf.util.JsonFormat) Document(org.bson.Document) TermOrBuilder(io.zulia.message.ZuliaBase.TermOrBuilder)

Aggregations

JsonFormat (com.google.protobuf.util.JsonFormat)1 TermOrBuilder (io.zulia.message.ZuliaBase.TermOrBuilder)1 Document (org.bson.Document)1