MarkLogic排序规则定制求助:实现A优先于An的排序
Problem Overview
You've nailed most of your sorting requirements with the collation http://marklogic.com/collation/en/S1/AS/T00BB: it handles case insensitivity, ignores symbols/punctuation/non-Latin special chars, and skips leading whitespace. But you're stuck on the 5th rule: getting entries like "A Treatise" to sit ahead of "An Asian". Right now, your sort order puts "An Asian" first, which doesn't match what you need.
Root Cause
Your current collation compares sequences character-by-character after applying its ignore rules. Since n (from "An") comes before t (from "A Treatise", with spaces ignored), "An Asian" gets sorted earlier. To fix this, we need to tweak the collation to give single-article "A" entries priority over "An" entries.
Solution: Custom Tailored Collation
MarkLogic lets you extend existing collations with custom ICU-based tailoring rules to adjust sort priorities for specific character sequences. Here's a collation that checks all your boxes, including the 5th requirement:
http://marklogic.com/collation/en/S1/AS/T00BB;tailoring=&A ; [*] << An
What the Tailoring Rule Does
&Aanchors the rule to the starting character "A"; [*]refers to any character that follows "A"<< Antells the collation that any sequence starting with "A" + any character should sort before the sequence "An"
This ensures that entries starting with "A " (even when spaces are ignored) will always come before entries starting with "An ", no matter what characters follow.
Expected Sort Result
With this custom collation, your entries will sort exactly as you want:
- Æ and Words
- ;a next
- A Treatise
- An Asian
- Beautiful sky
- -Ology and -Osophy
Public Access - Sentencing Frame
- 'Soda' Vs 'Pop'
- The Results
- ÜBer
Backup Plan (If Custom Collation Isn't an Option)
If you can't use a tailored collation for some reason, you could preprocess your indexed field to adjust the sort key:
- Replace leading "An " with a placeholder character that sorts later (like
~An) - This way, "A Treatise" (with an unmodified key) will naturally sort before the modified "~An Asian" key
That said, the custom collation approach is cleaner and aligns with your preference of handling sorting via collation rules directly.
内容的提问来源于stack exchange,提问作者Bin

