Could not add sortable annotation on some analysed field?


#1
@Column(nullable = false, length = 50)
    @Fields({
            @Field(name = "firstName", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer")),
            @Field(name = "dummyFieldToAddQueryAnalyzer", index = Index.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "nameAnalyzer_query"), bridge = @FieldBridge(impl = DummyStringBridge.class))
    })
    private String firstName;

Now I have a firstName field for searching, indexing and analysed, but now I want to make this field sortable, say sort by first name, I am not able to add @SortableField, because sort fields must not be analyzed, right?

How can I do that? any other tricks?


#2

Just add another field:

@Column(nullable = false, length = 50)
@Fields({
    @Field(name = "firstName", analyzer = @Analyzer(definition = "nameAnalyzer")),
    @Field(name = "firstName_sort"),
    @Field(name = "dummyFieldToAddQueryAnalyzer", analyzer = @Analyzer(definition = "nameAnalyzer_query"), bridge = @FieldBridge(impl = DummyStringBridge.class))
})
@SortableField(forField = "fieldName_sort")
private String firstName;

Then when you need a sort, you will have to sort on fieldName_sort.

Optionally, and provided you are using a recent version of Hibernate Search, you can use a normalizer on the sort field. Analyzers are forbidden on sort fields, but normalizers are allowed, since they cannot produce multiple tokens.