Search in sources :

Example 1 with MCROrCondition

use of org.mycore.parsers.bool.MCROrCondition in project mycore by MyCoRe-Org.

the class MCRQueryParser method buildConditions.

/**
 * Builds a new MCRCondition from parsed elements
 *
 * @param field
 *            one or more field names, separated by comma
 * @param oper
 *            the condition operator
 * @param value
 *            the condition value
 * @return
 */
private MCRCondition<Void> buildConditions(String field, String oper, String value) {
    if (field.contains(",")) {
        // Multiple fields in one condition, combine with OR
        StringTokenizer st = new StringTokenizer(field, ", ");
        MCROrCondition<Void> oc = new MCROrCondition<>();
        while (st.hasMoreTokens()) {
            oc.addChild(buildConditions(st.nextToken(), oper, value));
        }
        return oc;
    } else if (field.contains("-")) {
        // date condition von-bis
        StringTokenizer st = new StringTokenizer(field, "- ");
        String fieldFrom = st.nextToken();
        String fieldTo = st.nextToken();
        if (oper.equals("=")) {
            // von-bis = x --> (von <= x) AND (bis >= x)
            MCRAndCondition<Void> ac = new MCRAndCondition<>();
            ac.addChild(buildCondition(fieldFrom, "<=", value));
            ac.addChild(buildCondition(fieldTo, ">=", value));
            return ac;
        } else if (oper.contains("<")) {
            return buildCondition(fieldFrom, oper, value);
        } else {
            // oper.contains( ">" )
            return buildCondition(fieldTo, oper, value);
        }
    } else {
        return buildCondition(field, oper, value);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) MCRAndCondition(org.mycore.parsers.bool.MCRAndCondition)

Example 2 with MCROrCondition

use of org.mycore.parsers.bool.MCROrCondition 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));
}
Also used : MCRCondition(org.mycore.parsers.bool.MCRCondition) MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) MCRQuery(org.mycore.services.fieldquery.MCRQuery)

Example 3 with MCROrCondition

use of org.mycore.parsers.bool.MCROrCondition 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));
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) MCRQuery(org.mycore.services.fieldquery.MCRQuery) MCRSetCondition(org.mycore.parsers.bool.MCRSetCondition) MCRAndCondition(org.mycore.parsers.bool.MCRAndCondition)

Example 4 with MCROrCondition

use of org.mycore.parsers.bool.MCROrCondition in project mycore by MyCoRe-Org.

the class MCRQLSearchUtils method getSolrQuery.

@SuppressWarnings("rawtypes")
public static SolrQuery getSolrQuery(MCRQuery query, Document input, HttpServletRequest request) {
    int rows = Integer.parseInt(input.getRootElement().getAttributeValue("numPerPage", "10"));
    List<String> returnFields = query.getReturnFields();
    MCRCondition condition = query.getCondition();
    HashMap<String, List<MCRCondition>> table;
    if (condition instanceof MCRSetCondition) {
        table = MCRConditionTransformer.groupConditionsByIndex((MCRSetCondition) condition);
    } else {
        // if there is only one condition its no set condition. we don't need to group
        LOGGER.warn("Condition is not SetCondition.");
        table = new HashMap<>();
        ArrayList<MCRCondition> conditionList = new ArrayList<>();
        conditionList.add(condition);
        table.put("metadata", conditionList);
    }
    boolean booleanAnd = !(condition instanceof MCROrCondition<?>);
    SolrQuery mergedSolrQuery = MCRConditionTransformer.buildMergedSolrQuery(query.getSortBy(), false, booleanAnd, table, rows, returnFields);
    String mask = input.getRootElement().getAttributeValue("mask");
    if (mask != null) {
        mergedSolrQuery.setParam("mask", mask);
        mergedSolrQuery.setParam("_session", request.getParameter("_session"));
    }
    return mergedSolrQuery;
}
Also used : ArrayList(java.util.ArrayList) MCRSetCondition(org.mycore.parsers.bool.MCRSetCondition) SolrQuery(org.apache.solr.client.solrj.SolrQuery) MCRCondition(org.mycore.parsers.bool.MCRCondition) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with MCROrCondition

use of org.mycore.parsers.bool.MCROrCondition 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));
}
Also used : MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) MCRNotCondition(org.mycore.parsers.bool.MCRNotCondition) MCRAndCondition(org.mycore.parsers.bool.MCRAndCondition) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MCROrCondition (org.mycore.parsers.bool.MCROrCondition)5 MCRAndCondition (org.mycore.parsers.bool.MCRAndCondition)3 MCRQueryCondition (org.mycore.services.fieldquery.MCRQueryCondition)3 MCRCondition (org.mycore.parsers.bool.MCRCondition)2 MCRSetCondition (org.mycore.parsers.bool.MCRSetCondition)2 MCRQuery (org.mycore.services.fieldquery.MCRQuery)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 Test (org.junit.Test)1 MCRUsageException (org.mycore.common.MCRUsageException)1 MCRNotCondition (org.mycore.parsers.bool.MCRNotCondition)1