Exploring op.fromView()

Accessing TDE and Relational Views in MarkLogic Optic API

personClever Llamas
CleverLlamasMinimum Llamaverse Version: 1.4
databaseMinimum MarkLogic Version: 8

Introduction

The op.fromView() function is a foundational data access method in the MarkLogic Optic API. It allows you to source data from Template Driven Extraction (TDE) views, relatonal views or query-based-views, making it possible to treat document data as rows and columns.

Summary

Use op.fromView() when you want to query structured data exposed by TDE templates or relational views. This function is often the starting point for plans that need to join, filter, or aggregate data from documents in a relational manner.

Parameters

NameDatatypeRequiredNotes
schemaStringYesThe name of the schema that contains the view.
viewStringYesThe name of the view to source data from.
qualifierStringNoAn optional qualifier for the view, used to specify name for the view at runtime. See Understanding Qualifiers
systemColsStringNoAn optional named fragment ID column. This will be explored with some of the join realted articles. See Understanding Fragment ID Columns for more details.

Default Usage

In this example, we did not use a qualifier or system column, so the plan will use the default view and schema.

We also used the most basic options in the op.select() function. Once we dive deeper into joins, this can be an issue if column names are not unique across the views.

const op = require('/MarkLogic/optic');
const llamaPlan = op.fromView('llamaverse', 'llamas')
  .select(['name', 'placeOfBirth'])
  .orderBy('name')
  .limit(3)
llamaPlan.result()

{"llamaverse.llamas.name":"Aaron", "llamaverse.llamas.placeOfBirth":"Cusco, Peru"}
{"llamaverse.llamas.name":"Angela", "llamaverse.llamas.placeOfBirth":"Cusco, Peru"}
{"llamaverse.llamas.name":"Anthony", "llamaverse.llamas.placeOfBirth":"Santiago, Chile"}

You can see in the results that the data is scoped by the schema and view name - just like database and table names in a relational database.

Explicit View and Column References

Building off of the previous example, we can also be more specific about the column names using op.fromViewCol() which scopes the columnt name to the view(not the schema). Perhaps in this simple example it is not necessary, but in more complex situations with joins, this is critical to understand. At clever Llamas we prefer to be specific to avoid issues in the future.

It should be noted that there is a companion function called op.col() that can be used to reference columns in a more generic way. Realistically, forget you ever heard of it and be specific.

const op = require('/MarkLogic/optic');
const llamaPlan = op.fromView('llamaverse', 'llamas')
  .select([op.viewCol("llamas", "name"), op.viewCol("llamas", "placeOfBirth")])
  .orderBy('name')
  .limit(3)
llamaPlan.result()

{"llamaverse.llamas.name":"Aaron", "llamaverse.llamas.placeOfBirth":"Cusco, Peru"}
{"llamaverse.llamas.name":"Angela", "llamaverse.llamas.placeOfBirth":"Cusco, Peru"}
{"llamaverse.llamas.name":"Anthony", "llamaverse.llamas.placeOfBirth":"Santiago, Chile"}

The results are identical.

Change the View Name at runtime

Perhasps You would like to use the same view in different ways (think table aliases in SQL) or You really hate the name provides. You can do this by using the qualifier parameter. This allows you to specify a different name for the view at runtime.

const op = require('/MarkLogic/optic');
const llamaPlan = op.fromView('llamaverse', 'llamas', "llamaList")
  .select([op.viewCol("llamaList", "name"), op.viewCol("llamaList", "placeOfBirth")])
  .orderBy('name')
  .limit(3)
llamaPlan.result()
{"llamaList.name":"Aaron", "llamaList.placeOfBirth":"Cusco, Peru"}
{"llamaList.name":"Angela", "llamaList.placeOfBirth":"Cusco, Peru"}
{"llamaList.name":"Anthony", "llamaList.placeOfBirth":"Santiago, Chile"}

Now we see a difference - llamaList is the name of the view at runtime, not llamaverse.llamas.

We will continue this example under the article for op.select() where we will take this one step further and explore one more way to manage the view names in your Optic plans.

Conclusion

op.fromView() is essential for leveraging MarkLogic’s ability to present document data as relational tables, enabling powerful multi-model queries.