use of org.mycore.services.fieldquery.MCRQuery in project mycore by MyCoRe-Org.
the class MCRQLSearchServlet method doGetPost.
@Override
public void doGetPost(MCRServletJob job) throws IOException, ServletException, TransformerException, SAXException {
HttpServletRequest request = job.getRequest();
HttpServletResponse response = job.getResponse();
String searchString = getReqParameter(request, "search", null);
String queryString = getReqParameter(request, "query", null);
Document input = (Document) request.getAttribute("MCRXEditorSubmission");
MCRQuery query;
if (input != null) {
// xeditor input
query = MCRQLSearchUtils.buildFormQuery(input.getRootElement());
} else {
if (queryString != null) {
query = MCRQLSearchUtils.buildComplexQuery(queryString);
} else if (searchString != null) {
query = MCRQLSearchUtils.buildDefaultQuery(searchString, defaultSearchField);
} else {
query = MCRQLSearchUtils.buildNameValueQuery(request);
}
input = MCRQLSearchUtils.setQueryOptions(query, request);
}
// Show incoming query document
if (LOGGER.isDebugEnabled()) {
XMLOutputter out = new XMLOutputter(org.jdom2.output.Format.getPrettyFormat());
LOGGER.debug(out.outputString(input));
}
boolean doNotRedirect = "false".equals(getReqParameter(request, "redirect", null));
if (doNotRedirect) {
showResults(request, response, query, input);
} else {
sendRedirect(request, response, query, input);
}
}
use of org.mycore.services.fieldquery.MCRQuery in project mycore by MyCoRe-Org.
the class MCRQLSearchUtils method buildDefaultQuery.
/**
* Search in default search field specified by MCR.SearchServlet.DefaultSearchField
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static MCRQuery buildDefaultQuery(String search, String defaultSearchField) {
String[] fields = defaultSearchField.split(" *, *");
MCROrCondition queryCondition = new MCROrCondition();
for (String fDef : fields) {
MCRCondition condition = new MCRQueryCondition(fDef, "=", search);
queryCondition.addChild(condition);
}
return new MCRQuery(MCRQueryParser.normalizeCondition(queryCondition));
}
use of org.mycore.services.fieldquery.MCRQuery in project mycore by MyCoRe-Org.
the class MCRQLSearchUtils method buildNameValueQuery.
/**
* Search using name=value pairs from HTTP request
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static MCRQuery buildNameValueQuery(HttpServletRequest req) {
MCRAndCondition condition = new MCRAndCondition();
for (Enumeration<String> names = req.getParameterNames(); names.hasMoreElements(); ) {
String name = names.nextElement();
if (name.endsWith(".operator")) {
continue;
}
if (name.contains(".sortField")) {
continue;
}
if (SEARCH_PARAMETER.contains(name)) {
continue;
}
if (name.startsWith("XSL.")) {
continue;
}
String[] values = req.getParameterValues(name);
MCRSetCondition parent = condition;
if ((values.length > 1) || name.contains(",")) {
// Multiple fields with same name, combine with OR
parent = new MCROrCondition();
condition.addChild(parent);
}
for (String fieldName : name.split(",")) {
String operator = getReqParameter(req, fieldName + ".operator", "=");
for (String value : values) {
parent.addChild(new MCRQueryCondition(fieldName, operator, value));
}
}
}
if (condition.getChildren().isEmpty()) {
throw new MCRUsageException("Missing query condition");
}
return new MCRQuery(MCRQueryParser.normalizeCondition(condition));
}
use of org.mycore.services.fieldquery.MCRQuery in project mycore by MyCoRe-Org.
the class MCRQLSearchUtilsTest method testGetSolrQuery.
/**
* Test method for
* {@link org.mycore.solr.search.MCRQLSearchUtils#getSolrQuery(org.mycore.services.fieldquery.MCRQuery, org.jdom2.Document, javax.servlet.http.HttpServletRequest)}
* .
*/
@Test
public final void testGetSolrQuery() {
MCRQuery andQuery = getMCRQuery("(state = \"submitted\") AND (state = \"published\")");
assertEquals("+state:\"submitted\" +state:\"published\"", MCRQLSearchUtils.getSolrQuery(andQuery, andQuery.buildXML(), null).getQuery());
// MCR-994
MCRQuery orQuery = getMCRQuery("(state = \"submitted\") OR (state = \"published\")");
assertEquals("+(state:\"submitted\" state:\"published\")", MCRQLSearchUtils.getSolrQuery(orQuery, orQuery.buildXML(), null).getQuery());
}
Aggregations