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);
}
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;
}
}
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();
}
Aggregations