微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Hibernate Search [5.11.5.Final]动态数值属性:无法自动确定字段'...'的字段类型

如何解决Hibernate Search [5.11.5.Final]动态数值属性:无法自动确定字段'...'的字段类型

我在正确索引动态数字字段时遇到了一些麻烦,看来它们总是被索引为字符串。

据我了解,在为动态数字字段建立索引时,我必须使用动态模板:

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),因为它需要对每个属性进行显式验证,而我可能不知道其名称和类型。

我在索引编制过程中做错了什么吗?

预先感谢

解决方法

我认为您没有做错任何事情。 Hibernate Search 5中的实验性Elasticsearch集成实际上并不支持动态字段。您无法预先指定字段的类型,显然,它是动态字段的默认字符串类型。

升级到Hibernate Search 6(当前处于候选发布阶段)将是一个解决方案,因为it supports dynamic fields through field templates 但是,Hibernate Search 6 API有所不同,因此迁移可能需要大量工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。