【问题标题】:How to sort text/string in solr using a natural sort order?如何使用自然排序顺序对 solr 中的文本/字符串进行排序?
【发布时间】:2013-02-16 08:15:52
【问题描述】:
我想按照以下方式对值列表进行排序:
- 4
- 5xa
- 8kdjfew454
- 9
- 10
- 999cc
- b
- c9
- c10cc
- c11
换句话说,有时称为“自然排序”,其中文本在有文本的地方按字母/字典顺序排序,但在有数字的地方按数字排序,即使两者混合在同一个字符串中。
我无论如何都找不到在 Solr (4.0 atm) 中执行此操作。有没有标准的方法来做到这一点,或者至少有一个可行的“食谱”?
【问题讨论】:
标签:
sorting
solr
natural-sort
【解决方案1】:
this article中描述了您可以实现的最接近的事情
来自文章:
要强制数字按数字排序,我们需要左填充任何数字
带零:2 变为 0002,10 变为 0010,100 变为 0100,等等
等等。然后即使是词法排序也会像这样排列值:
标题1 标题2 标题10 标题100
字段类型
此字母数字排序字段类型将找到的任何数字转换为 6
数字,用零填充。 (如果您希望数字大于 6
您的字段值中的数字,您将需要增加
填充时为零。)
字段类型也去掉了英文和法文的前导冠词,
小写,并清除任何不是字母数字的字符。它是
以英语为中心,并假设变音符号已被折叠成
ASCII 字符。
<fieldType name="alphaNumericSort" class="solr.TextField" sortMissingLast="false" omitNorms="true">
<analyzer>
<!-- KeywordTokenizer does no actual tokenizing, so the entire
input string is preserved as a single token
-->
<tokenizer class="solr.KeywordTokenizerFactory"/>
<!-- The LowerCase TokenFilter does what you expect, which can be
when you want your sorting to be case insensitive
-->
<filter class="solr.LowerCaseFilterFactory" />
<!-- The TrimFilter removes any leading or trailing whitespace -->
<filter class="solr.TrimFilterFactory" />
<!-- Remove leading articles -->
<filter class="solr.PatternReplaceFilterFactory"
pattern="^(a |the |les |la |le |l'|de la |du |des )" replacement="" replace="all"
/>
<!-- Left-pad numbers with zeroes -->
<filter class="solr.PatternReplaceFilterFactory"
pattern="(\d+)" replacement="00000$1" replace="all"
/>
<!-- Left-trim zeroes to produce 6 digit numbers -->
<filter class="solr.PatternReplaceFilterFactory"
pattern="0*([0-9]{6,})" replacement="$1" replace="all"
/>
<!-- Remove all but alphanumeric characters -->
<filter class="solr.PatternReplaceFilterFactory"
pattern="([^a-z0-9])" replacement="" replace="all"
/>
</analyzer>
</fieldType>
样本输出
标题号 1 => titleno000001
标题号 2 => titleno000002
标题号 10 => titleno000010
标题编号 100 => titleno000100