use of org.mycore.services.fieldquery.MCRQueryCondition in project mycore by MyCoRe-Org.
the class MCRConditionTransformer method getIndex.
/**
* Returns the ID of the index of all fields referenced in this condition.
* If the fields come from multiple indexes, the constant mixed is returned.
*/
@SuppressWarnings("rawtypes")
private static String getIndex(MCRCondition cond) {
if (cond instanceof MCRQueryCondition) {
MCRQueryCondition queryCondition = ((MCRQueryCondition) cond);
String fieldName = queryCondition.getFieldName();
return getIndex(fieldName);
} else if (cond instanceof MCRNotCondition) {
return getIndex(((MCRNotCondition) cond).getChild());
}
@SuppressWarnings("unchecked") List<MCRCondition> children = ((MCRSetCondition) cond).getChildren();
// mixed indexes here!
return children.stream().map(MCRConditionTransformer::getIndex).reduce((l, r) -> l.equals(r) ? l : mixed).get();
}
use of org.mycore.services.fieldquery.MCRQueryCondition 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.MCRQueryCondition 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.MCRQueryCondition in project mycore by MyCoRe-Org.
the class MCRConditionTransformerTest method testToSolrQueryString.
@Test
public final void testToSolrQueryString() {
HashSet<String> usedFields = new HashSet<>();
MCRQueryCondition inner1 = new MCRQueryCondition("objectType", "=", "mods");
assertEquals("+objectType:\"mods\"", MCRConditionTransformer.toSolrQueryString(inner1, usedFields));
assertTrue("usedFields did not contain 'objectType'", usedFields.contains("objectType"));
MCRQueryCondition inner2 = new MCRQueryCondition("objectType", "=", "cbu");
MCROrCondition<Void> orCond = new MCROrCondition<>(inner1, inner2);
usedFields.clear();
// MCR-973 check for surrounding brackets on single OR query
assertEquals("+(objectType:\"mods\" objectType:\"cbu\")", MCRConditionTransformer.toSolrQueryString(orCond, usedFields));
assertTrue("usedFields did not contain 'objectType'", usedFields.contains("objectType"));
MCRQueryCondition inner3 = new MCRQueryCondition("derCount", ">", "0");
MCRAndCondition<Void> andCondition = new MCRAndCondition<>(orCond, inner3);
usedFields.clear();
assertEquals("+(objectType:\"mods\" objectType:\"cbu\") +derCount:{0 TO *]", MCRConditionTransformer.toSolrQueryString(andCondition, usedFields));
assertTrue("usedFields did not contain 'objectType'", usedFields.contains("objectType"));
assertTrue("usedFields did not contain 'derCount'", usedFields.contains("derCount"));
inner3.setOperator(">=");
assertEquals("+(objectType:\"mods\" objectType:\"cbu\") +derCount:[0 TO *]", MCRConditionTransformer.toSolrQueryString(andCondition, usedFields));
inner3.setOperator("<");
assertEquals("+(objectType:\"mods\" objectType:\"cbu\") +derCount:[* TO 0}", MCRConditionTransformer.toSolrQueryString(andCondition, usedFields));
inner3.setOperator("<=");
assertEquals("+(objectType:\"mods\" objectType:\"cbu\") +derCount:[* TO 0]", MCRConditionTransformer.toSolrQueryString(andCondition, usedFields));
MCRNotCondition<Void> notCond = new MCRNotCondition<>(orCond);
andCondition.getChildren().remove(0);
andCondition.getChildren().add(0, notCond);
assertEquals("-(objectType:\"mods\" objectType:\"cbu\") +derCount:[* TO 0]", MCRConditionTransformer.toSolrQueryString(andCondition, usedFields));
}
Aggregations