Skip to main content

Getting Started with Optic

To Find Documents in a Collection

In a MarkLogic multi-model database, your data does not have to be neat and highly structured to explore it. So, we could have explored our HR data using raw documents without creating a view.

The Data Accessor Function fromSearchDocs() is the Optic equivalent of cts.search(). It returns documents based on the CTS query provided along with their URI and score.

An Optic query like this one retrieves all of the data from 100 unique documents in a specified collection:

op.fromSearchDocs(cts.collectionQuery('https://example.com/content/employee'))
  .offsetLimit(0, 100)
  .result();

We used this query to retrieve a 3-column row sequence of all data from each employee-collection document, along with each document's URI and score, limited to 100 results:

  • The Data Accessor Function fromSearchDocs() pulls data from documents matching the cts.collectionQuery() parameter and narrowed down by other parameters into a row sequence with a unique row of these 3 columns for each matching document:

    • uri: Contains the document URI.

    • doc: Contains the document itself.

    • score: Contains the document’s search score, a measure of how relevant this result is with respect to other results. The higher the score, the higher the relevance.

  • The CTS Function cts.collectionQuery() restricts fromSearchDocs() to returning only data that matches the cts.collectionQuery(). In this case, fromSearchDocs() will only access documents in the specified collection.

  • The Operator Function offsetLimit() restricts results returned. The first parameter specifies the number of results to skip; the second, the number of results to return. So, (0, 100) returns the first 100 results.

  • The Executor Function result() executes the query and returns the results as a row sequence.

Here is row 1 of the 100-row x 3-column result:

{
 "uri": "/data/employees/c1f3450c-cf3b-4622-8df7-a2f0818ada72.json", 
 "doc": {
  "GUID": "c1f3450c-cf3b-4622-8df7-a2f0818ada72", 
  "Gender": "male", 
  "Title": "Mr.", 
  "GivenName": "Ralph", 
  "MiddleInitial": "J", 
  "Surname": "Garcia", 
  "StreetAddress": "209 Stratford Park", 
  "City": "Crane", 
  "State": "IN", 
  "ZipCode": "47522", 
  "Country": "US", 
  "EmailAddress": "RalphJGarcia@teleworm.us", 
  "TelephoneNumber": "812-854-1074", 
  "TelephoneCountryCode": "1", 
  "Birthday": "1/2/78", 
  "NationalID": "310-48-6699", 
  "BaseSalary": "78730", 
  "Bonus": "7873", 
  "Department": "R&D", 
  "Status": "Active - Regular Exempt (Full-time)", 
  "ManagerGUID": "695fdc37-42f1-4c19-9ba1-c4fe87454041", 
  "point": {
   "lat": 38.823173, 
   "long": -86.881793
  }, 
  "HiredDate": "2012-01-03"
 }, 
 "score": 0
}
  • This query returned the first 100 results as we specified in offsetLimit().

  • Note that the properties in the document returned are in a different order than the columns in our view that we created for this collection of documents. Building a view lets you put any of the data that you want from documents in a collection into any order you want without affecting the documents themselves.