浏览代码

fix 2487 check if update requests exist

Shinsuke Sugaya 4 年之前
父节点
当前提交
caa36e2830
共有 1 个文件被更改,包括 31 次插入22 次删除
  1. 31 22
      src/main/java/org/codelibs/fess/helper/SearchLogHelper.java

+ 31 - 22
src/main/java/org/codelibs/fess/helper/SearchLogHelper.java

@@ -331,7 +331,7 @@ public class SearchLogHelper {
                     logger.warn("Not Found for SearchLog: {}", clickLog);
                 });
             } catch (final Exception e) {
-                logger.warn("Failed to process: " + clickLog, e);
+                logger.warn("Failed to process: {}", clickLog, e);
             }
         }
         if (!clickLogList.isEmpty()) {
@@ -339,33 +339,42 @@ public class SearchLogHelper {
                 final ClickLogBhv clickLogBhv = ComponentUtil.getComponent(ClickLogBhv.class);
                 clickLogBhv.batchInsert(clickLogList);
             } catch (final Exception e) {
-                logger.warn("Failed to insert: " + clickLogList, e);
+                logger.warn("Failed to insert: {}", clickLogList, e);
             }
         }
 
         if (!clickCountMap.isEmpty()) {
             final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
+            final FessConfig fessConfig = ComponentUtil.getFessConfig();
             try {
-                searchHelper.bulkUpdate(builder -> {
-                    final FessConfig fessConfig = ComponentUtil.getFessConfig();
-                    searchHelper.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]),
-                            new String[] { fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldLang() },
-                            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 =
-                                            ComponentUtil.getLanguageHelper().createScript(doc,
-                                                    "ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
-                                    final Map<String, Object> upsertMap = new HashMap<>();
-                                    upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
-                                    builder.add(new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), id).script(script).upsert(
-                                            upsertMap));
-                                }
-                            });
-                });
+                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);
             }