[ELASTIC-25] IndexExtraFieldsGenerator<T>: Add support for multi-value fields and nested nodes Created: 16/Feb/22  Updated: 16/Feb/22

Status: Open
Project: Elasticsearch
Component/s: None
Affects Version/s: 1.0.0
Fix Version/s: None

Type: Improvement Priority: Major
Reporter: wolf bubenik Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Magnolia 6.2.15
Java 11
ES 7.13


Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)

 Description   

Currently the IndexExtraFieldsGenerator<T> handling seems to be restricted to add additional single-value fields. Adding multi-value fields or complex sub-items is not possible, because the return parameter is a Map of Strings.

One option would be to change the interface to return a List<ElasticItem>.



 Comments   
Comment by wolf bubenik [ 16/Feb/22 ]

Here is my patch of the IndexJcrDataFactory:

protected static void addExtraFields(GeneratedIndexPackage indexPackage, Node node, IndexItemConfiguration index, MappingItemConfiguration mappingField) throws ElasticConfigurationException {
    if (index.getExtraFieldsGeneratorClass() != null) {
        IndexExtraFieldsGenerator<Node> extraFieldsGenerator = (IndexExtraFieldsGenerator<Node>) Components.newInstance(index.getExtraFieldsGeneratorClass(), null);
        Map<String, String> dataMap = extraFieldsGenerator.getExtraFields(node);
        dataMap.forEach((key, value) -> {
            ElasticItem elasticItemManually;
            try {
                elasticItemManually = new ElasticItem(TypeSpecification.TEXT, key, node.getPath(), node.getIdentifier());
                elasticItemManually.setValue(value);
                indexPackage.getIndexPackage().put(elasticItemManually.getName(), elasticItemManually);
                indexPackage.setItemsAdded(true);
            } catch (RepositoryException e) {
                log.error("query-manager.elastic-index-app.error.unable_read_node", e);
            }
        });
        // improved extra fields implementation:
        if (extraFieldsGenerator instanceof ExtraElasticItemGenerator) {
            ((ExtraElasticItemGenerator<Node>) extraFieldsGenerator).getExtraElasticItems(node).forEach(item -> {
                    indexPackage.getIndexPackage().put(item.getName(), item);
                    indexPackage.setItemsAdded(true);
                }
            );
        }
    }
} 

with the new Interface to be implemented:

import info.magnolia.elasticsearch.configuration.ElasticItem;

import java.util.List;

/**
 * The extra item generator interface for supporting multi-value and nested object items.
 *
 * @param <T> the type of the source object - javax.jcr.Node for jcr indexing
 * @author wolf.bubenik
 */
public interface ExtraElasticItemGenerator<T> {
    List<ElasticItem> getExtraElasticItems(T source);
} 

 

Generated at Mon Feb 12 01:46:34 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.