use of org.opensearch.action.update.UpdateRequest in project fess by codelibs.
the class SearchLogHelper method updateClickFieldInIndex.
protected void updateClickFieldInIndex(final Map<String, Integer> clickCountMap) {
if (!clickCountMap.isEmpty()) {
final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
try {
final UpdateRequest[] updateRequests = searchHelper.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]), new String[] { fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldLang() }, OptionalThing.of(FessUserBean.empty()), SearchRequestType.ADMIN_SEARCH).stream().map(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 = ComponentUtil.getLanguageHelper().createScript(doc, "ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
final Map<String, Object> upsertMap = new HashMap<>();
upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
return new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), id).script(script).upsert(upsertMap);
}
return null;
}).filter(req -> req != null).toArray(n -> new UpdateRequest[n]);
if (updateRequests.length > 0) {
searchHelper.bulkUpdate(builder -> {
for (final UpdateRequest req : updateRequests) {
builder.add(req);
}
});
}
} catch (final Exception e) {
logger.warn("Failed to update clickCounts", e);
}
}
}
Aggregations