use of org.codelibs.fess.util.QueryResponseList in project fess by codelibs.
the class SearchService method search.
// ===================================================================================
// Method
// ==============
public void search(final SearchRequestParams params, final SearchRenderData data, final OptionalThing<FessUserBean> userBean) {
final long requestedTime = systemHelper.getCurrentTimeAsLong();
final long startTime = System.currentTimeMillis();
final boolean searchLogSupport = fessConfig.isSearchLog();
final String query = QueryStringBuilder.query(params.getQuery()).extraQueries(params.getExtraQueries()).fields(params.getFields()).build();
final int pageStart = params.getStartPosition();
final int pageSize = params.getPageSize();
final String sortField = params.getSort();
final List<Map<String, Object>> documentItems = fessEsClient.search(fessConfig.getIndexDocumentSearchIndex(), fessConfig.getIndexDocumentType(), searchRequestBuilder -> {
fessConfig.processSearchPreference(searchRequestBuilder, userBean);
return SearchConditionBuilder.builder(searchRequestBuilder).query(StringUtil.isBlank(sortField) ? query : query + " sort:" + sortField).offset(pageStart).size(pageSize).facetInfo(params.getFacetInfo()).geoInfo(params.getGeoInfo()).similarDocHash(params.getSimilarDocHash()).responseFields(queryHelper.getResponseFields()).searchRequestType(params.getType()).build();
}, (searchRequestBuilder, execTime, searchResponse) -> {
final QueryResponseList queryResponseList = ComponentUtil.getQueryResponseList();
queryResponseList.init(searchResponse, pageStart, pageSize);
return queryResponseList;
});
data.setDocumentItems(documentItems);
// search
final QueryResponseList queryResponseList = (QueryResponseList) documentItems;
data.setFacetResponse(queryResponseList.getFacetResponse());
@SuppressWarnings("unchecked") final Set<String> highlightQueries = (Set<String>) params.getAttribute(Constants.HIGHLIGHT_QUERIES);
if (highlightQueries != null) {
final StringBuilder buf = new StringBuilder(100);
highlightQueries.stream().forEach(q -> {
buf.append("&hq=").append(LaFunctions.u(q));
});
data.setAppendHighlightParams(buf.toString());
}
queryResponseList.setExecTime(System.currentTimeMillis() - startTime);
final NumberFormat nf = NumberFormat.getInstance(params.getLocale());
nf.setMaximumIntegerDigits(2);
nf.setMaximumFractionDigits(2);
String execTime;
try {
execTime = nf.format((double) queryResponseList.getExecTime() / 1000);
} catch (final Exception e) {
execTime = StringUtil.EMPTY;
}
data.setExecTime(execTime);
final String queryId = queryHelper.generateId();
data.setPageSize(queryResponseList.getPageSize());
data.setCurrentPageNumber(queryResponseList.getCurrentPageNumber());
data.setAllRecordCount(queryResponseList.getAllRecordCount());
data.setAllPageCount(queryResponseList.getAllPageCount());
data.setExistNextPage(queryResponseList.isExistNextPage());
data.setExistPrevPage(queryResponseList.isExistPrevPage());
data.setCurrentStartRecordNumber(queryResponseList.getCurrentStartRecordNumber());
data.setCurrentEndRecordNumber(queryResponseList.getCurrentEndRecordNumber());
data.setPageNumberList(queryResponseList.getPageNumberList());
data.setPartialResults(queryResponseList.isPartialResults());
data.setQueryTime(queryResponseList.getQueryTime());
data.setSearchQuery(query);
data.setRequestedTime(requestedTime);
data.setQueryId(queryId);
// search log
if (searchLogSupport) {
ComponentUtil.getSearchLogHelper().addSearchLog(params, DfTypeUtil.toLocalDateTime(requestedTime), queryId, query, pageStart, pageSize, queryResponseList);
}
}
use of org.codelibs.fess.util.QueryResponseList in project fess by codelibs.
the class SearchLogHelper method addSearchLog.
public void addSearchLog(final SearchRequestParams params, final LocalDateTime requestedTime, final String queryId, final String query, final int pageStart, final int pageSize, final QueryResponseList queryResponseList) {
final RoleQueryHelper roleQueryHelper = ComponentUtil.getRoleQueryHelper();
final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
final SearchLog searchLog = new SearchLog();
if (ComponentUtil.getFessConfig().isUserInfo()) {
final String userCode = userInfoHelper.getUserCode();
if (userCode != null) {
searchLog.setUserSessionId(userCode);
}
}
searchLog.setRoles(roleQueryHelper.build(params.getType()).stream().toArray(n -> new String[n]));
searchLog.setQueryId(queryId);
searchLog.setHitCount(queryResponseList.getAllRecordCount());
searchLog.setResponseTime(queryResponseList.getExecTime());
searchLog.setQueryTime(queryResponseList.getQueryTime());
searchLog.setSearchWord(StringUtils.abbreviate(query, 1000));
searchLog.setSearchQuery(StringUtils.abbreviate(queryResponseList.getSearchQuery(), 1000));
searchLog.setRequestedAt(requestedTime);
searchLog.setQueryOffset(pageStart);
searchLog.setQueryPageSize(pageSize);
ComponentUtil.getRequestManager().findUserBean(FessUserBean.class).ifPresent(user -> {
searchLog.setUser(user.getUserId());
});
final HttpServletRequest request = LaRequestUtil.getRequest();
searchLog.setClientIp(StringUtils.abbreviate(ComponentUtil.getViewHelper().getClientIp(request), 100));
searchLog.setReferer(StringUtils.abbreviate(request.getHeader("referer"), 1000));
searchLog.setUserAgent(StringUtils.abbreviate(request.getHeader("user-agent"), 255));
final Object accessType = request.getAttribute(Constants.SEARCH_LOG_ACCESS_TYPE);
if (Constants.SEARCH_LOG_ACCESS_TYPE_JSON.equals(accessType)) {
searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_JSON);
} else if (Constants.SEARCH_LOG_ACCESS_TYPE_XML.equals(accessType)) {
searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_XML);
} else if (Constants.SEARCH_LOG_ACCESS_TYPE_OTHER.equals(accessType)) {
searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_OTHER);
} else {
searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_WEB);
}
final Object languages = request.getAttribute(Constants.REQUEST_LANGUAGES);
if (languages != null) {
searchLog.setLanguages(StringUtils.join((String[]) languages, ","));
} else {
searchLog.setLanguages("");
}
@SuppressWarnings("unchecked") final Map<String, List<String>> fieldLogMap = (Map<String, List<String>>) request.getAttribute(Constants.FIELD_LOGS);
if (fieldLogMap != null) {
for (final Map.Entry<String, List<String>> logEntry : fieldLogMap.entrySet()) {
for (final String value : logEntry.getValue()) {
searchLog.addSearchFieldLogValue(logEntry.getKey(), StringUtils.abbreviate(value, 1000));
}
}
}
searchLogQueue.add(searchLog);
}
Aggregations