I want to search for XML documents in a MarkLogic database which contain the given element value pairs and I want to vary the size of the element value pairs.
This is the query I wrote to find the matching documents which have ACCOUNT "00100" and DESCRIPTION as "hello".
QueryManager queryMgr = client.newQueryManager();
StructuredQueryBuilder qryBldr = new StructuredQueryBuilder();
StructuredQueryDefinition query1 =
qryBldr.value(qryBldr.element("ACCOUNT"), "00100");
StructuredQueryDefinition query2 =
qryBldr.value(qryBldr.element("DESCRIPTION"), "hello");
StructuredQueryDefinition query = qryBldr.and(query1,query2);
SearchHandle resultsHandle = new SearchHandle();
queryMgr.search(query, resultsHandle);
I want to write a method which will take input map which contains key value pairs and it will form the query based the map key value pairs and return the matching documents.
Example document looks like this:
<Tax>
<ACCOUNT>00100</ACCOUNT>
<DESCRIPTION>hello</DESCRIPTION>
<AMOUNT>100</AMOUNT>
<DATE>05252018</DATE>
</Tax>
How can I implement this?
Java 8 should be able to convert the map with a stream similar to the following code (untested):
StructuredQueryDefinition query = qryBldr.and(
theInputMap
.entrySet()
.stream()
.map(entry -> qryBlder.value(
qryBldr.element(entry.getKey()), entry.getValue()
))
.toArray(size -> new StructuredQueryDefinition[size])
);
If the query always consists of key-value pairs, you could also consider Query By Example:
http://docs.marklogic.com/guide/search-dev/qbe
In that approach, you would just serialize the map to JSON -- for instance, using Jackson.
Hoping that helps,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With