Commit c9650cf7 authored by Michael Becker's avatar Michael Becker
Browse files

allow field definition in search servlet

parent 433b1784
Pipeline #12073 passed with stage
in 3 minutes and 1 second
......@@ -53,53 +53,13 @@ public class MyMssSearchServlet extends MCRQLSearchServletLpz {
public void doGetPost(MCRServletJob job) throws IOException {
final HttpServletRequest request = job.getRequest();
final HttpServletResponse response = job.getResponse();
final String objectType = getReqParameter(request, "objectType", "");
final String objectTypeAndField = getReqParameter(request, "objectType", "");
final String objectType = StringUtils.substringBefore(objectTypeAndField, ";");
final String fields = StringUtils.substringAfter(objectTypeAndField, ";");
final String search = getReqParameter(request, "search", "*");
final String additionalReturnFields = getReqParameter(request, "additionalReturnFields", "");
final Element conditions = new Element("conditions");
conditions.setAttribute("format", "xml");
final Element and = new Element("boolean");
and.setAttribute("operator", "and");
conditions.addContent(and);
// add the object type and suppressed status condition
if (StringUtils.isNotEmpty(objectType)) {
final Element conditionObjectType = new Element("condition");
conditionObjectType.setAttribute("field", "objectType");
conditionObjectType.setAttribute("operator", "=");
conditionObjectType.setAttribute("value", StringUtils.substringBefore(objectType, "_"));
and.addContent(conditionObjectType);
and.addContent(getStatusCondition(Collections.singleton(objectType)));
} else {
// if object type not set: iterate over all active object types and prohibit suppressed status
Map<String, String> props = MCRConfiguration2.getSubPropertiesMap("MCR.Metadata.Type.");
Map<String, String> activeTypes = props.entrySet().stream()
.filter(type -> type.getValue().equals("true"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
and.addContent(getStatusCondition(activeTypes.keySet()));
}
// add the query condition
if (StringUtils.equals(objectType, SEARCH_INVENT)) {
final Element conditionSearch = new Element("condition");
conditionSearch.setAttribute("field", "mymss_mss01,mymss_mss01text,mymss_mss01shelfmark");
conditionSearch.setAttribute("operator", "contains");
conditionSearch.setAttribute("value", search);
and.addContent(conditionSearch);
} else {
final Element conditionSearch = new Element("condition");
conditionSearch.setAttribute("field", "allMeta,mymss_allmeta_diacr");
conditionSearch.setAttribute("operator", "contains");
conditionSearch.setAttribute("value", search);
and.addContent(conditionSearch);
}
final Element returnFields = new Element("returnFields");
if (StringUtils.isNotBlank(additionalReturnFields)) {
returnFields.setText(DEFAULT_RETURN_FIELDS + "," + additionalReturnFields);
......@@ -107,24 +67,18 @@ public class MyMssSearchServlet extends MCRQLSearchServletLpz {
returnFields.setText(DEFAULT_RETURN_FIELDS);
}
final String searchObjectType;
if (StringUtils.equals(objectType, SEARCH_INVENT)) {
searchObjectType = "manuscript";
} else {
searchObjectType = objectType;
}
final Element sortType = new Element("field");
sortType.setAttribute("name", "objectType");
sortType.setAttribute("order", "ascending");
final Element sortContent = new Element("field");
sortContent.setAttribute("name", defaultSortField(searchObjectType, getSession(request).getCurrentLanguage()));
sortContent.setAttribute("name", defaultSortField(objectType, getSession(request).getCurrentLanguage()));
sortContent.setAttribute("order", "ascending");
final Element sort = new Element("sortBy");
sort.addContent(sortType);
sort.addContent(sortContent);
final Element conditions = createConditions(objectType, fields, search);
final Element query = new Element("query");
query.setAttribute("maxResults", "0");
query.setAttribute("numPerPage", "50");
......@@ -139,6 +93,50 @@ public class MyMssSearchServlet extends MCRQLSearchServletLpz {
sendRedirect(request, response, MCRQLSearchUtils.buildFormQuery(queryDoc.getRootElement()), queryDoc);
}
public Element createConditions(final String objectType, final String fields, final String search) {
final String searchFields;
if (StringUtils.isBlank(fields)) {
searchFields = "allMeta,mymss_allmeta_diacr";
} else {
searchFields = fields;
}
final Element conditions = new Element("conditions");
conditions.setAttribute("format", "xml");
final Element and = new Element("boolean");
and.setAttribute("operator", "and");
conditions.addContent(and);
// add the object type and suppressed status condition
if (StringUtils.isNotEmpty(objectType)) {
final Element conditionObjectType = new Element("condition");
conditionObjectType.setAttribute("field", "objectType");
conditionObjectType.setAttribute("operator", "=");
conditionObjectType.setAttribute("value", objectType);
and.addContent(conditionObjectType);
and.addContent(getStatusCondition(Collections.singleton(objectType)));
} else {
// if object type not set: iterate over all active object types and prohibit suppressed status
Map<String, String> props = MCRConfiguration2.getSubPropertiesMap("MCR.Metadata.Type.");
Map<String, String> activeTypes = props.entrySet().stream()
.filter(type -> type.getValue().equals("true"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
and.addContent(getStatusCondition(activeTypes.keySet()));
}
// add the query condition
final Element conditionSearch = new Element("condition");
conditionSearch.setAttribute("field", searchFields);
conditionSearch.setAttribute("operator", "contains");
conditionSearch.setAttribute("value", search);
and.addContent(conditionSearch);
return conditions;
}
/**
* Adds the status condition to prohibit returning objects with suppressed status.
*
......@@ -155,7 +153,7 @@ public class MyMssSearchServlet extends MCRQLSearchServletLpz {
*
* @param objectTypes the object types for which status conditions are to be set
*/
private Element getStatusCondition(Collection<String> objectTypes) {
public Element getStatusCondition(Collection<String> objectTypes) {
final Element not = new Element("boolean");
not.setAttribute("operator", "not");
final Element or = new Element("boolean");
......@@ -181,7 +179,7 @@ public class MyMssSearchServlet extends MCRQLSearchServletLpz {
return not;
}
private String defaultSortField(String objectType, String lang) {
public String defaultSortField(String objectType, String lang) {
String sortField = MCRConfiguration2.getString("MCR.IndexBrowser." + objectType + ".FieldsToSort")
.orElse("id");
......
package org.mycore.test.solr.search;
import org.jdom2.Document;
import org.jdom2.Element;
import org.junit.Test;
import org.mycore.services.fieldquery.MCRQuery;
import org.mycore.solr.search.MyMssSearchServlet;
import org.mycore.test.frontend.MyMssSolrTest;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class MyMssSearchServletTest extends MyMssSolrTest {
@Test
public void testCreateCondition() throws Exception {
final MyMssSearchServlet servlet = new MyMssSearchServlet();
// no field definitions: use allMeta, mymss_allmeta_diacr
Element conditions = servlet.createConditions("manuscript", null, "test");
String query = createQuery(conditions).getCondition().toString();
assertThat(query,
is("(objectType = \"manuscript\") AND ((allMeta contains \"test\") " +
"OR (mymss_allmeta_diacr contains \"test\"))"));
// use a field definition to return
conditions = servlet.createConditions("manuscript", "mymss_mssTitle_general_orig", "test");
query = createQuery(conditions).getCondition().toString();
assertThat(query,
is("(objectType = \"manuscript\") AND (mymss_mssTitle_general_orig contains \"test\")"));
}
private MCRQuery createQuery(Element conditions) {
final Element query = new Element("query");
query.addContent(conditions);
return MCRQuery.parseXML(new Document(query));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment