Search in sources :

Example 1 with SuggestHelper

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

the class SuggestApiManager method process.

@Override
public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
    int status = 0;
    String errMsg = StringUtil.EMPTY;
    // TODO replace response stream
    final StringBuilder buf = new StringBuilder(255);
    final RoleQueryHelper roleQueryHelper = ComponentUtil.getRoleQueryHelper();
    final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
    try {
        final RequestParameter parameter = RequestParameter.parse(request);
        final String[] langs = searchService.getLanguages(request, parameter);
        final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
        final SuggestRequestBuilder builder = suggestHelper.suggester().suggest();
        builder.setQuery(parameter.getQuery());
        stream(parameter.getSuggestFields()).of(stream -> stream.forEach(builder::addField));
        roleQueryHelper.build(SearchRequestType.SUGGEST).stream().forEach(builder::addRole);
        builder.setSize(parameter.getNum());
        stream(langs).of(stream -> stream.forEach(builder::addLang));
        builder.addKind(SuggestItem.Kind.USER.toString());
        if (ComponentUtil.getFessConfig().isSuggestSearchLog()) {
            builder.addKind(SuggestItem.Kind.QUERY.toString());
        }
        if (ComponentUtil.getFessConfig().isSuggestDocuments()) {
            builder.addKind(SuggestItem.Kind.DOCUMENT.toString());
        }
        final SuggestResponse suggestResponse = builder.execute().getResponse();
        buf.append("\"result\":{");
        buf.append("\"took\":\"").append(suggestResponse.getTookMs()).append('\"');
        buf.append(",\"total\":\"").append(suggestResponse.getTotal()).append('\"');
        buf.append(",\"num\":\"").append(suggestResponse.getNum()).append('\"');
        if (!suggestResponse.getItems().isEmpty()) {
            buf.append(",\"hits\":[");
            boolean first = true;
            for (final SuggestItem item : suggestResponse.getItems()) {
                if (!first) {
                    buf.append(',');
                }
                first = false;
                buf.append("{\"text\":\"").append(StringEscapeUtils.escapeJson(item.getText())).append('\"');
                buf.append(",\"tags\":[");
                for (int i = 0; i < item.getTags().length; i++) {
                    if (i > 0) {
                        buf.append(',');
                    }
                    buf.append('\"').append(StringEscapeUtils.escapeJson(item.getTags()[i])).append('\"');
                }
                buf.append(']');
                buf.append('}');
            }
            buf.append(']');
        }
        buf.append('}');
    } catch (final Exception e) {
        status = 1;
        errMsg = e.getMessage();
        if (errMsg == null) {
            errMsg = e.getClass().getName();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a suggest request.", e);
        }
        if (e instanceof InvalidAccessTokenException) {
            final InvalidAccessTokenException iate = (InvalidAccessTokenException) e;
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.setHeader("WWW-Authenticate", "Bearer error=\"" + iate.getType() + "\"");
        }
    }
    writeJsonResponse(status, buf.toString(), errMsg);
}
Also used : SuggestHelper(org.codelibs.fess.helper.SuggestHelper) SuggestItem(org.codelibs.fess.suggest.entity.SuggestItem) ServletException(javax.servlet.ServletException) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) IOException(java.io.IOException) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) RoleQueryHelper(org.codelibs.fess.helper.RoleQueryHelper) SearchService(org.codelibs.fess.app.service.SearchService) SuggestResponse(org.codelibs.fess.suggest.request.suggest.SuggestResponse) SuggestRequestBuilder(org.codelibs.fess.suggest.request.suggest.SuggestRequestBuilder)

Example 2 with SuggestHelper

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

the class SuggestCreator method purge.

private int purge(final LocalDateTime time) {
    final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
    try {
        suggestHelper.purgeDocumentSuggest(time);
        final long cleanupDay = ComponentUtil.getFessConfig().getPurgeSuggestSearchLogDay();
        if (cleanupDay > 0) {
            suggestHelper.purgeSearchlogSuggest(time.minusDays(cleanupDay));
        }
        return 0;
    } catch (final Exception e) {
        logger.info("Purge error.", e);
        return 1;
    }
}
Also used : SuggestHelper(org.codelibs.fess.helper.SuggestHelper) ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 3 with SuggestHelper

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

the class SuggestCreator method create.

private int create() {
    if (!ComponentUtil.getFessConfig().isSuggestDocuments()) {
        logger.info("Skip create suggest document.");
        return 0;
    }
    logger.info("Start create suggest document.");
    final AtomicInteger result = new AtomicInteger(1);
    final CountDownLatch latch = new CountDownLatch(1);
    final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
    suggestHelper.indexFromDocuments(ret -> {
        logger.info("Success index from documents.");
        result.set(0);
        latch.countDown();
    }, t -> {
        logger.error("Failed to update suggest index.", t);
        latch.countDown();
    });
    try {
        latch.await();
    } catch (final InterruptedException ignore) {
        if (logger.isDebugEnabled()) {
            logger.debug("Interrupted.", ignore);
        }
    }
    return result.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuggestHelper(org.codelibs.fess.helper.SuggestHelper) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

SuggestHelper (org.codelibs.fess.helper.SuggestHelper)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 SearchService (org.codelibs.fess.app.service.SearchService)1 ContainerNotAvailableException (org.codelibs.fess.exception.ContainerNotAvailableException)1 InvalidAccessTokenException (org.codelibs.fess.exception.InvalidAccessTokenException)1 RoleQueryHelper (org.codelibs.fess.helper.RoleQueryHelper)1 SuggestItem (org.codelibs.fess.suggest.entity.SuggestItem)1 SuggestRequestBuilder (org.codelibs.fess.suggest.request.suggest.SuggestRequestBuilder)1 SuggestResponse (org.codelibs.fess.suggest.request.suggest.SuggestResponse)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1