gpt4 book ai didi

java - hibernate 搜索/Lucene : String field cannot be used for sorting "indexed with multiple values per document, use SORTED_SET instead"

转载 作者:行者123 更新时间:2023-12-02 13:29:57 32 4
gpt4 key购买 nike

我有以下型号。

public class FeatureMeta {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(unique=true)
private String uri;

@Column
@Field
private String name;

@Field
@Column
private String businessDesc;

@Field
@Column
private String logicalDesc;

.
.

}

我正在尝试按“名称”对文档进行排序,如下所示:

org.hibernate.search.jpa.FullTextQuery jpaQuery =
fullTextEntityManager.createFullTextQuery(aggrBuilder.build(), FeatureMeta.class);
.
.

SortFieldContext sortCtx = queryBuilder.sort().byField("name",SortField.Type.STRING);
jpaQuery.setSort(sortCtx.createSort());
.

但是 Lucene 抛出以下异常?

java.lang.IllegalStateException: Type mismatch: name was indexed with multiple values per document, use SORTED_SET instead at org.apache.lucene.uninverting.FieldCacheImpl$SortedDocValuesCache.createValue(FieldCacheImpl.java:678) at org.apache.lucene.uninverting.FieldCacheImpl$Cache.get(FieldCacheImpl.java:189) at org.apache.lucene.uninverting.FieldCacheImpl.getTermsIndex(FieldCacheImpl.java:646) at org.apache.lucene.uninverting.FieldCacheImpl.getTermsIndex(FieldCacheImpl.java:626) at org.apache.lucene.uninverting.UninvertingReader.getSortedDocValues(UninvertingReader.java:256) at org.apache.lucene.index.DocValues.getSorted(DocValues.java:262) at org.apache.lucene.search.FieldComparator$TermOrdValComparator.getSortedDocValues(FieldComparator.java:762) at org.apache.lucene.search.FieldComparator$TermOrdValComparator.getLeafComparator(FieldComparator.java:767) at org.apache.lucene.search.FieldValueHitQueue.getComparators(FieldValueHitQueue.java:183) at org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector.getLeafCollector(TopFieldCollector.java:164) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:812) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:535) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:523) at org.hibernate.search.query.engine.impl.LazyQueryState.search(LazyQueryState.java:103)

有什么建议吗?

最佳答案

编辑:实际上,在执行其他操作之前,您应该检查您在 name 字段上使用的分析器。分析器可能有一个分词器,这将导致多值字段无法排序。尝试添加不同的字段进行排序,并在此字段上使用带有 KeywordTokenizer 的分析器:

@AnalyzerDef(name = "sort_analyzer",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class)
}
)
public class FeatureMeta {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(unique=true)
private String uri;

@Column
@Field
@Field(name = "name_sort", analyzer = @Analyzer(definition = "sort_analyzer"))
private String name;

@Field
@Column
private String businessDesc;

@Field
@Column
private String logicalDesc;

.
.

}

然后根据这个新字段而不是默认字段进行排序:

SortFieldContext sortCtx = queryBuilder.sort().byField("name_sort",SortField.Type.STRING);
<小时/>

原始答案(我提出的观点仍然有效):

不确定是什么原因导致您的情况出现异常,但请尝试在代码中修复这些问题:

  1. name 属性上添加 @SortableField 注解
  2. 不要使用queryBuilder.sort().byField("name",SortField.Type.STRING),只需使用queryBuilder.sort().byField("name")

如果不起作用,也许您应该尝试删除索引并重新索引。

关于java - hibernate 搜索/Lucene : String field cannot be used for sorting "indexed with multiple values per document, use SORTED_SET instead",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220349/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com