1

我想map<String, Integer>通过使用映射键作为字段名称来索引 a 。我发现默认情况下我只能索引键或值(BuiltinContainerExtractors.MAP_KEY/MAP_VALUES),所以我正在尝试实现我自己的活页夹/桥接器。那是我的代码:

public class SomeEntity {

    @Transient
    @PropertyBinding(binder = @PropertyBinderRef(type = ConceptDistancePropertyBinder.class))
    public Map<String, Integer> getRelated() {
        return ...;
    }
}

public class ConceptDistancePropertyBinder implements PropertyBinder {
    @Override
    public void bind(PropertyBindingContext context) {
        context.dependencies().useRootOnly();
        IndexSchemaElement schemaElement = context.indexSchemaElement();
        IndexSchemaObjectField conceptDistanceField = schemaElement.objectField("conceptDistance");
        conceptDistanceField.fieldTemplate(
                "conceptDistanceTemplate",
                fieldTypeFactory -> fieldTypeFactory.asString().analyzer("default")
        );
        final ConceptDistancePropertyBridge bridge = new ConceptDistancePropertyBridge(conceptDistanceField.toReference());
        context.bridge(Map.class, bridge);
    }
}


public class ConceptDistancePropertyBridge implements PropertyBridge<Map> {

    private final IndexObjectFieldReference conceptDistanceFieldReference;

    public ConceptDistancePropertyBridge(IndexObjectFieldReference conceptDistanceFieldReference) {
        this.conceptDistanceFieldReference = conceptDistanceFieldReference;
    }

    @Override
    public void write(DocumentElement target, Map bridgedElement, PropertyBridgeWriteContext context) {
        Map<String, Integer> relatedDistanceWithOtherConcepts = (Map<String, Integer>) bridgedElement;
        DocumentElement indexedUserMetadata = target.addObject(conceptDistanceFieldReference);
        relatedDistanceWithOtherConcepts
                .forEach((field, value) -> indexedUserMetadata.addValue(field, field));
    }
}

我有一个例外:

    Hibernate ORM mapping: 
    type 'com.odysseusinc.prometheus.concept.entity.ConceptEntity': 
        failures: 
          - HSEARCH800007: Unable to resolve path '.related' to a persisted attribute in Hibernate ORM metadata. If this path points to a transient attribute, use @IndexingDependency(derivedFrom = ...) to specify which persisted attributes it is derived from. See the reference documentation for more information.

context.dependencies().useRootOnly()没有帮助。我也尝试使用context.dependencies().use("somefield")但又遇到了另一个异常

    Hibernate ORM mapping: 
    type 'com.odysseusinc.prometheus.concept.entity.ConceptEntity': 
        path '.related': 
            failures: 
              - HSEARCH700078: No readable property named 'filedName' on type 'java.lang.Integer'

我提取了所有必要的代码并将其放在 GitHub https://github.com/YaroslavTir/map-index 上,非常简单,启动后出现异常Application.main

4

1 回答 1

3

正如错误消息告诉您的那样:

如果此路径指向瞬态属性,请使用 @IndexingDependency(derivedFrom = ...) 指定它派生自哪些持久属性。有关详细信息,请参阅参考文档。

特别是,请参阅本节

简而言之,添加注释getRelated()以便 Hibernate Search 知道您从哪些属性派生了地图:

public class SomeEntity {

    @Transient
    @PropertyBinding(binder = @PropertyBinderRef(type = ConceptDistancePropertyBinder.class))
    @IndexingDependency(
        derivedFrom = {
            @ObjectPath(@PropertyValue(propertyName = "someProperty1")),
            @ObjectPath({@PropertyValue(propertyName = "someProperty2"), @PropertyValue(propertyName = "someNestedProperty")})
        },
        extraction = @ContainerExtraction(extract = ContainerExtract.NO)
    )
    public Map<String, Integer> getRelated() {
        return ...;
    }
}

请注意,在您的情况下,您必须明确指定@IndexingDependency整个地图是“派生的”,而不仅仅是值(这是 Hibernate Search 默认的目标,正如您所注意到的)。这就是您需要的原因extraction = @ContainerExtraction(extract = ContainerExtract.NO):这基本上告诉 Hibernate Search“此元数据适用于整个地图,而不仅仅是值”。

于 2021-07-26T15:43:46.447 回答