【发布时间】:2021-02-22 17:05:11
【问题描述】:
我在正确索引动态数字字段时遇到了一些问题,似乎它们总是被索引为字符串。
据我了解,在索引动态数字字段时,我必须使用动态模板:
PUT /com.product.product
{
"mappings": {
"com.product.Product": {
"dynamic_templates": [
{
"numeric_sort": {
"match_mapping_type": "*",
"match_pattern": "regex",
"match": "^sort_num_.*",
"mapping": {
"type": "double"
}
}
}
]
}
}
}
我在事件监听器中上传:
@Configuration
@Transactional
public abstract class DynamicTemplateConfig {
@EventListener
public void addDynamicTemplates(ContextRefreshedEvent event) {
if (this.searchIndexingIsActive) {
this.addDynamicTemplates();
}
}
...
}
我正在索引字段桥中的属性:
public class PropertyValueFieldBridge implements FieldBridge {
...
private void indexBigDecimalProperties(Document document, LuceneOptions luceneOptions, PropertyBigDecimal property) {
String fieldName = PREFIX_SORT + NUMERIC + DELIMITER + property.getProperty().getCode();
Double indexedValue = property.getValue().doubleValue();
luceneOptions.addNumericFieldToDocument(
fieldName,
indexedValue,
document);
}
}
索引这些 BigDecimal 属性后,我总是以索引的字符串属性结束:
"_source": {
"id": "1",
"sort_id": 1,
"filter_id": 1,
"sort_num_quantity": "115.0"
}
当我尝试对该属性进行排序时,出现以下异常:
org.hibernate.search.exception.SearchException: Cannot automatically determine the field type for field 'sort_num_quantity'. Use byField(String, Sort.Type) to provide the sort type explicitly.
at org.hibernate.search.query.dsl.sort.impl.SortFieldStates.getCurrentSortFieldTypeFromMetamodel(SortFieldStates.java:177) ~[hibernate-search-engine-5.11.5.Final.jar:5.11.5.Final]
at org.hibernate.search.query.dsl.sort.impl.SortFieldStates.determineCurrentSortFieldTypeAutomaticaly(SortFieldStates.java:150) ~[hibernate-search-engine-5.11.5.Final.jar:5.11.5.Final]
at org.hibernate.search.query.dsl.sort.impl.ConnectedSortContext.byField(ConnectedSortContext.java:42) ~[hibernate-search-engine-5.11.5.Final.jar:5.11.5.Final]
我试图避免使用byField(String, Sort.Type),因为它需要对每个属性进行明确验证,而我可能不知道其名称和类型。
我在索引过程中做错了吗?
提前致谢
【问题讨论】:
标签: java spring-boot hibernate elasticsearch hibernate-search