Nest/ElasticSearch 按 _uid 排序

Nest/ElasticSearch Sorting by _uid

我正在尝试根据查询提取记录并使用 _uid 字段对它们进行排序。在我的例子中,_uid 是 Type 后跟 # 后跟我设置的 id。我的索引中充满了代码文件,_uid 的示例是 myType#MyDocuments/File.txt

所以我正在对 _uid 升序进行排序。它主要工作,它按顺序对类型进行排序,但在类型中它只对最上面的目录进行正确排序。

所以我会看到类似

的内容
Accounting/AP_ABC.asp
Accounting/AR_ABC.asp
Accounting/Account.asp

这是不对的,因为帐户应该在 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。 稍后我会用一个工作示例更新我的答案。

更新2

  1. 实现您正在尝试做的事情的最简单方法是创建 具有以下映射的索引:

    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"
                ]
             }
          ]
       }
    }
    
  2. 但是如果你真的关心你提供的 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"
                ]
             }
          ]
       }
    }
    

希望对您有所帮助。