fix #2397 append language fields

This commit is contained in:
Shinsuke Sugaya 2020-02-13 18:47:00 +09:00
parent 0cb3238056
commit 67672f25b6
3 changed files with 67 additions and 42 deletions

View file

@ -524,51 +524,58 @@ public class JsonApiManager extends BaseJsonApiManager {
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();
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.");
}
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);
}
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);
}
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);
});
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);
writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
}).orElse(() -> {
}).orElse(() -> {
throw new WebApiException(6, "Not found: " + docId);
});

View file

@ -27,6 +27,7 @@ import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.DocumentUtil;
import org.elasticsearch.script.Script;
public class LanguageHelper {
private static final Logger logger = LogManager.getLogger(LanguageHelper.class);
@ -120,4 +121,20 @@ public class LanguageHelper {
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());
}
}

View file

@ -349,15 +349,16 @@ public class SearchLogHelper {
searchHelper.bulkUpdate(builder -> {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
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 -> {
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());
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(