Shinsuke Sugaya пре 5 година
родитељ
комит
67672f25b6

+ 52 - 45
src/main/java/org/codelibs/fess/api/json/JsonApiManager.java

@@ -524,51 +524,58 @@ public class JsonApiManager extends BaseJsonApiManager {
                 throw new WebApiException(6, "No searched urls.");
                 throw new WebApiException(6, "No searched urls.");
             }
             }
 
 
-            searchHelper.getDocumentByDocId(docId, new String[] { fessConfig.getIndexFieldUrl() }, OptionalThing.empty())
-                    .ifPresent(doc -> {
-                        final String favoriteUrl = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
-                        final String userCode = userInfoHelper.getUserCode();
-
-                        if (StringUtil.isBlank(userCode)) {
-                            throw new WebApiException(2, "No user session.");
-                        } else if (StringUtil.isBlank(favoriteUrl)) {
-                            throw new WebApiException(2, "URL is null.");
-                        }
-
-                        boolean found = false;
-                        for (final String id : docIds) {
-                            if (docId.equals(id)) {
-                                found = true;
-                                break;
-                            }
-                        }
-                        if (!found) {
-                            throw new WebApiException(5, "Not found: " + favoriteUrl);
-                        }
-
-                        if (!favoriteLogService.addUrl(userCode, (userInfo, favoriteLog) -> {
-                            favoriteLog.setUserInfoId(userInfo.getId());
-                            favoriteLog.setUrl(favoriteUrl);
-                            favoriteLog.setDocId(docId);
-                            favoriteLog.setQueryId(queryId);
-                            favoriteLog.setCreatedAt(systemHelper.getCurrentTimeAsLocalDateTime());
-                        })) {
-                            throw new WebApiException(4, "Failed to add url: " + favoriteUrl);
-                        }
-
-                        final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
-                        searchHelper.update(id, builder -> {
-                            final Script script = new Script("ctx._source." + fessConfig.getIndexFieldFavoriteCount() + "+=1");
-                            builder.setScript(script);
-                            final Map<String, Object> upsertMap = new HashMap<>();
-                            upsertMap.put(fessConfig.getIndexFieldFavoriteCount(), 1);
-                            builder.setUpsert(upsertMap);
-                            builder.setRefreshPolicy(Constants.TRUE);
-                        });
-
-                        writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
-
-                    }).orElse(() -> {
+            searchHelper
+                    .getDocumentByDocId(docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() },
+                            OptionalThing.empty())
+                    .ifPresent(
+                            doc -> {
+                                final String favoriteUrl = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
+                                final String userCode = userInfoHelper.getUserCode();
+
+                                if (StringUtil.isBlank(userCode)) {
+                                    throw new WebApiException(2, "No user session.");
+                                } else if (StringUtil.isBlank(favoriteUrl)) {
+                                    throw new WebApiException(2, "URL is null.");
+                                }
+
+                                boolean found = false;
+                                for (final String id : docIds) {
+                                    if (docId.equals(id)) {
+                                        found = true;
+                                        break;
+                                    }
+                                }
+                                if (!found) {
+                                    throw new WebApiException(5, "Not found: " + favoriteUrl);
+                                }
+
+                                if (!favoriteLogService.addUrl(userCode, (userInfo, favoriteLog) -> {
+                                    favoriteLog.setUserInfoId(userInfo.getId());
+                                    favoriteLog.setUrl(favoriteUrl);
+                                    favoriteLog.setDocId(docId);
+                                    favoriteLog.setQueryId(queryId);
+                                    favoriteLog.setCreatedAt(systemHelper.getCurrentTimeAsLocalDateTime());
+                                })) {
+                                    throw new WebApiException(4, "Failed to add url: " + favoriteUrl);
+                                }
+
+                                final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
+                                searchHelper.update(
+                                        id,
+                                        builder -> {
+                                            final Script script =
+                                                    ComponentUtil.getLanguageHelper().createScript(doc,
+                                                            "ctx._source." + fessConfig.getIndexFieldFavoriteCount() + "+=1");
+                                            builder.setScript(script);
+                                            final Map<String, Object> upsertMap = new HashMap<>();
+                                            upsertMap.put(fessConfig.getIndexFieldFavoriteCount(), 1);
+                                            builder.setUpsert(upsertMap);
+                                            builder.setRefreshPolicy(Constants.TRUE);
+                                        });
+
+                                writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
+
+                            }).orElse(() -> {
                         throw new WebApiException(6, "Not found: " + docId);
                         throw new WebApiException(6, "Not found: " + docId);
                     });
                     });
 
 

+ 17 - 0
src/main/java/org/codelibs/fess/helper/LanguageHelper.java

@@ -27,6 +27,7 @@ import org.codelibs.core.lang.StringUtil;
 import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.DocumentUtil;
 import org.codelibs.fess.util.DocumentUtil;
+import org.elasticsearch.script.Script;
 
 
 public class LanguageHelper {
 public class LanguageHelper {
     private static final Logger logger = LogManager.getLogger(LanguageHelper.class);
     private static final Logger logger = LogManager.getLogger(LanguageHelper.class);
@@ -120,4 +121,20 @@ public class LanguageHelper {
         this.detector = detector;
         this.detector = detector;
     }
     }
 
 
+    public Script createScript(final Map<String, Object> doc, String code) {
+        final StringBuilder buf = new StringBuilder(100);
+        buf.append(code);
+        final FessConfig fessConfig = ComponentUtil.getFessConfig();
+        final String language = DocumentUtil.getValue(doc, fessConfig.getIndexFieldLang(), String.class);
+        if (StringUtil.isNotBlank(language)) {
+            for (final String f : langFields) {
+                buf.append(";ctx._source.").append(f).append('_').append(language).append("=ctx._source.").append(f);
+            }
+        }
+        if (logger.isDebugEnabled()) {
+            logger.debug("update script: {}", buf);
+        }
+        return new Script(buf.toString());
+    }
+
 }
 }

+ 4 - 3
src/main/java/org/codelibs/fess/helper/SearchLogHelper.java

@@ -349,15 +349,16 @@ public class SearchLogHelper {
                 searchHelper.bulkUpdate(builder -> {
                 searchHelper.bulkUpdate(builder -> {
                     final FessConfig fessConfig = ComponentUtil.getFessConfig();
                     final FessConfig fessConfig = ComponentUtil.getFessConfig();
                     searchHelper.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]),
                     searchHelper.getDocumentListByDocIds(clickCountMap.keySet().toArray(new String[clickCountMap.size()]),
-                            new String[] { fessConfig.getIndexFieldDocId() }, OptionalThing.of(FessUserBean.empty()),
-                            SearchRequestType.ADMIN_SEARCH).forEach(
+                            new String[] { fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldLang() },
+                            OptionalThing.of(FessUserBean.empty()), SearchRequestType.ADMIN_SEARCH).forEach(
                             doc -> {
                             doc -> {
                                 final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
                                 final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
                                 final String docId = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
                                 final String docId = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
                                 if (id != null && docId != null && clickCountMap.containsKey(docId)) {
                                 if (id != null && docId != null && clickCountMap.containsKey(docId)) {
                                     final Integer count = clickCountMap.get(docId);
                                     final Integer count = clickCountMap.get(docId);
                                     final Script script =
                                     final Script script =
-                                            new Script("ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
+                                            ComponentUtil.getLanguageHelper().createScript(doc,
+                                                    "ctx._source." + fessConfig.getIndexFieldClickCount() + "+=" + count.toString());
                                     final Map<String, Object> upsertMap = new HashMap<>();
                                     final Map<String, Object> upsertMap = new HashMap<>();
                                     upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
                                     upsertMap.put(fessConfig.getIndexFieldClickCount(), count);
                                     builder.add(new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), id).script(script).upsert(
                                     builder.add(new UpdateRequest(fessConfig.getIndexDocumentUpdateIndex(), id).script(script).upsert(