Author: tommaso Date: Mon Mar 23 13:06:02 2015 New Revision: 1668624 URL: http://svn.apache.org/r1668624 Log: OAK-2611 - suggester only updated if used for suggestions
Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinition.java jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorContext.java jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinitionTest.java Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinition.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinition.java?rev=1668624&r1=1668623&r2=1668624&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinition.java (original) +++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinition.java Mon Mar 23 13:06:02 2015 @@ -564,6 +564,25 @@ class IndexDefinition implements Aggrega return ntBaseRule != null; } + public boolean isSuggestEnabled() { + boolean suggestEnabled = false; + for (IndexingRule indexingRule : definedRules) { + for (PropertyDefinition propertyDefinition : indexingRule.propConfigs.values()) { + if (propertyDefinition.useInSuggest) { + suggestEnabled = true; + break; + } + } + for (NamePattern np : indexingRule.namePatterns) { + if (np.getConfig().useInSuggest) { + suggestEnabled = true; + break; + } + } + } + return suggestEnabled; + } + public class IndexingRule { private final String baseNodeType; private final String nodeTypeName; Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorContext.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorContext.java?rev=1668624&r1=1668623&r2=1668624&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorContext.java (original) +++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorContext.java Mon Mar 23 13:06:02 2015 @@ -171,32 +171,38 @@ public class LuceneIndexEditorContext { } } + /** + * eventually update suggest dictionary + * @throws IOException if suggest dictionary update fails + */ private void updateSuggester() throws IOException { - // eventually update suggest dictionary - boolean updateSuggester = false; - NodeBuilder suggesterStatus = definitionBuilder.child(":suggesterStatus"); - if (suggesterStatus.hasProperty("lastUpdated")) { - PropertyState suggesterLastUpdatedValue = suggesterStatus.getProperty("lastUpdated"); - Calendar suggesterLastUpdatedTime = ISO8601.parse(suggesterLastUpdatedValue.getValue(Type.DATE)); - int updateFrequency = definition.getSuggesterUpdateFrequencyMinutes(); - suggesterLastUpdatedTime.add(Calendar.MINUTE, updateFrequency); - if (Calendar.getInstance().after(suggesterLastUpdatedTime)) { + if (definition.isSuggestEnabled()) { + + boolean updateSuggester = false; + NodeBuilder suggesterStatus = definitionBuilder.child(":suggesterStatus"); + if (suggesterStatus.hasProperty("lastUpdated")) { + PropertyState suggesterLastUpdatedValue = suggesterStatus.getProperty("lastUpdated"); + Calendar suggesterLastUpdatedTime = ISO8601.parse(suggesterLastUpdatedValue.getValue(Type.DATE)); + int updateFrequency = definition.getSuggesterUpdateFrequencyMinutes(); + suggesterLastUpdatedTime.add(Calendar.MINUTE, updateFrequency); + if (Calendar.getInstance().after(suggesterLastUpdatedTime)) { + updateSuggester = true; + } + } else { updateSuggester = true; } - } else { - updateSuggester = true; - } - if (updateSuggester) { - DirectoryReader reader = DirectoryReader.open(writer, false); - try { - SuggestHelper.updateSuggester(reader); - suggesterStatus.setProperty("lastUpdated", ISO8601.format(Calendar.getInstance()), Type.DATE); - } catch (Throwable e) { - log.warn("could not update suggester", e); - } finally { - reader.close(); + if (updateSuggester) { + DirectoryReader reader = DirectoryReader.open(writer, false); + try { + SuggestHelper.updateSuggester(reader); + suggesterStatus.setProperty("lastUpdated", ISO8601.format(Calendar.getInstance()), Type.DATE); + } catch (Throwable e) { + log.warn("could not update suggester", e); + } finally { + reader.close(); + } } } } Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinitionTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinitionTest.java?rev=1668624&r1=1668623&r2=1668624&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinitionTest.java (original) +++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexDefinitionTest.java Mon Mar 23 13:06:02 2015 @@ -585,6 +585,40 @@ public class IndexDefinitionTest { assertTrue(!idxDefn.getApplicableIndexingRule(TestUtil.NT_TEST).getNotNullCheckEnabledProperties().isEmpty()); } + @Test + public void testSuggestEnabledOnNamedProp() throws Exception { + NodeBuilder rules = builder.child(INDEX_RULES); + TestUtil.child(rules, "oak:TestNode/properties/prop2") + .setProperty(LuceneIndexConstants.PROP_NAME, "foo") + .setProperty(LuceneIndexConstants.PROP_USE_IN_SUGGEST, true); + root = registerTestNodeType(builder).getNodeState(); + IndexDefinition idxDefn = new IndexDefinition(root, builder.getNodeState()); + assertTrue(idxDefn.isSuggestEnabled()); + } + + @Test + public void testSuggestEnabledOnRegexProp() throws Exception { + NodeBuilder rules = builder.child(INDEX_RULES); + rules.child(TestUtil.NT_TEST); + TestUtil.child(rules, "oak:TestNode/properties/prop2") + .setProperty(LuceneIndexConstants.PROP_NAME, ".*") + .setProperty(LuceneIndexConstants.PROP_IS_REGEX, true) + .setProperty(LuceneIndexConstants.PROP_USE_IN_SUGGEST, true); + root = registerTestNodeType(builder).getNodeState(); + IndexDefinition idxDefn = new IndexDefinition(root, builder.getNodeState()); + assertTrue(idxDefn.isSuggestEnabled()); + } + + @Test + public void testSuggestDisabled() throws Exception { + NodeBuilder rules = builder.child(INDEX_RULES); + TestUtil.child(rules, "oak:TestNode/properties/prop2") + .setProperty(LuceneIndexConstants.PROP_NAME, "foo"); + root = registerTestNodeType(builder).getNodeState(); + IndexDefinition idxDefn = new IndexDefinition(root, builder.getNodeState()); + assertFalse(idxDefn.isSuggestEnabled()); + } + //TODO indexesAllNodesOfMatchingType - with nullCheckEnabled private static IndexingRule getRule(IndexDefinition defn, String typeName){