Search in sources :

Example 1 with MCRQuery

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XMLOutputter(org.jdom2.output.XMLOutputter) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRQuery(org.mycore.services.fieldquery.MCRQuery) Document(org.jdom2.Document)

Example 2 with MCRQuery

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

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

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());
}
Also used : MCRQuery(org.mycore.services.fieldquery.MCRQuery) Test(org.junit.Test)

Aggregations

MCRQuery (org.mycore.services.fieldquery.MCRQuery)4 MCROrCondition (org.mycore.parsers.bool.MCROrCondition)2 MCRQueryCondition (org.mycore.services.fieldquery.MCRQueryCondition)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 Test (org.junit.Test)1 MCRUsageException (org.mycore.common.MCRUsageException)1 MCRAndCondition (org.mycore.parsers.bool.MCRAndCondition)1 MCRCondition (org.mycore.parsers.bool.MCRCondition)1 MCRSetCondition (org.mycore.parsers.bool.MCRSetCondition)1