[ 
https://issues.apache.org/jira/browse/OAK-12491?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Thomas Mueller reassigned OAK-12491:
------------------------------------

    Assignee: Thomas Mueller

> Indexing don't pick content updates for index updates using function
> --------------------------------------------------------------------
>
>                 Key: OAK-12491
>                 URL: https://issues.apache.org/jira/browse/OAK-12491
>             Project: Jackrabbit Oak
>          Issue Type: Bug
>          Components: indexing
>            Reporter: Mohit Kataria
>            Assignee: Thomas Mueller
>            Priority: Major
>
> When an Oak Lucene index definition configures a function-based index (e.g. 
> function = "first([sling:alias])") without also defining a separate property 
> index on the underlying property (e.g. sling:alias), updates to that property 
> on existing nodes are not detected during incremental or synchronous indexing.
> Root Cause:
> In IndexDefinition.java (lines 1395-1406):
> When a property definition has pd.function != null, it is added to 
> functionRestrictions and "continue;" is called. It is never added to 
> propDefns (which populates propConfigs).
> As a result:
> 1. indexingRule.isIndexed(propertyName) returns false for the property used 
> by the function.
> 2. When the property is modified on an existing node, 
> FulltextIndexEditor.propertyChanged() checks indexingRule.isIndexed(name). 
> Since it returns false, propertiesChanged remains false.
> 3. In FulltextIndexEditor.leave(), the node is skipped and not re-indexed.
> Resolution / Workaround:
> Adding a dedicated property definition for the underlying property (e.g. name 
> = "sling:alias", propertyIndex = true) alongside the function definition 
> ensures indexingRule.isIndexed("sling:alias") returns true, allowing 
> FulltextIndexEditor to detect changes on existing nodes and reindex the 
> function value synchronously on commit.
> Automated Tests:
> Two unit tests demonstrating both the failure and resolution have been added 
> to HybridIndexTest.java:
> 1. testSlingResourceResolverFunctionIndexAloneDoesNotDetectPropertyUpdate:
> Shows that when only the function is configured, updating sling:alias on 
> existing nodes is not detected or re-indexed.
> 2. testSlingResourceResolverSyncIndex:
> Shows that declaring sling:alias as a property rule alongside the function 
> index enables synchronous re-indexing on commit across multiple update phases.
>  
> {code:java}
> @Test
> public void 
> testSlingResourceResolverFunctionIndexAloneDoesNotDetectPropertyUpdate() 
> throws Exception {
>     Tree idx = 
> root.getTree("/").getChild("oak:index").addChild("slingResourceResolver-funcOnly");
>     idx.setProperty(JcrConstants.JCR_PRIMARYTYPE, "oak:QueryIndexDefinition", 
> Type.NAME);
>     idx.setProperty("type", "lucene", Type.STRING);
>     idx.setProperty("compatVersion", 2L, Type.LONG);
>     idx.setProperty("async", List.of("async", "sync"), Type.STRINGS);
>     idx.setProperty("evaluatePathRestrictions", true, Type.BOOLEAN);
>     idx.setProperty("includedPaths", List.of("/content"), Type.STRINGS);
>     idx.setProperty("queryPaths", List.of("/content"), Type.STRINGS);
>     Tree indexRules = idx.addChild("indexRules");
>     indexRules.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     Tree ntBase = indexRules.addChild("nt:base");
>     ntBase.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     Tree properties = ntBase.addChild("properties");
>     properties.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     // ONLY function property definition, NO explicit sling:alias property 
> definition
>     Tree aliasProp = properties.addChild("alias");
>     aliasProp.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     aliasProp.setProperty("name", "sling:alias", Type.STRING);
>     aliasProp.setProperty("function", "first([sling:alias])", Type.STRING);
>     aliasProp.setProperty("propertyIndex", true, Type.BOOLEAN);
>     aliasProp.setProperty("ordered", true, Type.BOOLEAN);
>     root.commit();
>     runAsyncIndex();
>     setTraversalEnabled(false);
>     // 1. Initial creation of content: indexed because node is new 
> (!before.exists())
>     Tree node1 = createPath("/content/node1");
>     node1.setProperty("sling:alias", List.of("alias1", "extra1"), 
> Type.STRINGS);
>     Tree node2 = createPath("/content/node2");
>     node2.setProperty("sling:alias", List.of("alias2", "extra2"), 
> Type.STRINGS);
>     root.commit();
>     String query = "select [jcr:path] from [nt:base] where 
> isdescendantnode('/content') and first([sling:alias]) >= '' order by 
> first([sling:alias])";
>     assertThat(explain(query), 
> containsString("/oak:index/slingResourceResolver-funcOnly"));
>     // Initial creation is queryable
>     assertQuery(query, List.of("/content/node1", "/content/node2"));
>     // 2. Update sling:alias on existing nodes
>     node1.setProperty("sling:alias", List.of("aliasZ", "extra1"), 
> Type.STRINGS);
>     node2.setProperty("sling:alias", List.of("aliasA", "extra2"), 
> Type.STRINGS);
>     root.commit();
>     // Because sling:alias is not in propDefns, isIndexed("sling:alias") was 
> false.
>     // The update was NOT re-indexed on commit: the query still returns the 
> old order [node1, node2]
>     // rather than the updated order [node2 (aliasA), node1 (aliasZ)].
>     List<String> result = executeQuery(query, "JCR-SQL2");
>     assertEquals("Without sling:alias property definition, existing node 
> property update is not detected",
>             List.of("/content/node1", "/content/node2"), result);
> }
> @Test
> public void testSlingResourceResolverSyncIndexWithPropertyIndexFalse() throws 
> Exception {
>     runSlingResourceResolverSyncTest("slingResourceResolver-propIdxFalse", 
> propAlias -> {
>         propAlias.setProperty("propertyIndex", false, Type.BOOLEAN);
>     });
> }
> @Test
> public void testSlingResourceResolverSyncIndexWithIndexFalse() throws 
> Exception {
>     runSlingResourceResolverSyncTest("slingResourceResolver-indexFalse", 
> propAlias -> {
>         propAlias.setProperty("index", false, Type.BOOLEAN);
>     });
> }
> @Test
> public void testSlingResourceResolverSyncIndexWithPropertyIndexTrue() throws 
> Exception {
>     runSlingResourceResolverSyncTest("slingResourceResolver-propIdxTrue", 
> propAlias -> {
>         propAlias.setProperty("propertyIndex", true, Type.BOOLEAN);
>     });
> }
> private void runSlingResourceResolverSyncTest(String indexName, 
> java.util.function.Consumer<Tree> propAliasConfigurator) throws Exception {
>     Tree idx = root.getTree("/").getChild("oak:index").addChild(indexName);
>     idx.setProperty(JcrConstants.JCR_PRIMARYTYPE, "oak:QueryIndexDefinition", 
> Type.NAME);
>     idx.setProperty("type", "lucene", Type.STRING);
>     idx.setProperty("compatVersion", 2L, Type.LONG);
>     idx.setProperty("async", List.of("async", "sync"), Type.STRINGS);
>     idx.setProperty("evaluatePathRestrictions", true, Type.BOOLEAN);
>     idx.setProperty("includedPaths", List.of("/content"), Type.STRINGS);
>     idx.setProperty("queryPaths", List.of("/content"), Type.STRINGS);
>     Tree indexRules = idx.addChild("indexRules");
>     indexRules.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     Tree ntBase = indexRules.addChild("nt:base");
>     ntBase.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     Tree properties = ntBase.addChild("properties");
>     properties.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     // Property definition for sling:alias so changes to sling:alias mark the 
> node as changed
>     Tree propAlias = properties.addChild("propAlias");
>     propAlias.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     propAlias.setProperty("name", "sling:alias", Type.STRING);
>     propAliasConfigurator.accept(propAlias);
>     Tree aliasProp = properties.addChild("alias");
>     aliasProp.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
> JcrConstants.NT_UNSTRUCTURED, Type.NAME);
>     aliasProp.setProperty("function", "first([sling:alias])", Type.STRING);
>     aliasProp.setProperty("propertyIndex", true, Type.BOOLEAN);
>     aliasProp.setProperty("ordered", true, Type.BOOLEAN);
>     root.commit();
>     // Run initial index update to initialize the index tracker on empty repo 
> (no reindex)
>     runAsyncIndex();
>     setTraversalEnabled(false);
>     // 1. Initial content: alias as an array
>     Tree node1 = createPath("/content/node1");
>     node1.setProperty("sling:alias", List.of("alias1", "extra1"), 
> Type.STRINGS);
>     Tree node2 = createPath("/content/node2");
>     node2.setProperty("sling:alias", List.of("alias2", "extra2"), 
> Type.STRINGS);
>     Tree node3 = createPath("/content/node3");
>     node3.setProperty("sling:alias", List.of("alias3"), Type.STRINGS);
>     // Commit content
>     root.commit();
>     String query = "select [jcr:path] from [nt:base] where 
> isdescendantnode('/content') and first([sling:alias]) >= '' order by 
> first([sling:alias])";
>     assertThat(explain(query), containsString("/oak:index/" + indexName));
>     // Query immediately without running async indexing
>     assertQuery(query, List.of("/content/node1", "/content/node2", 
> "/content/node3"));
>     // 2. Only update alias in test (using array)
>     node1.setProperty("sling:alias", List.of("alias5", "extra1"), 
> Type.STRINGS);
>     node2.setProperty("sling:alias", List.of("alias0", "extra2"), 
> Type.STRINGS);
>     node3.setProperty("sling:alias", List.of("alias4"), Type.STRINGS);
>     // Commit updates
>     root.commit();
>     // Query immediately after commit without any async indexing run
>     // New order by first([sling:alias]): node2 (alias0), node3 (alias4), 
> node1 (alias5)
>     assertQuery(query, List.of("/content/node2", "/content/node3", 
> "/content/node1"));
>     // 3. Another update phase: only update sling:alias array properties again
>     // (demonstrating new array values and array element reordering)
>     node1.setProperty("sling:alias", List.of("aliasA", "aliasZ"), 
> Type.STRINGS);
>     node2.setProperty("sling:alias", List.of("aliasZ", "alias0"), 
> Type.STRINGS);
>     node3.setProperty("sling:alias", List.of("aliasB", "aliasC"), 
> Type.STRINGS);
>     // Commit updates
>     root.commit();
>     // Query immediately after commit without any async indexing run
>     // New order by first([sling:alias]): node1 (aliasA), node3 (aliasB), 
> node2 (aliasZ)
>     assertQuery(query, List.of("/content/node1", "/content/node3", 
> "/content/node2"));
>     // Also verify seek-based query works synchronously against the newly 
> updated data
>     String seekQuery = "select [jcr:path] from [nt:base] where 
> isdescendantnode('/content') and first([sling:alias]) >= 'aliasB' order by 
> first([sling:alias])";
>     assertQuery(seekQuery, List.of("/content/node3", "/content/node2"));
> }
> {code}
>  



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to