I'm learning how to use Marklogic 6 via the Java API and I'd now like to find out how to search/list/paginate through documents within Java and JAXB. Specifically:
Suppose I have a set of user documents like so:
/user/joe453.xml
/user/karla47.xml
.
.
.
/user/sam.xml
/user/tom.xml
And a user is somewhat defined like this:
<user userId="aaabbb123" passwordHash="bcryptHash">
<role id="USER"> <--- other role types here depending on the kind of user.
</user>
I know I could do a Key/value based search if I wanted to search for a specific user over the userId attribute. However:
1) What functionality of the Java API should I use to, in the simplest case, list/paginate over all these users?
2) I know I could do an ad hoc XQuery using XCC but I really learned bad lessons the last time I used hardcoded query strings in SQL. I can see a similar fate with XML/Marklogic.
3) Is there a better way to do this?
Thanks!
Dat Silencer:
To have search return entire documents, you can set up query options to return the entire document as the snippet:
<transform-results apply="raw"/>
Then, on the client, you can pass the contents of search results that were persisted by JAXB through the JAXB unmarshaller to hydrate the Java objects.
As an alternative, the Java API includes the SearchCollector example, which returns a multipart / mixed payload where each part is a search result document. Documents that were persisted via JAXB can then be unmarshalled.
Specifics aside, the general strategy is quite sensible -- take advantage of MarkLogic search to fetch the persisted objects of interest using any field of the object or subject.
The primary key, of course, still has value for direct access. That is, if the object has some combination of fields that correspond immutably to the identity of the entity, take advantage of those fields to make the document uri and use that for direct access to the object when search would be overkill.
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