如何在Solr中使用自然排序方式对文本/字符串进行排序?

5

我想对一列值进行排序,希望按以下方式进行:

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

换句话说,有时候被称为“自然排序”,其中文本按字母顺序/词典顺序排序,数字按数值顺序排序,即使两者混合在同一个字符串中也是如此。

我无法在Solr(目前为4.0)中找到任何方法来实现这一点。是否有标准的方法来实现此功能或至少可行的“配方”?

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接