October 13, 2020
Estimated Post Reading Time ~

AEM Integration with Algolia Java API


As we have already seen so many integration with search servers like Solr and Elastic. Now we have something new as part of enhancing search capability in AEM.
Algolia is really easy to manage and it’s really simple to set it up. It provides different API for the connection to the Algolia server.

Below are the list of some API that Algolia supports:
https://www.algolia.com/doc/api-client/getting-started/install/java/?language=java
Algolia’s APIs

Lets start with creating our own Algolia account. It provides a 17 days trial period. So yeah this the enough time to explore all it’s features and integrations.

Creating Account:
1. Create you trial account on alogolia.com.
2. After logging in you can create you own index here and start indexing the data using that index name. I have take dev_initial as my index name.

Dashboard for the indexed data

I am going to use the JAVA Api provided by Algolia.
To use this API we need to add the following dependencies in POM.xml

<!-- Algolia library with Apache HTTP requester (compatible with Java 8 and above) -->
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-core</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-apache</artifactId>
<version>3.10.0</version>
</dependency>

<!-- Algolia library with JDK 11 native HTTP client (compatible with Java 11 and above) -->
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-core</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-java-net</artifactId>
<version>3.10.0</version>
</dependency>

<!-- Algolia library with Apache HTTP requester bundled as a single uber JAR (compatible with Java 8 and above) -->
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-apache-uber</artifactId>
<version>3.10.0</version>
</dependency>
After updating the POM.xml file we need to run the mvn compile command to download the dependencies.
Now we have all the dependencies available to integrate AEM with Algolia.

I have used a test servlet to index some random data.

You should have ApplicationID and Admin API Key to connect in AEM , which you can get it on your Algolia account https://www.algolia.com/api-keys
Which will be used in below code snippet:

SearchClient client = DefaultSearchClient.create("YourApplicationID", "YourAdminAPIKey");






Servlet to index data
class Contact {

private String firstname;
private String lastname;
private int followers;
private String company;
private String objectID;

// Getters/setters ommitted
}

Note: Before deploying the code deploy the algoliasearch-apache-uber.jar (3.10.0) on the AEM felix console from https://repo1.maven.org/maven2/com/algolia/algoliasearch-apache-uber/3.10.0/

After indexing the data we can see the data is stored at Algolia server for search implementation. It’s really easy to manage using the dashboard availability and easy to manage the data on the Algolia server.


Data got stored on the Server

In case of any query, you can post your queries on https://discourse.algolia.com/t/com-algolia-search-cannot-be-resolved/10222/12 and get it resolved. Algolia Team gets back to you very quickly. As they are still enhancing and building this API for AEM OSGI bundles so we might face some bundle issues in the AEM console.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.