java - solr sort by pinyin alphabetical order
黄舟
黄舟 2017-05-17 10:02:57
0
2
869

There is a title field, and I want to sort it by the first letter of Pinyin. There is a method in the solr method

params.setSort("title",ORDER.asc);//排序字段,正序还是倒序

But it seems that the result of this setting is not what I want. The result I want is as follows:
The first letter of the serial number title
1 aa
2 countryg
3 person r
4中z

In the SQL statement, I can sort by the first letter of Pinyin. I want to know if I can sort by the first letter of Pinyin in Solr? What should I do? Can I ask for guidance? ? ?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
黄舟

Can’t you just create a new field with the “first letter of title” as the value as a field? Generally speaking, in practical applications, custom fields are used as sorting conditions

淡淡烟草味

There is a relatively simple method, as follows:

List<String> strs = new ArrayList<>();
strs.add("中");
strs.add("啊");
strs.add("坎");
strs.add("哈");
strs.add("少");
strs.add("发");
strs.add("顶");
strs.add("擦");
strs.add("鹅");
strs.add("巴");

// 排序
strs.sort(Collator.getInstance(Locale.CHINA));
// 打印
strs.forEach(System.out::println);

Result

啊 // A
巴 // B
擦 // C
顶 // D
鹅 // E
发 // F
哈 // H
坎 // K
少 // S
中 // Z

You can also use pinyin4j for more complex sorting

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template