How to Analyze Queries and create OAK Index definitions in AEM (Adobe Experience Manager)

How to Analyze Queries and create OAK Index definitions in AEM(Adobe Experience Manager)

In AEM there are many tools to diagnosis the AEM repository. Query performance manager is one of them. By using this tool, we can find both slow and popular queries. And, it will explain the given query which index it is being used, how much time will take to run. This will explain the JCR-SQL2, SQL, XPath, and Query Builder queries. 

To open the Query performance Manager tool. From AEM start page Navigate to Tools --> Operations --> Diagnosis --> Query Performance 





















Navigate to Query performance tool. Where you can see the Slow queries and Popular Queries. you can select any query and click on the explain query to see which index definition used and how much time it took to execute. 
















When do we need to create a Query index definition?

When your query executes, you will see the warning logger messages like below 

*WARN* [sling-default-4-com.sony.sie.scheduler.ActivatePageSchedulerUS:05e1d8c7-d160-4ac5-8133-7d63c940ac1b] org.apache.jackrabbit.oak.query.QueryImpl Traversal query (query without index): <your query> consider creating an index 

Or you in Query performance tool you will find the slow queries. To optimize those, you can create Oak Index Definition to index content for your queries. 

How to create Oak Index Definition for your Query 

In AEM there few OOTB index definitions that are created under “/oak:index”. You can create your own index definition under this path. You create the two types of indexes property index or Lucence Index. Oak Index Definition Generator will help you to create Oak index definitions based on your queries. You can input your query it will generate the Index definition format as Text/ JSON / XML. For AEM choose XML format. After generating the XML compare it with any OOTB definition and make sure the property types are matching or not? You may see the below warning messages if the property definition is not configured properly. For property, definitions refer to the documentation

“org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexLookup Expected 'NAMES' as type of
property 'propertyNames' but found 'STRING'. Node - '{ jcr:primaryType = oak:QueryIndexDefinition, 
jcr:createdBy = Ensure Oak Index, declaringNodeTypes = [cq:AuditEvent], propertyNames = cq:time,
jcr:created = 2020-06-03T17:52:47.983Z, type = property, reindex = false, reindexCount = 3, 
:index = { ... } }' 


Refer documentation when to use the Lucence index and Property index. 


How to Debug/ Troubleshoot Sling Context Aware Configuration AEM

If we are using Sling Context-Aware configurations in AEM and something went wrong it's difficult to debug/know from where these values or pulling. We know this in the AEM Web console. 

After open AEM Web console click on the sling tab, you will find the "Context-Aware Configuration" option



After Navigate Context-Aware Configuration web console page you will see a form. You can enter the "Content Path" and "Config name" which you want to debug and click on resolve. if you are debugging a  collection config check the checkbox "Resouce Collection" 



Note: if "cq:conf" property set to your jcr:content node of page sometimes it will read configurations for the path. So keep an eye on "cq:conf" and "sling:configRef" properties.  


To know how to develop with Sling context configurations refer to my post Sling Context Aware Configuration AEM 6.5 Examples



OSGi R7 annotations Examples AEM (Adobe Experience Manager)

OSGi component / service Configs

String Config

 @AttributeDefinition(
        name = "String Property",
        description = "String example",
        type = AttributeType.STRING
    )
    String stringExample() default "String value";

String Array Config

@AttributeDefinition(
name = "String Array Example",
description = "String Array Example",
type = AttributeType.STRING
  )
 String[] stringArrayExample() default {"String1", "String2"};

Dropdown Config

 @AttributeDefinition(
        name = "Dropdown example",
        description = "Dropdown example",
        options = {
            @Option(label = "Option1", value = "Option1"),
            @Option(label = "Option2", value = "Option2"),
            @Option(label = "Option3", value = "Option3")
        }
    )
 String dropdownExample() default StringUtils.EMPTY;

Boolean Config
To declare your  OSGi configuration as boolean you must use the attribute type BOOLEAN
@AttributeDefinition(
        name = "Boolean Property",
        description = "Boolean example",
        type = AttributeType.BOOLEAN
 )

Long Config

 @AttributeDefinition(
     name = "Long Property",
     description = "Sample long property",
     type = AttributeType.LONG
)
  long longExample() default 0L;