How to Use Liferay Objects for Efficient Data Filtering and Searching Using Headless APIs

blog-banner

Liferay's prime objective is to increase its user interface in terms of functionality and features. One of the new feature is Objects. Let's see how to filter and search custom object entries using Liferay Headless APIs.

For this example, we've used Liferay 7.4.3 GA112 version.

What is Liferay "Object" ?

Liferay object is a method of making a custom entity without service builder or deploying the module. You can create customized apps personalized to your company's needs without writing a single line of code using Liferay Objects. This involves creating new fields, connections, operations, relationships, layouts, views, validations, and customized states.

What are the prerequisites?

  • Liferay DXP/Portal 7.4
  • Basic knowledge of Liferay
  • Liferay Objects
  • Liferay Objects Headless API

For this example, we have created an entity to manage Dealer information. The following are the attributes that we’ll store for dealers.

Object Name: Dealer

Dealer Object Fields

Liferay Object Filtering Techniques

Liferay allows you to filter object records based on specific fields using a variety of operators and functions for complex filtering with different criteria. The available operators include:

  • Comparison Operators: eq, ne, lt, le, gt, ge, startswith
  • Logical Operators: and, or, not
  • Grouping Operators: ()
  • String Function contains

Liferay Headless API URL to apply filter for dealer object

  • http://localhost:8080/o/api?endpoint=http://localhost:8080/o/c/dealers/openapi.json

In this example, we'll see different cases where you can filter data based on attributes from the Dealers.

  1. Filter by Name
  2. Filter by Type
  3. Filter by Account Number
  4. Filter by Registration Date
  5. Filter by Account Expiry Date
  6. Filter by Dealer Code
  7. Filter by Model Rate
  8. Filter by Labor Rate
  9. Filter by Is Licensed

Filtering with Text and Picklist fields:

Filter by Dealer Name (Text Field)

  • ?filter=dealerName eq 'John Doe'search with dealername input search with dealername output
  • ?filter=dealerName ne 'John Doe'
  • ?filter=contains(dealerName, 'kerl') search with dealer keyword input search with dealer keyword output
  • ?filter=startswith(dealerName, 'Jack') starts with dealername input starts with dealername output

Filter by Dealer Type (Picklist Field)

  • ?filter=dealerType eq 'carBroker'
  • ?filter=dealerType ne 'carBroker'
  • ?filter=contains(dealerType, 'car')
  • ?filter=startswith(dealerType, 'Certified') search with picklist input search with picklist output

    Note: You can only filter using the key to the picklist options. Liferay doesn't support filtering based on the display name of a picklist value.

Filtering with Encrypted Field:

Filter by Account Number (Encrypted Field)

To use encrypted fields, first you need to add an encryption algorithm and key to your portal properties. After that you can create an encrypted field for your object.

Note: Required Liferay Version: 7.4 U72+ / GA72+

  • object.encryption.algorithm=AES
  • object.encryption.key=l1AN3oLKGdrLhpEyLBQOrA== (Your Encryption Key)

Supported algorithm values include AES, Blowfish, and DES. Encryption key values must use base64 encoding.

  • ?filter=accountNumber eq 'Encrypted account numbersearch with encrypted field input search with encrypted field output

Filtering Date fields:

Filter by Registration Date (Date Field)

  • ?filter=registrationDate eq 2024-12-11 search with date field input search with date field output

Filter by Account Expiry Date (DateTime Field)

  • ?filter=accountExpiryDate eq 2027-12-31T23:00:00.000Z
  • ?filter=accountExpiryDate le 2030-12-31T23:00:00.000Z search with date and time field input search with date and time field output

Filtering with Numeric and Decimal fields:

Filter with Numeric and Decimal fields:

  • ?filter=dealerCode eq 312001

Filter by Model Rate (Decimal Field)

  • ?filter=modelRate eq 11.49
  • ?filter=modelRate gt 11search with decimal field input search with decimal field output

Filter by by Labor Rate (Precision Decimal Field)

  • ?filter=laborRate ge 10
  • ?filter=laborRate eq 10.45

NOTE: You can use other comparison operator's like ne, gt, ge, lt and le with Integer, Long Integer, Decimal, Precision Decimal, Date and Date & Time fields for filtering.

Filtering with Boolean Field:

Filter by Is Licensed (Boolean Field)

  • ?filter= isLicensed eq true
  • ?filter=isLicensed ne falsesearch with Boolean field input search with boolean field output

Advanced Data filtering with Logical operator and grouping.

  • ?filter=dealerType eq 'carBroker' or dealerType eq 'bikeDealer'search with complex filter input search with complex filter output
  • ?filter=dealerType eq 'carBroker' and contains(dealerName, 'john')
  • ?filter=(dealerType eq 'carBroker' or dealerType eq 'bikeDealer') and registrationDate eq 2024-12-11

Liferay Object Searching Methods

Liferay's Headless APIs support full-text search for certain fields. Full-text search works best with Text, Rich Text, and Picklist fields, which are enabled for search. It allows you to search across these fields using keywords.

  • Text and Rich Text Fields: Apply search by full-text and keywords with these fields.
  • Picklist Fields: Apply search for picklist values by their key (not the name)
  • Encrypted Fields: Full-text search doesn't apply to encrypted fields
  • Numeric and Decimal Fields: You must use exact values rather than searching with partial keywords.complex search input complex search output

Conclusion

Liferay Objects Headless API provides powerful filtering and search capabilities. You can filter results based on specific field criteria, such as dealer type, account expiry date, or encrypted account numbers. The full-text search functionality provides enhanced search capability to search across multiple fields. This combination of advanced filtering and searching feature enables more dynamic and efficient data management within Liferay DXP.

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium