Search code examples
marklogic

Retrieve the field values from a MarkLogic search


I have an XQuery FLOWR search in MarkLogic like the below which returns paginated matching documents in a collection ordered based on a field

let $options := 
  <options xmlns="http://marklogic.com/appservices/search">
    <transform-results apply="raw"/>
    <additional-query> 
      {cts:collection-query("http://example.com/myCollectionName")} 
    </additional-query>
    <sort-order direction="descending" >
      <field name="myCustomFieldA" />
    </sort-order>
     <debug>true</debug> 
  </options>

for $searchResults in search:search("someQuery", $options, 1, 10)
return $searchResults

I have several custom fields setup on the documents with coresponding field range indexes and I would like to retrieve the values in the fields for the matching documents, rather than the document itself. Something like the following?

for $searchResults in search:search("someQuery", $options, 1, 10)
return get-field-values($searchResults, ("myCustomFieldA", "myCustomFieldB"))

How would I return these field values?


Solution

  • Your example suggests that you want the results paginated and sorted by a certain range index.

    cts:field-values() can be used in your iteration with the document URI as a part of a document-query passed to the 4th parameter(query parameter).

    However, you may find cts:value-co-occurences to be useful.