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).
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.
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.
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.
Creating Related Object Definitions
-
Open the Global Menu (
), go to the Control Panel tab, and click Objects. -
Create three object drafts.
First Object:
Field Value Label AblePlural Label AblesName AbleSecond Object:
Field Value Label BakerPlural Label BakersName BakerThird Object:
Field Value Label CharliePlural Label CharliesName Charlie -
Add the
nametext field to each object draft.Label Field Name Type Required Namename