Search in sources :

Example 1 with RelatedContentHelper

use of org.codelibs.fess.helper.RelatedContentHelper in project fess by codelibs.

the class JsonApiManager method processSearchRequest.

protected void processSearchRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final RelatedQueryHelper relatedQueryHelper = ComponentUtil.getRelatedQueryHelper();
    final RelatedContentHelper relatedContentHelper = ComponentUtil.getRelatedContentHelper();
    int status = 0;
    Exception err = null;
    String query = null;
    // TODO replace response stream
    final StringBuilder buf = new StringBuilder(1000);
    request.setAttribute(Constants.SEARCH_LOG_ACCESS_TYPE, Constants.SEARCH_LOG_ACCESS_TYPE_JSON);
    try {
        final SearchRenderData data = new SearchRenderData();
        final JsonRequestParams params = new JsonRequestParams(request, fessConfig);
        query = params.getQuery();
        searchHelper.search(params, data, OptionalThing.empty());
        final String execTime = data.getExecTime();
        final String queryTime = Long.toString(data.getQueryTime());
        final String pageSize = Integer.toString(data.getPageSize());
        final String currentPageNumber = Integer.toString(data.getCurrentPageNumber());
        final String allRecordCount = Long.toString(data.getAllRecordCount());
        final String allRecordCountRelation = data.getAllRecordCountRelation();
        final String allPageCount = Integer.toString(data.getAllPageCount());
        final List<Map<String, Object>> documentItems = data.getDocumentItems();
        final FacetResponse facetResponse = data.getFacetResponse();
        final String queryId = data.getQueryId();
        final String highlightParams = data.getAppendHighlightParams();
        final boolean nextPage = data.isExistNextPage();
        final boolean prevPage = data.isExistPrevPage();
        final long startRecordNumber = data.getCurrentStartRecordNumber();
        final long endRecordNumber = data.getCurrentEndRecordNumber();
        final List<String> pageNumbers = data.getPageNumberList();
        final boolean partial = data.isPartialResults();
        final String searchQuery = data.getSearchQuery();
        final long requestedTime = data.getRequestedTime();
        buf.append("\"q\":");
        buf.append(escapeJson(query));
        buf.append(",\"query_id\":");
        buf.append(escapeJson(queryId));
        buf.append(",\"exec_time\":");
        buf.append(execTime);
        buf.append(",\"query_time\":");
        buf.append(queryTime);
        buf.append(',');
        buf.append("\"page_size\":");
        buf.append(pageSize);
        buf.append(',');
        buf.append("\"page_number\":");
        buf.append(currentPageNumber);
        buf.append(',');
        buf.append("\"record_count\":");
        buf.append(allRecordCount);
        buf.append(',');
        buf.append("\"record_count_relation\":");
        buf.append(escapeJson(allRecordCountRelation));
        buf.append(',');
        buf.append("\"page_count\":");
        buf.append(allPageCount);
        buf.append(",\"highlight_params\":");
        buf.append(escapeJson(highlightParams));
        buf.append(",\"next_page\":");
        buf.append(escapeJson(nextPage));
        buf.append(",\"prev_page\":");
        buf.append(escapeJson(prevPage));
        buf.append(",\"start_record_number\":");
        buf.append(startRecordNumber);
        buf.append(",\"end_record_number\":");
        buf.append(escapeJson(endRecordNumber));
        buf.append(",\"page_numbers\":");
        buf.append(escapeJson(pageNumbers));
        buf.append(",\"partial\":");
        buf.append(escapeJson(partial));
        buf.append(",\"search_query\":");
        buf.append(escapeJson(searchQuery));
        buf.append(",\"requested_time\":");
        buf.append(requestedTime);
        final String[] relatedQueries = relatedQueryHelper.getRelatedQueries(params.getQuery());
        buf.append(",\"related_query\":");
        buf.append(escapeJson(relatedQueries));
        final String[] relatedContents = relatedContentHelper.getRelatedContents(params.getQuery());
        buf.append(",\"related_contents\":");
        buf.append(escapeJson(relatedContents));
        buf.append(',');
        buf.append("\"result\":[");
        if (!documentItems.isEmpty()) {
            boolean first1 = true;
            for (final Map<String, Object> document : documentItems) {
                if (!first1) {
                    buf.append(',');
                } else {
                    first1 = false;
                }
                buf.append('{');
                boolean first2 = true;
                for (final Map.Entry<String, Object> entry : document.entrySet()) {
                    final String name = entry.getKey();
                    if (StringUtil.isNotBlank(name) && entry.getValue() != null && ComponentUtil.getQueryHelper().isApiResponseField(name)) {
                        if (!first2) {
                            buf.append(',');
                        } else {
                            first2 = false;
                        }
                        buf.append(escapeJson(name));
                        buf.append(':');
                        buf.append(escapeJson(entry.getValue()));
                    }
                }
                buf.append('}');
            }
        }
        buf.append(']');
        if (facetResponse != null && facetResponse.hasFacetResponse()) {
            // facet field
            buf.append(',');
            buf.append("\"facet_field\":[");
            if (facetResponse.getFieldList() != null) {
                boolean first1 = true;
                for (final Field field : facetResponse.getFieldList()) {
                    if (!first1) {
                        buf.append(',');
                    } else {
                        first1 = false;
                    }
                    buf.append("{\"name\":");
                    buf.append(escapeJson(field.getName()));
                    buf.append(",\"result\":[");
                    boolean first2 = true;
                    for (final Map.Entry<String, Long> entry : field.getValueCountMap().entrySet()) {
                        if (!first2) {
                            buf.append(',');
                        } else {
                            first2 = false;
                        }
                        buf.append("{\"value\":");
                        buf.append(escapeJson(entry.getKey()));
                        buf.append(",\"count\":");
                        buf.append(entry.getValue());
                        buf.append('}');
                    }
                    buf.append(']');
                    buf.append('}');
                }
            }
            buf.append(']');
            // facet q
            buf.append(',');
            buf.append("\"facet_query\":[");
            if (facetResponse.getQueryCountMap() != null) {
                boolean first1 = true;
                for (final Map.Entry<String, Long> entry : facetResponse.getQueryCountMap().entrySet()) {
                    if (!first1) {
                        buf.append(',');
                    } else {
                        first1 = false;
                    }
                    buf.append("{\"value\":");
                    buf.append(escapeJson(entry.getKey()));
                    buf.append(",\"count\":");
                    buf.append(entry.getValue());
                    buf.append('}');
                }
            }
            buf.append(']');
        }
    } catch (final Exception e) {
        status = 1;
        err = e;
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a search request.", e);
        }
    }
    writeJsonResponse(status, buf.toString(), err);
}
Also used : RelatedContentHelper(org.codelibs.fess.helper.RelatedContentHelper) SearchHelper(org.codelibs.fess.helper.SearchHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) RelatedQueryHelper(org.codelibs.fess.helper.RelatedQueryHelper) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) Field(org.codelibs.fess.util.FacetResponse.Field) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) FacetResponse(org.codelibs.fess.util.FacetResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 IORuntimeException (org.codelibs.core.exception.IORuntimeException)1 SearchRenderData (org.codelibs.fess.entity.SearchRenderData)1 WebApiException (org.codelibs.fess.exception.WebApiException)1 RelatedContentHelper (org.codelibs.fess.helper.RelatedContentHelper)1 RelatedQueryHelper (org.codelibs.fess.helper.RelatedQueryHelper)1 SearchHelper (org.codelibs.fess.helper.SearchHelper)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 FacetResponse (org.codelibs.fess.util.FacetResponse)1 Field (org.codelibs.fess.util.FacetResponse.Field)1