Custom Object APIs
Powered by Liferay

Using nestedFields to Query Related Entries

Liferay 7.4 U69+/GA69+

The nestedFields parameter returns multiple levels of related object entries in a single GET request. Pass the nestedFields parameter with the relationship names to include in the query, separating each relationship with a comma: nestedFields=[firstObjectRelationship],[secondObjectRelationship]. If the relationships span multiple levels, set the nestedFieldsDepth parameter to the depth you need. You can include up to five levels (e.g., nestedFieldsDepth=5).

Warning

Using the search parameter to search multiple levels of nestedFields impacts performance which worsens for every additional level of depth. To search multiple levels of related objects, separate the query into multiple requests with only one relationship per query. This approach avoids compounding performance hits.

Tip

The nestedFields parameter optimizes your request by retrieving entries with related entries that would otherwise require multiple requests. To return only the related entries, Liferay provides dedicated relationship APIs. See Using Relationship REST APIs for an introduction.

To proceed, set up a new Liferay 7.4 instance and prepare the provided tutorial code. Then run the scripts to create related entries and query them using the nestedFields parameter.

Note

Currently, access to nested fields or relationships is unavailable when using a custom object on the many side of a one-to-many relationship with a system object. Keep this in mind when planning how to retrieve your data.

Setting Up a Liferay Instance

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.132-ga132

Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.

Next, create and relate three object definitions.

  1. Open the Global Menu (Global Menu), go to the Control Panel tab, and click Objects.

  2. Create three object drafts.

    First Object:

    FieldValue
    LabelAble
    Plural LabelAbles
    NameAble

    Second Object:

    FieldValue
    LabelBaker
    Plural LabelBakers
    NameBaker

    Third Object:

    FieldValue
    LabelCharlie
    Plural LabelCharlies
    NameCharlie
  3. Add the name text field to each object draft.

    LabelField NameTypeRequired
    Namename