Dynamic options via client-java-api

烈酒焚心 提交于 2019-12-24 01:06:07

问题


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


回答1:


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");


来源:https://stackoverflow.com/questions/35047372/dynamic-options-via-client-java-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!