In order to fix sorting I created my own NorwegianCollationFactory. It is not perfect solution as I copied code from old version of Hibernate Search (IndexableBinaryStringTools.class), but it is working fine.
NorwegianCollationFactory class:
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.util.TokenFilterFactory;
import java.text.Collator;
import java.util.Locale;
import java.util.Map;
public final class NorwegianCollationFactory extends TokenFilterFactory {
public NorwegianCollationFactory(Map<String, String> args) {
super(args);
}
@Override
public TokenStream create(TokenStream input) {
Collator norwegianCollator = Collator.getInstance(new Locale("no", "NO"));
return new CollationKeyFilter(input, norwegianCollator);
}
}
CollationKeyFilter class:
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import java.io.IOException;
import java.text.Collator;
import java.util.Objects;
public final class CollationKeyFilter extends TokenFilter {
// This code is copied from IndexableBinaryStringTools.class from the old version of hibernate search 4.3.0.Final
private static final CollationKeyFilter.CodingCase[] CODING_CASES = {
new CollationKeyFilter.CodingCase(7, 1),
new CollationKeyFilter.CodingCase(14, 6, 2),
new CollationKeyFilter.CodingCase(13, 5, 3),
new CollationKeyFilter.CodingCase(12, 4, 4),
new CollationKeyFilter.CodingCase(11, 3, 5),
new CollationKeyFilter.CodingCase(10, 2, 6),
new CollationKeyFilter.CodingCase(9, 1, 7),
new CollationKeyFilter.CodingCase(8, 0)
};
private final Collator collator;
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
public CollationKeyFilter(TokenStream input, Collator collator) {
super(input);
this.collator = (Collator) collator.clone();
}
@Override
public boolean incrementToken() throws IOException {
if (input.incrementToken()) {
byte[] collationKey = collator.getCollationKey(termAtt.toString()).toByteArray();
int encodedLength = getBinaryStringEncodedLength(collationKey.length);
termAtt.resizeBuffer(encodedLength);
termAtt.setLength(encodedLength);
encodeToBinaryString(collationKey, collationKey.length, termAtt.buffer());
return true;
} else {
return false;
}
}
// This code is copied from IndexableBinaryStringTools class from the old version of hibernate search 4.3.0.Final
private void encodeToBinaryString(byte[] inputArray, int inputLength, char[] outputArray) {
if (inputLength > 0) {
int inputByteNum = 0;
int caseNum = 0;
int outputCharNum = 0;
CollationKeyFilter.CodingCase codingCase;
for (; inputByteNum + CODING_CASES[caseNum].numBytes <= inputLength; ++outputCharNum) {
codingCase = CODING_CASES[caseNum];
if (codingCase.numBytes == 2) {
outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift)
+ (((inputArray[inputByteNum + 1] & 0xFF) >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF);
} else {
outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift)
+ ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift)
+ (((inputArray[inputByteNum + 2] & 0xFF) >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF);
}
inputByteNum += codingCase.advanceBytes;
if (++caseNum == CODING_CASES.length) {
caseNum = 0;
}
}
codingCase = CODING_CASES[caseNum];
if (inputByteNum + 1 < inputLength) {
outputArray[outputCharNum++] = (char) ((((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift)
+ ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift)) & (short) 0x7FFF);
outputArray[outputCharNum] = (char) 1;
} else if (inputByteNum < inputLength) {
outputArray[outputCharNum++] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) & (short) 0x7FFF);
outputArray[outputCharNum] = caseNum == 0 ? (char) 1 : (char) 0;
} else {
outputArray[outputCharNum] = (char) 1;
}
}
}
// This code is copied from IndexableBinaryStringTools class from the old version of hibernate search 4.3.0.Final
private int getBinaryStringEncodedLength(int inputLength) {
return (int) ((8L * inputLength + 14L) / 15L) + 1;
}
// This code is copied from IndexableBinaryStringTools class from the old version of hibernate search 4.3.0.Final
private static class CodingCase {
int numBytes;
int initialShift;
int middleShift;
int finalShift;
int advanceBytes = 2;
short middleMask;
short finalMask;
CodingCase(int initialShift, int middleShift, int finalShift) {
this.numBytes = 3;
this.initialShift = initialShift;
this.middleShift = middleShift;
this.finalShift = finalShift;
this.finalMask = (short) ((short) 0xFF >>> finalShift);
this.middleMask = (short) ((short) 0xFF << middleShift);
}
CodingCase(int initialShift, int finalShift) {
this.numBytes = 2;
this.initialShift = initialShift;
this.finalShift = finalShift;
this.finalMask = (short) ((short) 0xFF >>> finalShift);
if (finalShift != 0) {
advanceBytes = 1;
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
CollationKeyFilter that = (CollationKeyFilter) o;
return Objects.equals(collator, that.collator) &&
Objects.equals(termAtt, that.termAtt);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), collator, termAtt);
}
}
Entity mapping example:
@Entity
@NormalizerDef(name = "textSortNormalizer",
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = PatternReplaceFilterFactory.class, params = {
@Parameter(name = "pattern", value = "('-&\\.,\\(\\))"),
@Parameter(name = "replacement", value = " "),
@Parameter(name = "replace", value = "all")
}),
@TokenFilterDef(factory = PatternReplaceFilterFactory.class, params = {
@Parameter(name = "pattern", value = "([^0-9\\p{L} ])"),
@Parameter(name = "replacement", value = ""),
@Parameter(name = "replace", value = "all")
}),
@TokenFilterDef(factory = NorwegianCollationFactory.class)
}
)
public class Entity {
@Field(name = "name_for_sort", normalizer = @Normalizer(definition = "textSortNormalizer"))
@SortableField(forField = "name_for_sort")
private String name;
}