按照枚举字段用 Hibernate Search 排序

huangapple go评论82阅读模式
英文:

Sort by enum field with hibernate search

问题

我在实体中有一个枚举字段

@Field(store = Store.NO, index = Index.YES, analyze = Analyze.NO)
@Enumerated(EnumType.STRING)
@FieldBridge(impl = EnumBridge.class)
@SortableField
private Status status;

我想通过这个字段对实体进行排序。因此我在 Hibernate Search 中创建了一个 Sort

Sort sort = qb
    .sort()
    .byDistance()
    .onField("location.location")
    .fromLatitude(sfq.getTheCenterLatitude())
    .andLongitude(sfq.getTheCenterLongitude())
    .andByField("status")
    .createSort();

(我还按距离排序,但这不重要)

但是,当我尝试搜索时,我看到了这个错误:

ERROR [io.undertow.request] (default task-1) UT005023: 处理对 /api/search/ 的请求时出现异常org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalStateException: 对字段 'status' 的 docvalues 类型为 NONE期望为 SORTED)。使用 UninvertingReader 或带有 docvalues 的索引
Caused by: java.lang.IllegalStateException: 对字段 'status' 的 docvalues 类型为 NONE期望为 SORTED)。使用 UninvertingReader 或带有 docvalues 的索引

如何在 Hibernate Search 中按枚举字段进行排序?

英文:

I have a ENUM field in my entity

@Field(store=Store.NO,index=Index.YES,analyze=Analyze.NO)
@Enumerated(EnumType.STRING)
@FieldBridge(impl = EnumBridge.class)
@SortableField
private Status status;

and i want to sort entities by this field. So i've create a Sort in Hibernate search:

Sort sort = qb
		.sort()
		.byDistance()
		.onField("location.location")
		.fromLatitude(sfq.getTheCenterLatitude())
		.andLongitude(sfq.getTheCenterLongitude())
		.andByField("status")
		.createSort();

(i also sort by distance, but it doesn't matter)

BUT, when i try to search i see this error

ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /api/search/: org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalStateException: unexpected docvalues type NONE for field 'status' (expected=SORTED). Use UninvertingReader or index with docvalues
Caused by: java.lang.IllegalStateException: unexpected docvalues type NONE for field 'status' (expected=SORTED). Use UninvertingReader or index with docvalues.

How to sort by enum field in hibernate search?

答案1

得分: 0

你可能在添加了 @SortableField 后忘记重新索引了?

英文:

You probably forgot to reindex after you added @SortableField?

答案2

得分: 0

通过在property.xml中设置<property name="hibernate.search.default.locking_strategy" value="none"/>解决。我不知道为什么,但对我有效。

英文:

Solved by setting &lt;property name=&quot;hibernate.search.default.locking_strategy&quot; value=&quot;none&quot;/&gt; in property.xml. I don't know how, but it works for me.

huangapple
  • 本文由 发表于 2020年7月24日 18:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63071625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定