The Index Settings Contributor sample demonstrates how to add a custom type mapping to Liferay DXP. You can demo this sample by completing the following steps:
-
Navigate to the Control Panel → Configuration → Search menu.
-
Click Execute for the Reindex all search indexes action.
All properties defined in your
.json
file are added to Liferay DXP’s search engine. This sample adds the following index properties:sampleDate
sampleDouble
sampleLong
sampleText
You’ll verify this next.
-
Find your Liferay DXP’s instance ID. This can be found in the Control Panel → Configuration → Virtual Instances menu.
-
Navigate to the following URL:
http://localhost:9200/liferay-[INSTANCE_ID]/_mapping/LiferayDocumentType?pretty
Be sure to insert your instance ID into the URL.
-
Verify the added properties are listed.
Figure 1: This sample added four new index properties.
What API(s) and/or code components does this sample highlight?
This sample leverages the IndexSettingsContributor API.
How does this sample leverage the API(s) and/or code component?
Liferay’s search engine provides an API to define custom mappings. To use it, follow these fundamental steps:
-
Define the new mapping. In this sample, the mapping is defined in the
META-INF/mappings/resources/index-type-mappings.json
file. Notice that the default document for Liferay DXP is calledLiferayDocumentType
. The mapping’s features can be found in Elasticsearch’s docs. -
Inject the mapping into Elasticsearch. The
IndexSettingsContributor
class’ components are invoked during the reindexing stage and receive aTypeMappingsHelper
as a hook to add new mappings.
This sample has two classes:
-
ResourceUtil
: reads the.json
file. -
IndexSettingsContributor
: allows the addition of type mappings on Liferay DXP’s search engine.
The IndexSettingsContributor
’s contribute
method adds the type mappings:
@Override
public void contribute(
String indexName, TypeMappingsHelper typeMappingsHelper) {
try {
String mappings = ResourceUtil.readResouceAsString(
"META-INF/resources/mappings/index-type-mappings.json");
typeMappingsHelper.addTypeMappings(indexName, mappings);
}
catch (Exception e) {
e.printStackTrace();
}
}
For the ResourceUtil.readResouceAsString
parameter, you should pass the path
for the .json
file that contains the properties to be added.
Also, it is important to highlight the IndexSettingsContributor
’s @Component
annotation that registers a new service to the OSGi container:
@Component(
immediate = true,
service = com.liferay.portal.search.elasticsearch6.settings.IndexSettingsContributor.class
)
> If using Elasticsearch 7, the value of the `service` property is instead `com.liferay.portal.search.elasticsearch7.settings.IndexSettingsContributor.class`.
This sample demonstrates the essentials needed to contribute your own index settings.
Where Is This Sample?
There are three different versions of this sample, each built with a different build tool: