use of org.opensearch.script.Script in project fess by codelibs.
the class JsonApiManager method processFavoriteRequest.
protected void processFavoriteRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
if (!ComponentUtil.getFessConfig().isUserFavorite()) {
writeJsonResponse(9, null, "Unsupported operation.");
return;
}
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
try {
final String docId = request.getParameter("docId");
final String queryId = request.getParameter("queryId");
final String[] docIds = userInfoHelper.getResultDocIds(URLDecoder.decode(queryId, Constants.UTF_8));
if (docIds == null) {
throw new WebApiException(6, "No searched urls.");
}
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.");
}
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);
});
} catch (final Exception e) {
int status;
if (e instanceof WebApiException) {
status = ((WebApiException) e).getStatusCode();
} else {
status = 1;
}
writeJsonResponse(status, null, e);
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a favorite request.", e);
}
}
}
use of org.opensearch.script.Script 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);
}
}
}
use of org.opensearch.script.Script in project fess by codelibs.
the class LanguageHelper method createScript.
public Script createScript(final Map<String, Object> doc, final 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());
}
use of org.opensearch.script.Script in project fess by codelibs.
the class LanguageHelper method getReindexScriptSource.
public String getReindexScriptSource() {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String langField = fessConfig.getIndexFieldLang();
final String code = Arrays.stream(langFields).map(s -> "ctx._source['" + s + "_'+ctx._source." + langField + "]=ctx._source." + s).collect(Collectors.joining(";"));
if (logger.isDebugEnabled()) {
logger.debug("reindex script: {}", code);
}
return "if(ctx._source." + langField + "!=null){" + code + "}";
}
use of org.opensearch.script.Script in project fess by codelibs.
the class UpdateLabelJob method execute.
public String execute() {
final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
final LanguageHelper languageHelper = ComponentUtil.getLanguageHelper();
final StringBuilder resultBuf = new StringBuilder();
try {
final long count = searchEngineClient.updateByQuery(fessConfig.getIndexDocumentUpdateIndex(), option -> {
if (queryBuilder != null) {
option.setQuery(queryBuilder);
}
return option.setFetchSource(new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, null);
}, (builder, hit) -> {
try {
final Map<String, Object> doc = hit.getSourceAsMap();
final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (StringUtil.isNotBlank(url)) {
final Set<String> labelSet = labelTypeHelper.getMatchedLabelValueSet(url);
final Script script = languageHelper.createScript(doc, "ctx._source." + fessConfig.getIndexFieldLabel() + "=new String[]{" + labelSet.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(",")) + "}");
return builder.setScript(script);
}
} catch (final Exception e) {
logger.warn("Failed to process {}", hit, e);
}
return null;
});
resultBuf.append(count).append(" docs").append("\n");
} catch (final Exception e) {
logger.error("Could not update labels.", e);
resultBuf.append(e.getMessage()).append("\n");
}
return resultBuf.toString();
}
Aggregations