Search in sources :

Example 1 with SearchHelper

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

the class JsonApiManager method processFavoriteRequest.

protected void processFavoriteRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    if (!ComponentUtil.getFessConfig().isUserFavorite()) {
        writeJsonResponse(9, null, "Unsupported operation.");
        return;
    }
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
    final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
    final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    try {
        final String docId = request.getParameter("docId");
        final String queryId = request.getParameter("queryId");
        final String[] docIds = userInfoHelper.getResultDocIds(URLDecoder.decode(queryId, Constants.UTF_8));
        if (docIds == null) {
            throw new WebApiException(6, "No searched urls.");
        }
        searchHelper.getDocumentByDocId(docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, OptionalThing.empty()).ifPresent(doc -> {
            final String favoriteUrl = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
            final String userCode = userInfoHelper.getUserCode();
            if (StringUtil.isBlank(userCode)) {
                throw new WebApiException(2, "No user session.");
            }
            if (StringUtil.isBlank(favoriteUrl)) {
                throw new WebApiException(2, "URL is null.");
            }
            boolean found = false;
            for (final String id : docIds) {
                if (docId.equals(id)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new WebApiException(5, "Not found: " + favoriteUrl);
            }
            if (!favoriteLogService.addUrl(userCode, (userInfo, favoriteLog) -> {
                favoriteLog.setUserInfoId(userInfo.getId());
                favoriteLog.setUrl(favoriteUrl);
                favoriteLog.setDocId(docId);
                favoriteLog.setQueryId(queryId);
                favoriteLog.setCreatedAt(systemHelper.getCurrentTimeAsLocalDateTime());
            })) {
                throw new WebApiException(4, "Failed to add url: " + favoriteUrl);
            }
            final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
            searchHelper.update(id, builder -> {
                final Script script = ComponentUtil.getLanguageHelper().createScript(doc, "ctx._source." + fessConfig.getIndexFieldFavoriteCount() + "+=1");
                builder.setScript(script);
                final Map<String, Object> upsertMap = new HashMap<>();
                upsertMap.put(fessConfig.getIndexFieldFavoriteCount(), 1);
                builder.setUpsert(upsertMap);
                builder.setRefreshPolicy(Constants.TRUE);
            });
            writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
        }).orElse(() -> {
            throw new WebApiException(6, "Not found: " + docId);
        });
    } catch (final Exception e) {
        int status;
        if (e instanceof WebApiException) {
            status = ((WebApiException) e).getStatusCode();
        } else {
            status = 1;
        }
        writeJsonResponse(status, null, e);
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a favorite request.", e);
        }
    }
}
Also used : Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) IORuntimeException(org.codelibs.core.exception.IORuntimeException) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) URLDecoder(java.net.URLDecoder) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) ServletException(javax.servlet.ServletException) OptionalThing(org.dbflute.optional.OptionalThing) HashMap(java.util.HashMap) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) WebApiException(org.codelibs.fess.exception.WebApiException) RelatedQueryHelper(org.codelibs.fess.helper.RelatedQueryHelper) ArrayList(java.util.ArrayList) GeoInfo(org.codelibs.fess.entity.GeoInfo) HttpServletRequest(javax.servlet.http.HttpServletRequest) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) PingResponse(org.codelibs.fess.entity.PingResponse) SearchRequestParams(org.codelibs.fess.entity.SearchRequestParams) Locale(java.util.Locale) PopularWordHelper(org.codelibs.fess.helper.PopularWordHelper) Map(java.util.Map) FacetInfo(org.codelibs.fess.entity.FacetInfo) SearchHelper(org.codelibs.fess.helper.SearchHelper) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) Field(org.codelibs.fess.util.FacetResponse.Field) Script(org.opensearch.script.Script) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringUtil(org.codelibs.core.lang.StringUtil) RelatedContentHelper(org.codelibs.fess.helper.RelatedContentHelper) IOException(java.io.IOException) FacetResponse(org.codelibs.fess.util.FacetResponse) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SystemHelper(org.codelibs.fess.helper.SystemHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) PostConstruct(javax.annotation.PostConstruct) HighlightInfo(org.codelibs.fess.entity.HighlightInfo) LogManager(org.apache.logging.log4j.LogManager) BaseJsonApiManager(org.codelibs.fess.api.BaseJsonApiManager) Script(org.opensearch.script.Script) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchHelper(org.codelibs.fess.helper.SearchHelper) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) SystemHelper(org.codelibs.fess.helper.SystemHelper) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SearchHelper

use of org.codelibs.fess.helper.SearchHelper 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)

Example 3 with SearchHelper

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

the class JsonApiManager method processScrollSearchRequest.

protected void processScrollSearchRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (!fessConfig.isAcceptedSearchReferer(request.getHeader("referer"))) {
        writeJsonResponse(99, StringUtil.EMPTY, "Referer is invalid.");
        return;
    }
    if (!fessConfig.isApiSearchScroll()) {
        writeJsonResponse(99, StringUtil.EMPTY, "Scroll Search is not available.");
        return;
    }
    final StringBuilder buf = new StringBuilder(1000);
    request.setAttribute(Constants.SEARCH_LOG_ACCESS_TYPE, Constants.SEARCH_LOG_ACCESS_TYPE_JSON);
    final JsonRequestParams params = new JsonRequestParams(request, fessConfig);
    try {
        response.setContentType("application/x-ndjson; charset=UTF-8");
        final long count = searchHelper.scrollSearch(params, doc -> {
            buf.setLength(0);
            buf.append('{');
            boolean first2 = true;
            for (final Map.Entry<String, Object> entry : doc.entrySet()) {
                final String name = entry.getKey();
                if (StringUtil.isNotBlank(name) && entry.getValue() != null) {
                    if (!first2) {
                        buf.append(',');
                    } else {
                        first2 = false;
                    }
                    buf.append(escapeJson(name));
                    buf.append(':');
                    buf.append(escapeJson(entry.getValue()));
                }
            }
            buf.append('}');
            buf.append('\n');
            try {
                response.getWriter().print(buf.toString());
            } catch (final IOException e) {
                throw new IORuntimeException(e);
            }
            return true;
        }, OptionalThing.empty());
        response.flushBuffer();
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded {} docs", count);
        }
    } catch (final Exception e) {
        final int status = 9;
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a ping request.", e);
        }
        writeJsonResponse(status, null, e);
    }
}
Also used : IOException(java.io.IOException) SearchHelper(org.codelibs.fess.helper.SearchHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with SearchHelper

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

the class JsonApiManager method processFavoritesRequest.

protected void processFavoritesRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    if (!ComponentUtil.getFessConfig().isUserFavorite()) {
        writeJsonResponse(9, null, "Unsupported operation.");
        return;
    }
    final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
    final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
    int status = 0;
    String body = null;
    Exception err = null;
    try {
        final String queryId = request.getParameter("queryId");
        final String userCode = userInfoHelper.getUserCode();
        if (StringUtil.isBlank(userCode)) {
            throw new WebApiException(2, "No user session.");
        }
        if (StringUtil.isBlank(queryId)) {
            throw new WebApiException(3, "Query ID is null.");
        }
        final String[] docIds = userInfoHelper.getResultDocIds(queryId);
        final List<Map<String, Object>> docList = searchHelper.getDocumentListByDocIds(docIds, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldFavoriteCount() }, OptionalThing.empty(), SearchRequestType.JSON);
        List<String> urlList = new ArrayList<>(docList.size());
        for (final Map<String, Object> doc : docList) {
            final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
            if (urlObj != null) {
                urlList.add(urlObj);
            }
        }
        urlList = favoriteLogService.getUrlList(userCode, urlList);
        final List<String> docIdList = new ArrayList<>(urlList.size());
        for (final Map<String, Object> doc : docList) {
            final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
            if (urlObj != null && urlList.contains(urlObj)) {
                final String docIdObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
                if (docIdObj != null) {
                    docIdList.add(docIdObj);
                }
            }
        }
        // TODO replace response stream
        final StringBuilder buf = new StringBuilder(255);
        buf.append("\"num\":").append(docIdList.size());
        buf.append(", \"doc_ids\":[");
        if (!docIdList.isEmpty()) {
            for (int i = 0; i < docIdList.size(); i++) {
                if (i > 0) {
                    buf.append(',');
                }
                buf.append(escapeJson(docIdList.get(i)));
            }
        }
        buf.append(']');
        body = buf.toString();
    } catch (final Exception e) {
        if (e instanceof WebApiException) {
            status = ((WebApiException) e).getStatusCode();
        } else {
            status = 1;
        }
        err = e;
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a favorites request.", e);
        }
    }
    writeJsonResponse(status, body, err);
}
Also used : FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchHelper(org.codelibs.fess.helper.SearchHelper) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ServletException (javax.servlet.ServletException)4 IORuntimeException (org.codelibs.core.exception.IORuntimeException)4 WebApiException (org.codelibs.fess.exception.WebApiException)4 SearchHelper (org.codelibs.fess.helper.SearchHelper)4 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)4 ArrayList (java.util.ArrayList)2 FavoriteLogService (org.codelibs.fess.app.service.FavoriteLogService)2 SearchRenderData (org.codelibs.fess.entity.SearchRenderData)2 RelatedContentHelper (org.codelibs.fess.helper.RelatedContentHelper)2 RelatedQueryHelper (org.codelibs.fess.helper.RelatedQueryHelper)2 UserInfoHelper (org.codelibs.fess.helper.UserInfoHelper)2 FacetResponse (org.codelibs.fess.util.FacetResponse)2 Field (org.codelibs.fess.util.FacetResponse.Field)2 URLDecoder (java.net.URLDecoder)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Locale (java.util.Locale)1