Elasticsearch 6.3 - 如何对分析字段进行排序?

Elasticsearch 6.3 - How to Sort on an Analyzed field?

因此,我找到了旧版本 Elastic 的示例,但由于语法更改,我无法将这些解决方案转换为 6.3 语法。

我有一个字段 ShowName (String),我已经对其应用了 N-Gram 分析器。问题是我还需要按该字段排序。在我添加分析器之前,我得到了关键字后缀 sub 属性 并且能够轻松排序,但是,自从添加分析器后,我现在无法访问该关键字 sub 属性.

我尝试用 [Text(Analyzer = "nGram_analyzer")])Keyword 属性装饰模型中的 属性,但是在创建索引时我收到一条错误消息:

Multiple custom attributes of the same type found.

然后我尝试在创建索引(下面的代码)时在映射中显式添加这两个字段,虽然这会创建这两个字段,但我仍然无法按关键字后缀字段进行排序。谁能指出我哪里出错了?

var createIndexResponse = client.CreateIndex(shows, c => c
            .Settings(s => s
                .Analysis(a => a
                    .TokenFilters(t => t.NGram("nGram_filter", ng => ng.MinGram(3).MaxGram(10)))
                    .Analyzers(aa => aa
                        .Custom("nGram_analyzer", cc => cc
                            .Tokenizer("whitespace")
                            .Filters(nGramFilters1)
                        )
                    )
                )
            )
            .Mappings(ms => ms
                .Map<ShowElasticSearchModel>(m => m
                    .AutoMap<ShowElasticSearchModel>()
                    .Properties(p => p
                        .Text(t => t
                            .Name(n => n.ShowName)
                            .Analyzer("nGram_analyzer")
                            .Fields(fs => fs
                                .Text(tt => tt.Name(nn => nn.ShowName.Suffix("keyword")))
                            )
                        )
                    )
                )
            )
        );

编辑:根据收到的答案,代码块的最终语法如下(现在代码中包含多个多字段)。

var createIndexResponse = client.CreateIndex(shows, c => c
            .Settings(s => s
                .Analysis(a => a
                    .TokenFilters(t => t.NGram("nGram_filter", ng => ng.MinGram(3).MaxGram(10)))
                    .Analyzers(aa => aa
                        .Custom("nGram_analyzer", cc => cc
                            .Tokenizer("whitespace")
                            .Filters(nGramFilters1)
                        )
                    )
                )
            )
            .Mappings(ms => ms
                .Map<ShowElasticSearchModel>(m => m
                    .AutoMap<ShowElasticSearchModel>()
                    .Properties(p => p
                        .Text(t => t
                            .Name(n => n.ShowName)
                            .Analyzer("nGram_analyzer")
                            .Fields(ff => ff
                                .Keyword(k => k
                                    .Name(n => n.ShowName.Suffix("keyword"))
                                )
                            )
                        )
                        .Text(t => t
                            .Name(n => n.Organisation)
                            .Analyzer("nGram_analyzer")
                            .Fields(ff => ff
                                .Keyword(k => k
                                    .Name(n => n.Organisation.Suffix("keyword"))
                                )
                            )
                        )
                        .Text(t => t
                            .Name(n => n.Venues)
                            .Analyzer("nGram_analyzer")
                            .Fields(ff => ff
                                .Keyword(k => k
                                    .Name(n => n.Venues.Suffix("keyword"))
                                )
                            )
                        )
                    )
                )
            )
        );

String 字段类型已 split to keyword and text types since Elasticsearch 5.0. Text fields can't be used for sorting, so you need to create multi-field:类型 text 用于全文搜索,类型 keyword 用于聚合和排序。

但在您的示例中,您创建了两个 text 字段,并使用 keyword 作为字段名称后缀而不是字段类型(据我所知 C# 语法)。