Skip to content

Commit

Permalink
Added a namespace-pattern configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
an1310 committed Jan 23, 2024
1 parent 1456431 commit 80086bc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ The default behavior of Ditto is to index the complete JSON of a thing, which in
* Increased load on the search database, leading to performance degradation and increased database cost.
* Only a few fields are ever used for searching.

In Ditto *3.5.0*, there is now configuration to specify, by namespace, which fields will be included in the search database.
In Ditto *3.5.0*, there is now configuration to specify, by a namespace pattern, which fields will be included in the search database.

To enable this functionality, there are two new options in the `thing-search.conf` configuration:

Expand All @@ -342,22 +342,30 @@ ditto {
search {
namespace-search-include-fields = [
{
namespace = "org.eclipse",
namespace-pattern = "org.eclipse",
search-include-fields = [ "attributes", "features/info" ]
},
{
namespace = "org.eclipse.test",
namespace-pattern = "org.eclipse.test",
search-include-fields = [ "attributes", "features/info/properties/", "features/info/other" ]
}
]
}
```

There is a new implementation of the caching signal enrichment facade provider that must be configured to enable this functionality.
There is a new implementation of the caching signal enrichment facade provider that must be configured to enable this
functionality.

For each namespace, only the selected fields are included in the search database. In the example above, for things in the "org.eclipse" namespace, only the "attributes" and "features/info" paths will be the only fields indexed in the search database. For things in the "org.eclipse.test" namespace, the fields indexed in the search database will only be "attributes", "features/info/properties", and "features/info/other".
For each namespace pattern, only the selected fields are included in the search database. In the example above, for
things in the "org.eclipse" namespace, only the "attributes" and "features/info" paths will be the only fields indexed
in the search database. For things in the "org.eclipse.test" namespace, the fields indexed in the search database will
only be "attributes", "features/info/properties", and "features/info/other".

NOTE: Ditto will automatically add the system-level fields it needs to operate, so no manual configuration of these is necessary.
Important notes:
* Ditto will use the namespace of the thing and match the FIRST namespace-pattern it encounters. So make sure any
configured namespace-patterns are unique enough to match.
* Ditto will automatically add the system-level fields it needs to operate, so no manual configuration of these is
necessary.

## Logging

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,24 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.eclipse.ditto.base.model.common.LikeHelper;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import org.eclipse.ditto.internal.utils.config.DittoConfigError;

import com.typesafe.config.Config;

import static org.eclipse.ditto.base.model.common.ConditionChecker.*;

/**
* This class is the default implementation of the NamespaceSearchIndex config.
* It is instantiated for each namespace search index entry containing the namespace definition and the list of search indexes.
*/
public final class DefaultNamespaceSearchIndexConfig implements NamespaceSearchIndexConfig {

private final String namespace;
private final String namespacePattern;

private final List<String> searchIncludeFields;

private DefaultNamespaceSearchIndexConfig(final ConfigWithFallback configWithFallback) {

this.namespace = configWithFallback.getString(NamespaceSearchIndexConfigValue.NAMESPACE.getConfigPath());
this.namespacePattern = configWithFallback.getString(NamespaceSearchIndexConfigValue.NAMESPACE_PATTERN.getConfigPath());

final List<String> fields = configWithFallback.getStringList(NamespaceSearchIndexConfigValue.SEARCH_INCLUDE_FIELDS.getConfigPath());
if (!fields.isEmpty()) {
Expand All @@ -49,8 +44,8 @@ private DefaultNamespaceSearchIndexConfig(final ConfigWithFallback configWithFal
}
}

private DefaultNamespaceSearchIndexConfig(final String namespace, final Collection<String> fields) {
this.namespace = namespace;
private DefaultNamespaceSearchIndexConfig(final String namespacePattern, final Collection<String> fields) {
this.namespacePattern = namespacePattern;
this.searchIncludeFields = Collections.unmodifiableList(new ArrayList<>(fields));
}

Expand All @@ -66,8 +61,8 @@ public static DefaultNamespaceSearchIndexConfig of(final Config config) {
}

@Override
public String getNamespace() {
return namespace;
public String getNamespacePattern() {
return namespacePattern;
}

@Override
Expand All @@ -84,18 +79,18 @@ public boolean equals(final Object o) {
return false;
}
final DefaultNamespaceSearchIndexConfig that = (DefaultNamespaceSearchIndexConfig) o;
return Objects.equals(namespace, that.namespace) && searchIncludeFields.equals(that.searchIncludeFields);
return Objects.equals(namespacePattern, that.namespacePattern) && searchIncludeFields.equals(that.searchIncludeFields);
}

@Override
public int hashCode() {
return Objects.hash(namespace, searchIncludeFields);
return Objects.hash(namespacePattern, searchIncludeFields);
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"namespace=" + namespace +
"namespace=" + namespacePattern +
", searchIncludeFields=" + searchIncludeFields +
"]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.util.regex.Pattern;

/**
* Provides configuration settings of the namespace-scoped search indexes.
Expand All @@ -26,11 +25,11 @@
public interface NamespaceSearchIndexConfig {

/**
* Returns the namespace definition.
* Returns the namespace pattern definition.
*
* @return the namespace definition
* @return the namespace pattern definition
*/
String getNamespace();
String getNamespacePattern();

/**
* Returns a list of fields that will be explicitly included in the search index.
Expand All @@ -44,7 +43,7 @@ enum NamespaceSearchIndexConfigValue implements KnownConfigValue {
/**
* The namespace value to apply the search indexed fields.
*/
NAMESPACE("namespace", ""),
NAMESPACE_PATTERN("namespace-pattern", ""),

/**
* The list of fields that will be included in the search DB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public CachingSignalEnrichmentFacade getSignalEnrichmentFacade(

// Build a Pattern from the namespace value.
final Pattern namespacePattern = Pattern.compile(
Objects.requireNonNull(LikeHelper.convertToRegexSyntax(namespaceConfig.getNamespace())));
Objects.requireNonNull(LikeHelper.convertToRegexSyntax(namespaceConfig.getNamespacePattern())));

namespaceAndFieldSelector.add(Pair.create(namespacePattern, indexedFields));
}
Expand Down
4 changes: 2 additions & 2 deletions thingsearch/service/src/main/resources/search.conf
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ ditto {
# This configuration is used in conjunction with the new caching-signal-enrichment-facade-provider implementation
# to only index a selected array of JSON pointers scoped by namespace.
namespace-search-include-fields = [
# Example: For the namespace "org.eclipse", only the "attributes" and "features/info" will be indexed in the
# Example: For the namespace-pattern "org.eclipse", only the "attributes" and "features/info" will be indexed in the
# search database.
# {
# namespace = "org.eclipse",
# namespace-pattern = "org.eclipse",
# search-include-fields = [ "attributes", "features/info" ]
# }
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public void underTestReturnsValuesOfConfigFile() {
NamespaceSearchIndexConfig second = underTest.getNamespaceSearchIncludeFields().get(1);

// First config
softly.assertThat(first.getNamespace()).isEqualTo("org.eclipse");
softly.assertThat(first.getNamespacePattern()).isEqualTo("org.eclipse");

softly.assertThat(first.getSearchIncludeFields())
.as(NamespaceSearchIndexConfig.NamespaceSearchIndexConfigValue.SEARCH_INCLUDE_FIELDS.getConfigPath())
.isEqualTo(
List.of("attributes", "features/info"));

// Second config
softly.assertThat(second.getNamespace()).isEqualTo("org.eclipse.test");
softly.assertThat(second.getNamespacePattern()).isEqualTo("org.eclipse.test");

softly.assertThat(second.getSearchIncludeFields())
.as(NamespaceSearchIndexConfig.NamespaceSearchIndexConfigValue.SEARCH_INCLUDE_FIELDS.getConfigPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ ditto {
namespace-search-include-fields = [
# The list of thing paths that are included in the search index.
{
namespace = "org.eclipse",
namespace-pattern = "org.eclipse",
search-include-fields = [ "attributes", "features/info" ]
},
{
namespace = "org.eclipse.test",
namespace-pattern = "org.eclipse.test",
search-include-fields = [ "attributes", "features/info/properties/", "features/info/other" ]
}
]
Expand Down

0 comments on commit 80086bc

Please sign in to comment.