我想通过使用映射键作为字段名称来索引 map<String, Integer>
。我发现默认情况下我只能索引键或值 (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
真的很简单,在 start Application.main
答案 0 :(得分:2)
正如错误消息告诉您的那样:
<块引用>如果此路径指向一个瞬态属性,请使用@IndexingDependency(derivedFrom = ...) 指定它源自哪些持久化属性。有关详细信息,请参阅参考文档。
特别是,请参阅this section。
简而言之,将注释添加到 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“此元数据适用于整个地图,而不仅仅是值”。