use of org.codelibs.fess.app.service.SearchService 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 SearchService searchService = ComponentUtil.getComponent(SearchService.class);
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.");
}
searchService.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);
searchService.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(() -> {
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.codelibs.fess.app.service.SearchService in project fess by codelibs.
the class JsonApiManager method processSearchRequest.
protected void processSearchRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
final FessConfig fessConfig = ComponentUtil.getFessConfig();
int status = 0;
Exception err = null;
String query = null;
// TODO replace response stream
final StringBuilder buf = new StringBuilder(1000);
request.setAttribute(Constants.SEARCH_LOG_ACCESS_TYPE, Constants.SEARCH_LOG_ACCESS_TYPE_JSON);
try {
final SearchRenderData data = new SearchRenderData();
final JsonRequestParams params = new JsonRequestParams(request, fessConfig);
query = params.getQuery();
request.setAttribute(Constants.REQUEST_QUERIES, query);
searchService.search(params, data, OptionalThing.empty());
final String execTime = data.getExecTime();
final String queryTime = Long.toString(data.getQueryTime());
final String pageSize = Integer.toString(data.getPageSize());
final String currentPageNumber = Integer.toString(data.getCurrentPageNumber());
final String allRecordCount = Long.toString(data.getAllRecordCount());
final String allPageCount = Integer.toString(data.getAllPageCount());
final List<Map<String, Object>> documentItems = data.getDocumentItems();
final FacetResponse facetResponse = data.getFacetResponse();
final String queryId = data.getQueryId();
final String highlightParams = data.getAppendHighlightParams();
final boolean nextPage = data.isExistNextPage();
final boolean prevPage = data.isExistPrevPage();
final long startRecordNumber = data.getCurrentStartRecordNumber();
final long endRecordNumber = data.getCurrentEndRecordNumber();
final List<String> pageNumbers = data.getPageNumberList();
final boolean partial = data.isPartialResults();
final String searchQuery = data.getSearchQuery();
final long requestedTime = data.getRequestedTime();
buf.append("\"q\":");
buf.append(escapeJson(query));
buf.append(",\"query_id\":");
buf.append(escapeJson(queryId));
buf.append(",\"exec_time\":");
buf.append(execTime);
buf.append(",\"query_time\":");
buf.append(queryTime);
buf.append(',');
buf.append("\"page_size\":");
buf.append(pageSize);
buf.append(',');
buf.append("\"page_number\":");
buf.append(currentPageNumber);
buf.append(',');
buf.append("\"record_count\":");
buf.append(allRecordCount);
buf.append(',');
buf.append("\"page_count\":");
buf.append(allPageCount);
buf.append(",\"highlight_params\":");
buf.append(escapeJson(highlightParams));
buf.append(",\"next_page\":");
buf.append(escapeJson(nextPage));
buf.append(",\"prev_page\":");
buf.append(escapeJson(prevPage));
buf.append(",\"start_record_number\":");
buf.append(startRecordNumber);
buf.append(",\"end_record_number\":");
buf.append(escapeJson(endRecordNumber));
buf.append(",\"page_numbers\":");
buf.append(escapeJson(pageNumbers));
buf.append(",\"partial\":");
buf.append(escapeJson(partial));
buf.append(",\"search_query\":");
buf.append(escapeJson(searchQuery));
buf.append(",\"requested_time\":");
buf.append(requestedTime);
if (!documentItems.isEmpty()) {
buf.append(',');
buf.append("\"result\":[");
boolean first1 = true;
for (final Map<String, Object> document : documentItems) {
if (!first1) {
buf.append(',');
} else {
first1 = false;
}
buf.append('{');
boolean first2 = true;
for (final Map.Entry<String, Object> entry : document.entrySet()) {
final String name = entry.getKey();
if (StringUtil.isNotBlank(name) && entry.getValue() != null && ComponentUtil.getQueryHelper().isApiResponseField(name)) {
if (!first2) {
buf.append(',');
} else {
first2 = false;
}
buf.append(escapeJson(name));
buf.append(':');
buf.append(escapeJson(entry.getValue()));
}
}
buf.append('}');
}
buf.append(']');
}
if (facetResponse != null && facetResponse.hasFacetResponse()) {
// facet field
if (facetResponse.getFieldList() != null) {
buf.append(',');
buf.append("\"facet_field\":[");
boolean first1 = true;
for (final Field field : facetResponse.getFieldList()) {
if (!first1) {
buf.append(',');
} else {
first1 = false;
}
buf.append("{\"name\":");
buf.append(escapeJson(field.getName()));
buf.append(",\"result\":[");
boolean first2 = true;
for (final Map.Entry<String, Long> entry : field.getValueCountMap().entrySet()) {
if (!first2) {
buf.append(',');
} else {
first2 = false;
}
buf.append("{\"value\":");
buf.append(escapeJson(entry.getKey()));
buf.append(",\"count\":");
buf.append(entry.getValue());
buf.append('}');
}
buf.append(']');
buf.append('}');
}
buf.append(']');
}
// facet q
if (facetResponse.getQueryCountMap() != null) {
buf.append(',');
buf.append("\"facet_query\":[");
boolean first1 = true;
for (final Map.Entry<String, Long> entry : facetResponse.getQueryCountMap().entrySet()) {
if (!first1) {
buf.append(',');
} else {
first1 = false;
}
buf.append("{\"value\":");
buf.append(escapeJson(entry.getKey()));
buf.append(",\"count\":");
buf.append(entry.getValue());
buf.append('}');
}
buf.append(']');
}
}
} catch (final Exception e) {
status = 1;
err = e;
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a search request.", e);
}
}
writeJsonResponse(status, buf.toString(), err);
}
use of org.codelibs.fess.app.service.SearchService in project fess by codelibs.
the class SuggestApiManager method process.
@Override
public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
int status = 0;
String errMsg = StringUtil.EMPTY;
// TODO replace response stream
final StringBuilder buf = new StringBuilder(255);
final RoleQueryHelper roleQueryHelper = ComponentUtil.getRoleQueryHelper();
final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
try {
final RequestParameter parameter = RequestParameter.parse(request);
final String[] langs = searchService.getLanguages(request, parameter);
final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
final SuggestRequestBuilder builder = suggestHelper.suggester().suggest();
builder.setQuery(parameter.getQuery());
stream(parameter.getSuggestFields()).of(stream -> stream.forEach(builder::addField));
roleQueryHelper.build(SearchRequestType.SUGGEST).stream().forEach(builder::addRole);
builder.setSize(parameter.getNum());
stream(langs).of(stream -> stream.forEach(builder::addLang));
builder.addKind(SuggestItem.Kind.USER.toString());
if (ComponentUtil.getFessConfig().isSuggestSearchLog()) {
builder.addKind(SuggestItem.Kind.QUERY.toString());
}
if (ComponentUtil.getFessConfig().isSuggestDocuments()) {
builder.addKind(SuggestItem.Kind.DOCUMENT.toString());
}
final SuggestResponse suggestResponse = builder.execute().getResponse();
buf.append("\"result\":{");
buf.append("\"took\":\"").append(suggestResponse.getTookMs()).append('\"');
buf.append(",\"total\":\"").append(suggestResponse.getTotal()).append('\"');
buf.append(",\"num\":\"").append(suggestResponse.getNum()).append('\"');
if (!suggestResponse.getItems().isEmpty()) {
buf.append(",\"hits\":[");
boolean first = true;
for (final SuggestItem item : suggestResponse.getItems()) {
if (!first) {
buf.append(',');
}
first = false;
buf.append("{\"text\":\"").append(StringEscapeUtils.escapeJson(item.getText())).append('\"');
buf.append(",\"tags\":[");
for (int i = 0; i < item.getTags().length; i++) {
if (i > 0) {
buf.append(',');
}
buf.append('\"').append(StringEscapeUtils.escapeJson(item.getTags()[i])).append('\"');
}
buf.append(']');
buf.append('}');
}
buf.append(']');
}
buf.append('}');
} catch (final Exception e) {
status = 1;
errMsg = e.getMessage();
if (errMsg == null) {
errMsg = e.getClass().getName();
}
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a suggest request.", e);
}
if (e instanceof InvalidAccessTokenException) {
final InvalidAccessTokenException iate = (InvalidAccessTokenException) e;
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "Bearer error=\"" + iate.getType() + "\"");
}
}
writeJsonResponse(status, buf.toString(), errMsg);
}
use of org.codelibs.fess.app.service.SearchService in project fess by codelibs.
the class GsaApiManager method processSearchRequest.
protected void processSearchRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
final FessConfig fessConfig = ComponentUtil.getFessConfig();
int status = 0;
String errMsg = StringUtil.EMPTY;
String query = null;
// TODO replace response stream
final StringBuilder buf = new StringBuilder(1000);
request.setAttribute(Constants.SEARCH_LOG_ACCESS_TYPE, Constants.SEARCH_LOG_ACCESS_TYPE_XML);
boolean xmlDtd = false;
try {
final SearchRenderData data = new SearchRenderData();
final GsaRequestParams params = new GsaRequestParams(request, fessConfig);
query = params.getQuery();
request.setAttribute(Constants.REQUEST_QUERIES, query);
searchService.search(params, data, OptionalThing.empty());
final String execTime = data.getExecTime();
final long allRecordCount = data.getAllRecordCount();
final List<Map<String, Object>> documentItems = data.getDocumentItems();
final List<String> getFields = new ArrayList<>();
// meta tags should be returned
final String getFieldsParam = request.getParameter("getfields");
if (StringUtil.isNotBlank(getFieldsParam)) {
getFields.addAll(Arrays.asList(getFieldsParam.split("\\.")));
}
// DTD
if ("xml".equals(request.getParameter("output"))) {
xmlDtd = true;
}
final StringBuilder requestUri = new StringBuilder(request.getRequestURI());
if (request.getQueryString() != null) {
requestUri.append("?").append(request.getQueryString());
}
final String uriQueryString = requestUri.toString();
// Input/Output encoding
final String ie = request.getCharacterEncoding();
final String oe = "UTF-8";
// IP address
final String ip = ComponentUtil.getViewHelper().getClientIp(request);
final String start = request.getParameter("start");
long startNumber = 1;
if (StringUtil.isNotBlank(start)) {
startNumber = Long.parseLong(start) + 1;
}
long endNumber = startNumber + data.getPageSize() - 1;
if (endNumber > allRecordCount) {
endNumber = allRecordCount;
}
buf.append("<Q>");
buf.append(escapeXml(query));
buf.append("</Q>");
buf.append("<TM>");
buf.append(execTime);
buf.append("</TM>");
for (final Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
final String[] values = entry.getValue();
if (values == null) {
continue;
}
final String key = entry.getKey();
for (final String value : values) {
buf.append("<PARAM name=\"");
buf.append(key);
buf.append("\" value=\"");
buf.append(value);
buf.append("\" original_value=\"");
buf.append(URLEncoder.encode(value, Constants.UTF_8));
buf.append("\"/>");
}
}
buf.append("<PARAM name=\"ie\" value=\"");
buf.append(ie);
buf.append("\" original_value=\"");
buf.append(URLEncoder.encode(ie, Constants.UTF_8));
buf.append("\"/>");
buf.append("<PARAM name=\"oe\" value=\"");
buf.append(oe);
buf.append("\" original_value=\"");
buf.append(URLEncoder.encode(ie, Constants.UTF_8));
buf.append("\"/>");
buf.append("<PARAM name=\"ip\" value=\"");
buf.append(ip);
buf.append("\" original_value=\"");
buf.append(URLEncoder.encode(ie, Constants.UTF_8));
buf.append("\"/>");
if (!documentItems.isEmpty()) {
buf.append("<RES SN=\"");
buf.append(startNumber);
buf.append("\" EN=\"");
buf.append(endNumber);
buf.append("\">");
buf.append("<M>");
buf.append(allRecordCount);
buf.append("</M>");
if (endNumber < allRecordCount) {
buf.append("<NB>");
buf.append("<NU>");
buf.append(escapeXml(uriQueryString.replaceFirst("start=([^&]+)", "start=" + endNumber)));
buf.append("</NU>");
buf.append("</NB>");
}
long recordNumber = startNumber;
for (final Map<String, Object> document : documentItems) {
buf.append("<R N=\"");
buf.append(recordNumber);
buf.append("\">");
final String url = (String) document.remove("url");
document.put("UE", url);
document.put("U", URLDecoder.decode(url, Constants.UTF_8));
document.put("T", document.remove("title"));
final float score = Float.parseFloat((String) document.remove("boost"));
document.put("RK", (int) (score * 10));
document.put("S", ((String) document.remove("content_description")).replaceAll("<(/*)em>", "<$1b>"));
document.put("LANG", document.remove("lang"));
for (final Map.Entry<String, Object> entry : document.entrySet()) {
final String name = entry.getKey();
if (StringUtil.isNotBlank(name) && entry.getValue() != null && ComponentUtil.getQueryHelper().isApiResponseField(name)) {
if (name.startsWith(gsaMetaPrefix)) {
final String tagName = name.replaceAll("^" + gsaMetaPrefix, "").replaceAll(GSA_META_SUFFIX + "\\z", "");
if (getFields != null && getFields.contains(tagName)) {
buf.append("<MT N=\"");
buf.append(tagName);
buf.append("\" V=\"");
buf.append(escapeXml(entry.getValue().toString()));
buf.append("\"/>");
}
} else {
final String tagName = name;
buf.append('<');
buf.append(tagName);
buf.append('>');
buf.append(escapeXml(entry.getValue()));
buf.append("</");
buf.append(tagName);
buf.append('>');
}
}
}
buf.append("<HAS>");
buf.append("<L/>");
buf.append("<C SZ=\"");
buf.append(Long.parseLong((String) document.remove("content_length")) / 1000);
buf.append("k\" CID=\"");
buf.append(document.remove("doc_id"));
buf.append("\" ENC=\"");
String charset = (String) document.remove("charset_s");
if (StringUtil.isNotBlank(charset)) {
buf.append(charset);
} else {
charset = (String) document.remove("contentType_s");
if (StringUtil.isNotBlank(charset)) {
final Matcher m = Pattern.compile(".*;\\s*charset=(.+)").matcher(charset);
if (m.matches()) {
charset = m.group(1);
buf.append(charset);
}
}
}
buf.append("\"/>");
buf.append("</HAS>");
buf.append("</R>");
recordNumber++;
}
buf.append("</RES>");
}
} catch (final Exception e) {
status = 1;
errMsg = e.getMessage();
if (errMsg == null) {
errMsg = e.getClass().getName();
}
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a search request.", e);
}
if (e instanceof InvalidAccessTokenException) {
final InvalidAccessTokenException iate = (InvalidAccessTokenException) e;
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "Bearer error=\"" + iate.getType() + "\"");
}
}
writeXmlResponse(status, xmlDtd, buf.toString(), errMsg);
}
use of org.codelibs.fess.app.service.SearchService in project fess by codelibs.
the class JsonApiManager method processFavoritesRequest.
protected void processFavoritesRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
if (!ComponentUtil.getFessConfig().isUserFavorite()) {
writeJsonResponse(9, null, "Unsupported operation.");
return;
}
final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
int status = 0;
String body = null;
Exception err = null;
try {
final String queryId = request.getParameter("queryId");
final String userCode = userInfoHelper.getUserCode();
if (StringUtil.isBlank(userCode)) {
throw new WebApiException(2, "No user session.");
} else if (StringUtil.isBlank(queryId)) {
throw new WebApiException(3, "Query ID is null.");
}
final String[] docIds = userInfoHelper.getResultDocIds(queryId);
final List<Map<String, Object>> docList = searchService.getDocumentListByDocIds(docIds, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldFavoriteCount() }, OptionalThing.empty(), SearchRequestType.JSON);
List<String> urlList = new ArrayList<>(docList.size());
for (final Map<String, Object> doc : docList) {
final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (urlObj != null) {
urlList.add(urlObj);
}
}
urlList = favoriteLogService.getUrlList(userCode, urlList);
final List<String> docIdList = new ArrayList<>(urlList.size());
for (final Map<String, Object> doc : docList) {
final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (urlObj != null && urlList.contains(urlObj)) {
final String docIdObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
if (docIdObj != null) {
docIdList.add(docIdObj);
}
}
}
// TODO replace response stream
final StringBuilder buf = new StringBuilder(255);
buf.append("\"num\":").append(docIdList.size());
if (!docIdList.isEmpty()) {
buf.append(", \"doc_ids\":[");
for (int i = 0; i < docIdList.size(); i++) {
if (i > 0) {
buf.append(',');
}
buf.append(escapeJson(docIdList.get(i)));
}
buf.append(']');
}
body = buf.toString();
} catch (final Exception e) {
if (e instanceof WebApiException) {
status = ((WebApiException) e).getStatusCode();
} else {
status = 1;
}
err = e;
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a favorites request.", e);
}
}
writeJsonResponse(status, body, err);
}
Aggregations