5

我想按照以下方式对值列表进行排序:

  • 4
  • 5xa
  • 8kdjfew454
  • 9
  • 10
  • 999cc
  • b
  • c9
  • c10cc
  • c11

换句话说,有时被称为“自然排序”,其中文本在有文本的地方按字母/字典顺序排序,但在有数字的地方按数字排序,即使两者混合在同一个字符串中。

我无论如何都找不到在 Solr (4.0 atm) 中执行此操作。有没有标准的方法来做到这一点,或者至少有一个可行的“食谱”?

4

1 回答 1

1

本文描述了您可以实现的最接近的事情

来自文章:

要强制数字按数字排序,我们需要用零填充任何数字: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

于 2016-09-26T11:21:52.603 回答