How to create oak:index in AEM programatically

613 views Asked by At

How do I create oak:index programatically using xml files so that I don't have to create it manually in AEM6.5?

1

There are 1 answers

0
Alexander Berndt On

The best approach is, to create a content package with indexes. This would also be AEMaaCS compatible.

Here is a sample pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- ====================================================================== -->
    <!-- P A R E N T  P R O J E C T  D E S C R I P T I O N                      -->
    <!-- ====================================================================== -->
    <parent>
        ...
    </parent>

    <!-- ====================================================================== -->
    <!-- P R O J E C T  D E S C R I P T I O N                                   -->
    <!-- ====================================================================== -->
    <artifactId>com.acme.ui.indexes</artifactId>
    <packaging>content-package</packaging>
    <name>ACME - UI Indexes</name>
    <description>oak:index package for ACME</description>

    <!-- ====================================================================== -->
    <!-- B U I L D   D E F I N I T I O N                                        -->
    <!-- ====================================================================== -->
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/content/jcr_root</directory>
            </resource>
        </resources>
        <plugins>
            <!-- ====================================================================== -->
            <!-- V A U L T   P A C K A G E   P L U G I N S                              -->
            <!-- ====================================================================== -->
            <plugin>
                <groupId>org.apache.jackrabbit</groupId>
                <artifactId>filevault-package-maven-plugin</artifactId>
                <configuration>
                    <group>com.acme.aem</group>
                    <name>com.acme.ui.indexes</name>
                    <packageType>application</packageType>
                    <allowIndexDefinitions>true</allowIndexDefinitions>
                    <properties>
                        <!-- Best practise for Oak:Indexes package (but only here) -->
                        <noIntermediateSaves>true</noIntermediateSaves>
                    </properties>
                    <validatorsSettings>
                        <jackrabbit-nodetypes>
                            <options>
                                <!-- As /oak:index is a nt:unstructured, this shall be the default parent-node-type -->
                                <defaultNodeType>nt:unstructured</defaultNodeType>
                            </options>
                        </jackrabbit-nodetypes>
                    </validatorsSettings>
                    <repositoryStructurePackages>
                        <repositoryStructurePackage>
                            <groupId>com.acme.aem</groupId>
                            <artifactId>com.acme.ui.apps.structure</artifactId>
                        </repositoryStructurePackage>
                    </repositoryStructurePackages>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.day.jcr.vault</groupId>
                <artifactId>content-package-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <verbose>true</verbose>
                    <failOnError>true</failOnError>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <!-- ====================================================================== -->
    <!-- D E P E N D E N C I E S                                                -->
    <!-- ====================================================================== -->
    <dependencies>
        <dependency>
            <groupId>com.acme.aem</groupId>
            <artifactId>com.acme.ui.apps.structure</artifactId>
            <version>${project.version}</version>
            <type>zip</type>
        </dependency>
    </dependencies>
</project>

As you have mixed content below /oak:index, you need to specify each custom index in the filter.xml

<?xml version="1.0" encoding="UTF-8"?>
<workspaceFilter version="1.0">
    <filter root="/oak:index/experienceFragmentsIndex-3-custom-1" />
    <filter root="/oak:index/productCatalogLuceneIndex-custom-1" />
</workspaceFilter>

The index-names should follow the convention described in the official documentation.

https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/operations/indexing.html?lang=en