2

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>

1 Answer 1

2

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:

https://github.com/marklogic/java-client-api/blob/master/src/main/java/com/marklogic/client/example/cookbook/StructuredSearch.java#L126

More detail about matching collections:

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/StructuredQueryBuilder.html#collection%28java.lang.String...%29

More detail about matching a value:

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/StructuredQueryBuilder.html#value%28com.marklogic.client.query.StructuredQueryBuilder.TextIndex,%20java.lang.String...%29

If you have to match the metadata in the properties, you can use the properties() builder function, but it is not the best practice:

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/StructuredQueryBuilder.html#properties%28com.marklogic.client.query.StructuredQueryDefinition%29

Hoping that helps,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.