Hibernate Search / Lucene:字符串字段不能用于排序"使用每个文档的多个值索引,而是使用SORTED_SET"

时间:2017-04-05 01:33:09

标签: java lucene spring-data-jpa hibernate-search

我有以下型号。

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;

    .
    .

}

我正在尝试按" name"对文档进行排序。如下:

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:类型不匹配:name已编入索引   每个文档有多个值,请使用SORTED_SET   org.apache.lucene.uninverting.FieldCacheImpl $ SortedDocValuesCache.createValue(FieldCacheImpl.java:678)     在   org.apache.lucene.uninverting.FieldCacheImpl $ Cache.get(FieldCacheImpl.java:189)     在   org.apache.lucene.uninverting.FieldCacheImpl.getTermsIndex(FieldCacheImpl.java:646)     在   org.apache.lucene.uninverting.FieldCacheImpl.getTermsIndex(FieldCacheImpl.java:626)     在   org.apache.lucene.uninverting.UninvertingReader.getSortedDocValues(UninvertingReader.java:256)     在org.apache.lucene.index.DocValues.getSorted(DocValues.java:262)     在   org.apache.lucene.search.FieldComparator $ TermOrdValComparator.getSortedDocValues(FieldComparator.java:762)     在   org.apache.lucene.search.FieldComparator $ TermOrdValComparator.getLeafComparator(FieldComparator.java:767)     在   org.apache.lucene.search.FieldValueHitQueue.getComparators(FieldValueHitQueue.java:183)     在   org.apache.lucene.search.TopFieldCollector $ SimpleFieldCollector.getLeafCollector(TopFieldCollector.java:164)     在   org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:812)     在   org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:535)     在   org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:523)     在   org.hibernate.search.query.engine.impl.LazyQueryState.search(LazyQueryState.java:103)

任何提示?

1 个答案:

答案 0 :(得分:1)

编辑:实际上,在其他任何事情之前,您应该在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. @SortableField属性
  2. 上添加name注释
  3. 请勿使用queryBuilder.sort().byField("name",SortField.Type.STRING),只需使用queryBuilder.sort().byField("name")
  4. 如果它不起作用,也许您应该尝试擦除索引并重新索引。