我正在尝试根据查询拉回记录,并使用_uid
字段对它们进行排序。在我的情况下,_uid
是Type,后跟#,再加上我设置的id。我的索引中充满了代码文件,并且_uid
的示例为myType#MyDocuments/File.txt
所以我正在对_uid
升序进行排序。它通常可以正常工作,将类型按顺序排序,但在类型内,仅在最上层目录中正确排序。
所以我会看到类似
Accounting/AP_ABC.asp
Accounting/AR_ABC.asp
Accounting/Account.asp
这是不对的,因为Account应该先于AP和AR。
有没有办法确保可以正确排序?
编辑
从我的索引添加映射
"dotnet":{"properties":{"fileContents":{"type":"string"},"filePath":{"type":"string"},"lastUpdate":{"type":"date","format":"dateOptionalTime"},"type":{"type":"string"}}}
最佳答案
创建一个新的not_analyzed字段,例如sortid,它将保存未分析的id值(Accounting / Account.asp)。 This文章将详细解释您为什么要这样做。
更新:
尝试应用case-insensitive sorting。
稍后,我将通过一个有效的示例来更新我的答案。
UPDATE2
具有以下映射的索引:
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"
]
}
]
}
}
您可以使用前面提到的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"
]
}
]
}
}
希望它会有所帮助。
关于c# - Nest/ElasticSearch按_uid排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29988300/