Dynamic options via client-java-api

I am creating REST webservice that take parameters via url and search in marklogic based on these dynamic parameters.

q=search&offset=0&limit=10& sort=documentId|asc&termAggregations=group:10

I am creating using StructuredQueryDefinition & RawCombinedQueryDefinition :

StructuredQueryDefinition queryCriteria = sqb.or(query, sqb.properties(sqb.term(parameters.getQuery))));
String combinedQuery = "<search xmlns=\"http://marklogic.com/appservices/search\">"             + queryCriteria.serialize() + options + "</search>";
RawCombinedQueryDefinition rawCombinedQuery = queryMgr.newRawCombinedQueryDefinition( new StringHandle(combinedQuery));

For creating query with options, I am using String /String Buffer like for option extract-document-data

extractedDataOption.append("<extract-document-data selected=\"include\">")
Loop through each field 

{   extractedDataOption.append("<extract-path ");
    extractedDataOption.append(" xmlns:");
    extractedDataOption.append(field_attributes.get("namespace"));
    extractedDataOption.append(" >//");
    extractedDataOption.append(field_attributes.get("fieldname"));
    extractedDataOption.append(" </extract-path>");
}   
extractedDataOption.append("</extract-document-data>");

Similarly for sort, facets, filters constraints.

I can't use persisted query as parameters for sort, facets & filters are based on webservice request parameters.

Also, I see QueryOptionsBuilder and other similar classes are deprecated.

Can someone please let me what would be best way to create these different options dynamically instead of string?

Thanks

Answers


Strings work fine when your options are very simple or static. But if you're building a complex or dynamic XML structure I think you're wise to seek something less likely to accidentally produce mal-formed XML. XML builder libraries for Java are prevalent and several good ones are directly supported by the Java Client API: Jackson, JDOM, DOM4J, XOM, DOM, and JAXB. Pick your favorite.

Also, there are XML Builders that create an InputStream or String and can thus be supported using StringHandle or InputStreamHandle.

Here's an example of using XMLStreamWriter to build options XML then serialize it to a string (cobled from pieces in CombinedQueryBuilderTest.java).

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8");
    writer.setDefaultNamespace("http://marklogic.com/appservices/search");
    writer.writeStartElement("options");
        writer.writeStartElement("search-option");
            writer.writeCharacters("filtered");
        writer.writeEndElement();
    writer.writeEndElement();
    return baos.toString("UTF-8");

Need Your Help

JAX-RS Application on the root context - how can it be done?

java jax-rs

I would like to have my JAX-RX Application start at the root context so my URLs will be

SQL subtraction code works in SQL but not in sqldatasource

asp.net sql sql-server gridview sql-server-2012

I'm scratching my head over this one as I can get this code to work in SQL but when I transfer it to a simple gridview the code errors out stating 'Invalid Object name tablename'.