marklogic - Dynamic options via client-java-api -
i creating rest webservice take parameters via url , search in marklogic based on these dynamic parameters.
q=search&offset=0&limit=10& sort=documentid|asc&termaggregations=group:10
i 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 options, using string /string buffer 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 sort, facets, filters constraints.
i can't use persisted query parameters sort, facets & filters based on webservice request parameters.
also, see queryoptionsbuilder , other similar classes deprecated.
can please let me best way create these different options dynamically instead of string?
thanks
strings work fine when options simple or static. if you're building complex or dynamic xml structure think you're wise seek less accidentally produce mal-formed xml. xml builder libraries java prevalent , several ones directly supported java client api: jackson, jdom, dom4j, xom, dom, , jaxb. pick favorite.
also, there xml builders create inputstream or string , can supported using stringhandle or inputstreamhandle.
here's example of using xmlstreamwriter build options xml serialize string (cobled 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");
Comments
Post a Comment