Search in sources :

Example 1 with MCRQueryCondition

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();
}
Also used : MCRCondition(org.mycore.parsers.bool.MCRCondition) Iterator(java.util.Iterator) MCRAndCondition(org.mycore.parsers.bool.MCRAndCondition) Set(java.util.Set) HashMap(java.util.HashMap) MCRConfiguration(org.mycore.common.config.MCRConfiguration) Collectors(java.util.stream.Collectors) MCRException(org.mycore.common.MCRException) MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) MCRSetCondition(org.mycore.parsers.bool.MCRSetCondition) Logger(org.apache.logging.log4j.Logger) MCRSortBy(org.mycore.services.fieldquery.MCRSortBy) MCRSolrConstants(org.mycore.solr.MCRSolrConstants) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) MCRSolrUtils(org.mycore.solr.MCRSolrUtils) ORDER(org.apache.solr.client.solrj.SolrQuery.ORDER) LogManager(org.apache.logging.log4j.LogManager) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) SortClause(org.apache.solr.client.solrj.SolrQuery.SortClause) MCRNotCondition(org.mycore.parsers.bool.MCRNotCondition) MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) MCRCondition(org.mycore.parsers.bool.MCRCondition) MCRNotCondition(org.mycore.parsers.bool.MCRNotCondition) MCRSetCondition(org.mycore.parsers.bool.MCRSetCondition)

Example 2 with MCRQueryCondition

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));
}
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 MCRQueryCondition

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));
}
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 MCRQueryCondition

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));
}
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)4 MCRQueryCondition (org.mycore.services.fieldquery.MCRQueryCondition)4 MCRAndCondition (org.mycore.parsers.bool.MCRAndCondition)3 HashSet (java.util.HashSet)2 MCRCondition (org.mycore.parsers.bool.MCRCondition)2 MCRNotCondition (org.mycore.parsers.bool.MCRNotCondition)2 MCRSetCondition (org.mycore.parsers.bool.MCRSetCondition)2 MCRQuery (org.mycore.services.fieldquery.MCRQuery)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 ORDER (org.apache.solr.client.solrj.SolrQuery.ORDER)1 SortClause (org.apache.solr.client.solrj.SolrQuery.SortClause)1