Search code examples
javamarklogic

MarkLogic search and retrieve specific fields


I am faily new to MarkLogic (and noSQL) and currently trying to learn the Java API client. My question on searching, which returns back search result snippets / matches, is it possible for the search result to include specific fields in the document?

For example, given this document:

{"id":"1", "type":"classified", "description": "This is a classified type."}

And I search using this:

    QueryManager queryMgr = client.newQueryManager();       
    StringQueryDefinition query = queryMgr.newStringDefinition();       
    query.setCriteria("classified");
    queryMgr.search(query, resultsHandle);

How can I get the JSON document's 3 defined fields (id, type, description) as part of the search result - so I can display them in my UI table?

Do I need to hit the DB again by loading the document via URI (thus if I have 1000 records, that means hitting the DB again 1000 times)?


Solution

  • You have several options to retrieve specific fields with your search results. You could use the Pojo Data Binding Interface. You could read multiple documents matching a query which brings back the entirety of each document which you can then get as a pojo or String or any other handle. Or you can use the same API you're using above but add search options to allow you to extract a portion of a matching document.

    If you're bring back thousands of matches, you're probably not showing all those snippets to end users, so you should probably disable snippets using something like

    <transform-results apply="empty-snippet" />
    

    in your options.