Mastering Consuming Liferay Headless APIs

Retrieving Clarity's Product Data with Search APIs

Liferay’s search APIs provide tools enabling precise and scalable search experiences. Clarity’s team previously tested retrieving product data using GET requests and GraphQL queries. Next, they want to expand their toolset by testing with search APIs to filter, sort, and structure search results. In these exercises, you’ll execute search API requests to return results and suggestions.

Exercise: Retrieving Clarity’s Product Information with the Search API

Here, you'll use the search API to find, filter, and paginate product information.

  1. Sign in as the Clarity Admin user.

    • Username: admin@clarityvisionsolutions.com

    • Password: learn

  2. Navigate to the API Explorer at http://localhost:8080/o/api.

  3. Click on the REST Application drop-down and select search/v1.0.

  4. Under the SearchResult section, expand the postSearchPage endpoint.

  5. Set com.liferay.commerce.product.model.CPDefinition for the entryClassNames parameter.

  6. Replace the sample code in the Request body with this code snippet:

    {
      "attributes": {
        "search.empty.search": "true"
      }
    }
  7. Click Execute.

  8. Examine the response body to verify it retrieved 11 entries. At the bottom, you should see:

    "lastPage": 1,
    "page": 1,
    "pageSize": 20,
    "totalCount": 11
  9. Scroll up, enter sunglasses lenses in the search parameter.

  10. Click Execute.
    The response body should now include six items:

    "lastPage": 1,
    "page": 1,
    "pageSize": 20,
    "totalCount": 6
  11. Set the following values for the respective parameters:

    Parameter Value
    page 2
    pageSize 5
  1. Click Execute.
    There should be six products matching the search, meaning the second page will contain only one item. Here’s what you’ll find in the response body:

    {
      "items": [
        {
          "dateCreated": "2024-08-29T16:22:12Z",
          "dateModified": "2024-08-29T16:22:45Z",
          "description": "",
          "score": 14.886782,
          "title": "Single Vision Lenses"
        }
      ],
      "lastPage": 2,
      "page": 2,
      "pageSize": 5,
      "totalCount": 6
    }

This approach executes the search using the underlying search engine, optimizing performance by leveraging the engine’s indexed results.

Exercise: Extending Clarity’s Search Results with Suggestions

Here, you'll use the search suggestions API to retrieve suggestions as a user starts typing “sunglasses.”

  1. While viewing the search endpoints in the API explorer, expand the postSuggestionsPage endpoint.

     

  2. Set the following values for the respective parameters:

    Parameter Value
    currentURL /search
    destinationFriendlyURL /search
    plid 1
    search sun
  3. Input the following code to the Request body:

    [{"contributorName":"basic","displayGroupName":"suggestions","size":5}]
  4. Click Execute.

  5. Examine the response body to verify it includes exactly five suggestions, providing a headless payload mirroring the UI’s search bar suggestions.

Conclusion

Great! You’ve successfully leveraged Liferay’s search APIs to retrieve and filter search results. Harnessing these APIs provides Clarity’s foundation for enhancing dashboards to rapidly filter and sort through extensive data.

Next, you'll review what you've learned before moving on to the next module.

  • Exercise: Retrieving Clarity’s Product Information with the Search API

  • Exercise: Extending Clarity’s Search Results with Suggestions

  • Conclusion

Loading Knowledge

Capabilities

Product

Education

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy