实现您正在尝试做的最简单的方法是创建
具有以下映射的索引:
client.CreateIndex(descriptor => descriptor
.Index(indexName)
.AddMapping<Document>(m => m
.Properties(p => p
.String(s => s.Name(n => n.Id).Index(FieldIndexOption.NotAnalyzed)))));
class Document
{
public string Id { get; set; }
}
索引一些带有小写 id 值的文档:
client.Index(new Document {Id = "Accounting/AP_ABC.asp".ToLower()});
client.Index(new Document {Id = "Accounting/AR_ABC.asp".ToLower()});
client.Index(new Document {Id = "Accounting/Account.asp".ToLower()});
那么对于这个排序
var searchResponse = client.Search<Document>(s => s
.Sort(sort => sort
.OnField(f => f.Id).Ascending()));
我们会得到
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "indexname",
"_type": "document",
"_id": "accounting/account.asp",
"_score": null,
"_source": {
"id": "accounting/account.asp"
},
"sort": [
"accounting/account.asp"
]
},
{
"_index": "indexname",
"_type": "document",
"_id": "accounting/ap_abc.asp",
"_score": null,
"_source": {
"id": "accounting/ap_abc.asp"
},
"sort": [
"accounting/ap_abc.asp"
]
},
{
"_index": "indexname",
"_type": "document",
"_id": "accounting/ar_abc.asp",
"_score": null,
"_source": {
"id": "accounting/ar_abc.asp"
},
"sort": [
"accounting/ar_abc.asp"
]
}
]
}
}
但是,如果您真的关心您提供的 ID(例如
Accounting/AP_ABC.asp) 你可以使用前面提到的
Case-Insensitive Sorting.
使用 NEST 应用此解决方案:
如下创建映射
client.CreateIndex(descriptor => descriptor
.Index(indexName)
.Analysis(analysisDescriptor => analysisDescriptor
.Analyzers(a => a
.Add("case_insensitive_sort", new CustomAnalyzer
{
Tokenizer = "keyword",
Filter = new List<string> {"lowercase"}
})))
.AddMapping<Document>(m => m
.Properties(p => p
.String(s => s
.Name(n => n.Id)
.Analyzer("case_insensitive_sort")))));
索引文件:
client.Index(new Document {Id = "Accounting/AP_ABC.asp"});
client.Index(new Document {Id = "Accounting/AR_ABC.asp"});
client.Index(new Document {Id = "Accounting/Account.asp"});
对于排序,我们将排序,我们将得到以下结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "indexname",
"_type": "document",
"_id": "Accounting/Account.asp",
"_score": null,
"_source": {
"id": "Accounting/Account.asp"
},
"sort": [
"accounting/account.asp"
]
},
{
"_index": "indexname",
"_type": "document",
"_id": "Accounting/AP_ABC.asp",
"_score": null,
"_source": {
"id": "Accounting/AP_ABC.asp"
},
"sort": [
"accounting/ap_abc.asp"
]
},
{
"_index": "indexname",
"_type": "document",
"_id": "Accounting/AR_ABC.asp",
"_score": null,
"_source": {
"id": "Accounting/AR_ABC.asp"
},
"sort": [
"accounting/ar_abc.asp"
]
}
]
}
}