Reputation: 51
I have a document with properties userId:sri
, collection
as "sony", and
the document content looks like:
<?xml version="1.0" encoding="UTF-8"?>
<songs>
<song>
<title>Against the Wind</title>
<artist>Bob Seger</artist>
<song>
<song>
<title>one love</title>
<artist>Bob Marley</artist>
<song>
<song>
<title>Beat it</title>
<artist>Micheal Jackson</artist>
<song>
<songs>
I want to write a query by artist
, whose property userId
is equal to "XYZ", collection name equal to "abc" and artist
equal to "per". The end result should look like:
<song>
<title>MNO</title>
</song>
Upvotes: 2
Views: 168
Reputation: 7335
Can you model the data to store each song as a separate document? One best practice for modelling in MarkLogic is to use a document as the equivalent of a row in a relational database and use a collection as the equivalent for a table.
Can you store the userId within the document? Another best practice for modelling in MarkLogic is to use the envelope pattern (as with HTML head and body) so the metadata and data are both in the same document. That way, a query can get both metadata and data with a single fragment read (which is faster).
If you make the suggested changes to the model, the query using the StructuredQueryBuilder in the Java API would resemble the following code:
QueryManager queryMgr = client.newQueryManager();
StructuredQueryBuilder qb = new StructuredQueryBuilder();
StructuredQueryDefinition querydef = qb.and(
qb.collection("abc"),
qb.value(element("userId"), "xyz"),
qb.value(element("artist"), "pqr")
);
SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle());
For iterating the results, see this example:
More detail about matching collections:
More detail about matching a value:
If you have to match the metadata in the properties, you can use the properties() builder function, but it is not the best practice:
Hoping that helps,
Upvotes: 2