import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.z3950.zing.cql.CQLAndNode; import org.z3950.zing.cql.CQLBooleanNode; import org.z3950.zing.cql.CQLNode; import org.z3950.zing.cql.CQLNotNode; import org.z3950.zing.cql.CQLOrNode; import org.z3950.zing.cql.CQLRelation; import org.z3950.zing.cql.CQLTermNode; import eu.clarin.sru.server.SRUConstants; import eu.clarin.sru.server.SRUException; import eu.clarin.sru.server.fcs.parser.QueryParserException; public class LexCQLToSolrConverter { private static final Logger LOGGER = LogManager.getLogger(LexCQLToSolrConverter.class); public static String convertLexCQLtoSolrQuery(final CQLNode node) throws QueryParserException, SRUException { StringBuilder sb = new StringBuilder(); convertLexCQLtoSolrSingle(node, sb); return sb.toString(); } private static void convertLexCQLtoSolrSingle(final CQLNode node, StringBuilder sb) throws SRUException { if (node instanceof CQLTermNode) { final CQLTermNode tn = ((CQLTermNode) node); final CQLRelation rel = tn.getRelation(); if (!"=".equals(rel.getBase()) && !"==".equals(rel.getBase())) { throw new SRUException(SRUConstants.SRU_CANNOT_PROCESS_QUERY_REASON_UNKNOWN, "Queries with queryType 'cql' do not support '" + rel.getBase() + "' relations by this FCS Endpoint."); } if (!rel.getModifiers().isEmpty()) { throw new SRUException(SRUConstants.SRU_CANNOT_PROCESS_QUERY_REASON_UNKNOWN, "Queries with queryType 'cql' do not support modifiers on '" + rel.getBase() + "' relation by this FCS Endpoint."); } if (tn.getIndex() != null && !"cql.serverChoice".equalsIgnoreCase(tn.getIndex())) { if ("lemma".equalsIgnoreCase(tn.getIndex())) { sb.append("lemma4search"); } else { sb.append(tn.getIndex()); } sb.append(":"); } sb.append('"'); sb.append(tn.getTerm()); sb.append('"'); } else if (node instanceof CQLOrNode || node instanceof CQLAndNode) { final CQLBooleanNode bn = (CQLBooleanNode) node; if (!bn.getModifiers().isEmpty()) { throw new SRUException(SRUConstants.SRU_CANNOT_PROCESS_QUERY_REASON_UNKNOWN, "Queries with queryType 'cql' do not support modifiers on '" + node.getClass().getSimpleName() + "' by this FCS Endpoint."); } sb.append("( "); convertLexCQLtoSolrSingle(bn.getLeftOperand(), sb); if (node instanceof CQLOrNode) { sb.append(" OR "); } else if (node instanceof CQLAndNode) { sb.append(" AND "); } convertLexCQLtoSolrSingle(bn.getRightOperand(), sb); sb.append(" )"); } else if (node instanceof CQLNotNode) { final CQLNotNode bnn = (CQLNotNode) node; throw new SRUException( SRUConstants.SRU_CANNOT_PROCESS_QUERY_REASON_UNKNOWN, "Queries with queryType 'cql' do not (yet) support '" + node.getClass().getSimpleName() + "' by this FCS Endpoint."); } else { throw new SRUException( SRUConstants.SRU_CANNOT_PROCESS_QUERY_REASON_UNKNOWN, "Queries with queryType 'cql' do not support '" + node.getClass().getSimpleName() + "' by this FCS Endpoint."); } } }