use of org.codelibs.fess.exception.InvalidQueryException in project fess by codelibs.
the class ApiAdminSearchlistAction method docs.
// ===================================================================================
// Search Execute
// ==============
// GET /api/admin/searchlist
// POST /api/admin/searchlist
@Execute
public JsonResponse<ApiResult> docs(final SearchBody body) {
validateApi(body, messages -> {
});
if (StringUtil.isBlank(body.q)) {
// query matches on all documents.
body.q = Constants.MATCHES_ALL_QUERY;
}
final SearchRenderData renderData = new SearchRenderData();
body.initialize();
try {
searchService.search(body, renderData, getUserBean());
return asJson(new ApiDocsResponse().renderData(renderData).status(Status.OK).result());
} catch (final InvalidQueryException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
throwValidationErrorApi(e.getMessageCode());
} catch (final ResultOffsetExceededException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
throwValidationErrorApi(messages -> messages.addErrorsResultSizeExceeded(GLOBAL));
}
throwValidationErrorApi(messages -> messages.addErrorsInvalidQueryUnknown(GLOBAL));
// ignore
return null;
}
use of org.codelibs.fess.exception.InvalidQueryException in project fess by codelibs.
the class FessEsClient method search.
public <T> T search(final String index, final String type, final SearchCondition<SearchRequestBuilder> condition, final SearchResult<T, SearchRequestBuilder, SearchResponse> searchResult) {
final long startTime = System.currentTimeMillis();
SearchResponse searchResponse = null;
final SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
if (condition.build(searchRequestBuilder)) {
if (ComponentUtil.hasQueryHelper()) {
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
if (queryHelper.getTimeAllowed() >= 0) {
searchRequestBuilder.setTimeout(TimeValue.timeValueMillis(queryHelper.getTimeAllowed()));
}
}
try {
if (logger.isDebugEnabled()) {
logger.debug("Query DSL:\n" + searchRequestBuilder.toString());
}
searchResponse = searchRequestBuilder.execute().actionGet(ComponentUtil.getFessConfig().getIndexSearchTimeout());
} catch (final SearchPhaseExecutionException e) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY), "Invalid query: " + searchRequestBuilder, e);
}
}
final long execTime = System.currentTimeMillis() - startTime;
return searchResult.build(searchRequestBuilder, execTime, OptionalEntity.ofNullable(searchResponse, () -> {
}));
}
use of org.codelibs.fess.exception.InvalidQueryException in project fess by codelibs.
the class SearchAction method doSearch.
protected HtmlResponse doSearch(final SearchForm form) {
validate(form, messages -> {
}, () -> asHtml(virtualHost(path_SearchJsp)));
if (isLoginRequired()) {
return redirectToLogin();
}
if (viewHelper.isUseSession()) {
final HttpSession session = request.getSession(false);
if (session != null) {
final Object resultsPerPage = session.getAttribute(Constants.RESULTS_PER_PAGE);
if (resultsPerPage instanceof Integer) {
form.num = (Integer) resultsPerPage;
}
}
}
if (StringUtil.isBlank(form.q) && form.fields.isEmpty()) {
// redirect to index page
form.q = null;
return redirectToRoot();
}
try {
buildFormParams(form);
form.lang = searchService.getLanguages(request, form);
request.setAttribute(Constants.REQUEST_LANGUAGES, form.lang);
request.setAttribute(Constants.REQUEST_QUERIES, form.q);
final WebRenderData renderData = new WebRenderData();
searchService.search(form, renderData, getUserBean());
return asHtml(virtualHost(path_SearchJsp)).renderWith(data -> {
renderData.register(data);
if (favoriteSupport || thumbnailSupport) {
final String queryId = renderData.getQueryId();
final List<Map<String, Object>> documentItems = renderData.getDocumentItems();
userInfoHelper.storeQueryId(queryId, documentItems);
if (thumbnailSupport) {
thumbnailManager.storeRequest(queryId, documentItems);
}
}
RenderDataUtil.register(data, "displayQuery", getDisplayQuery(form, labelTypeHelper.getLabelTypeItemList(SearchRequestType.SEARCH)));
createPagingQuery(form);
});
} catch (final InvalidQueryException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
saveError(e.getMessageCode());
return redirectToRoot();
} catch (final ResultOffsetExceededException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
saveError(messages -> {
messages.addErrorsResultSizeExceeded(GLOBAL);
});
return redirectToRoot();
}
}
use of org.codelibs.fess.exception.InvalidQueryException in project fess by codelibs.
the class QueryHelper method convertTermQuery.
protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery, final float boost) {
final String field = termQuery.getTerm().field();
final String text = termQuery.getTerm().text();
if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
return convertPrefixQuery(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))), boost);
} else if (Constants.DEFAULT_FIELD.equals(field)) {
context.addFieldLog(field, text);
context.addHighlightedQuery(text);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b * boost));
} else if ("sort".equals(field)) {
final String[] values = text.split("\\.");
if (values.length > 2) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQuerySortValue(UserMessages.GLOBAL_PROPERTY_KEY, text), "Invalid sort field: " + termQuery);
}
final String sortField = values[0];
if (!isSortField(sortField)) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortField(UserMessages.GLOBAL_PROPERTY_KEY, sortField), "Unsupported sort field: " + termQuery);
}
SortOrder sortOrder;
if (values.length == 2) {
sortOrder = SortOrder.DESC.toString().equalsIgnoreCase(values[1]) ? SortOrder.DESC : SortOrder.ASC;
if (sortOrder == null) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnsupportedSortOrder(UserMessages.GLOBAL_PROPERTY_KEY, values[1]), "Invalid sort order: " + termQuery);
}
} else {
sortOrder = SortOrder.ASC;
}
context.addSorts(createFieldSortBuilder(sortField, sortOrder));
return null;
} else if (INURL_FIELD.equals(field)) {
return QueryBuilders.wildcardQuery(fessConfig.getIndexFieldUrl(), "*" + text + "*").boost(boost);
} else if (isSearchField(field)) {
context.addFieldLog(field, text);
context.addHighlightedQuery(text);
if (notAnalyzedFieldSet.contains(field)) {
return QueryBuilders.termQuery(field, text).boost(boost);
} else {
return QueryBuilders.matchPhraseQuery(field, text).boost(boost);
}
} else {
final String origQuery = termQuery.toString();
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
context.addHighlightedQuery(origQuery);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b * boost));
}
}
use of org.codelibs.fess.exception.InvalidQueryException in project fess by codelibs.
the class QueryHelper method buildBaseQuery.
public void buildBaseQuery(final QueryContext queryContext, final Consumer<QueryContext> context) {
try {
final Query query = getQueryParser().parse(queryContext.getQueryString());
final QueryBuilder queryBuilder = convertQuery(queryContext, query, 1.0f);
if (queryBuilder != null) {
queryContext.setQueryBuilder(queryBuilder);
} else {
queryContext.setQueryBuilder(QueryBuilders.matchAllQuery());
}
// TODO options query
context.accept(queryContext);
} catch (final ParseException e) {
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY), "Invalid query: " + queryContext.getQueryString(), e);
}
}
Aggregations