use of org.codelibs.fess.app.service.SearchService in project fess by codelibs.
the class SearchLogHelper method processClickLogQueue.
protected void processClickLogQueue(final Queue<ClickLog> queue) {
final Map<String, Integer> clickCountMap = new HashMap<>();
final List<ClickLog> clickLogList = new ArrayList<>();
for (final ClickLog clickLog : queue) {
try {
final SearchLogBhv searchLogBhv = ComponentUtil.getComponent(SearchLogBhv.class);
searchLogBhv.selectEntity(cb -> {
cb.query().setQueryId_Equal(clickLog.getQueryId());
}).ifPresent(entity -> {
clickLogList.add(clickLog);
final String docId = clickLog.getDocId();
Integer countObj = clickCountMap.get(docId);
if (countObj == null) {
countObj = Integer.valueOf(1);
} else {
countObj = countObj.intValue() + 1;
}
clickCountMap.put(docId, countObj);
}).orElse(() -> {
logger.warn("Not Found for SearchLog: " + clickLog);
});
} catch (final Exception e) {
logger.warn("Failed to process: " + clickLog, e);
}
}
if (!clickLogList.isEmpty()) {
try {
final ClickLogBhv clickLogBhv = ComponentUtil.getComponent(ClickLogBhv.class);
clickLogBhv.batchInsert(clickLogList);
} catch (final Exception e) {
logger.warn("Failed to insert: " + clickLogList, e);
}
}
if (!clickCountMap.isEmpty()) {
final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
try {
searchService.bulkUpdate(builder -> {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
searchService.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]), new String[] { fessConfig.getIndexFieldDocId() }, OptionalThing.of(FessUserBean.empty()), SearchRequestType.ADMIN_SEARCH).forEach(doc -> {
final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
final String docId = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
if (id != null && docId != null && clickCountMap.containsKey(docId)) {
final Integer count = clickCountMap.get(docId);
final Script script = new Script("ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
final Map<String, Object> upsertMap = new HashMap<>();
upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
builder.add(new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), id).script(script).upsert(upsertMap));
}
});
});
} catch (final Exception e) {
logger.warn("Failed to update clickCounts", e);
}
}
}
Aggregations