【发布时间】:2021-07-26 15:11:34
【问题描述】:
我想通过使用映射键作为字段名来索引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
这真的很简单,并且在启动Application.main之后出现异常@
【问题讨论】:
-
"没有名为 'filedName' 的可读属性":只是检查一下:
filedName看起来像是一个错字 - 应该是fieldName? -
@andrewjames 感谢您的重播。这是一个错字,但它不会改变情况,我也得到了同样的例外,没有错字。
标签: spring lucene hibernate-search hibernate-search-6