【问题标题】:.NET ElasticSearch NEST - NGram Analyzer on Multiple Fields for Partial Matches.NET ElasticSearch NEST - 用于部分匹配的多个字段的 NGram 分析器
【发布时间】:2020-02-11 18:52:51
【问题描述】:

我正在尝试使用 ElasticSearch 在使用 NGram 的多个字段上进行部分匹配,但在构建索引后我匹配 0 个结果。这对我来说不是很自然,而且我什至无法让 NGram 为一个领域工作。这对我来说是一个充满激情的项目,我真的希望新的搜索能够用于部分单词匹配。我尝试使用模糊性,但它开始将错误匹配得分过高。

索引创建:

var nGramFilters = new List<string> { "lowercase", "asciifolding", "nGram_filter" };

Client.Indices.Create(CurrentIndexName, c => c
    .Settings(st => st
            .Analysis(an => an // https://stackoverflow.com/questions/38065966/token-chars-mapping-to-ngram-filter-elasticsearch-nest
                .Analyzers(anz => anz
                    .Custom("ngram_analyzer", cc => cc
                        .Tokenizer("ngram_tokenizer")
                            .Filters(nGramFilters))
                        )
                        .Tokenizers(tz => tz
                                .NGram("ngram_tokenizer", td => td
                                    .MinGram(2)
                                        .MaxGram(20)
                                        .TokenChars(
                                            TokenChar.Letter,
                                            TokenChar.Digit,
                                            TokenChar.Punctuation,
                                            TokenChar.Symbol
                                        )
                                    )
                                )
                            )
                        )
                        .Map<Package>(map => map
                            .AutoMap()
                            .Properties(p => p
                            .Text(t => t
                                .Name(n => n.Title)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.Summary)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PestControlledBy)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticideControlsThesePests)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticideInstructions)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticideActiveIngredients)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticidesContainingThisActiveIngredient)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticideSafeOn)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                            .Text(t => t
                                .Name(n => n.PesticideNotSafeOn)
                                .Fields(f => f
                                    .Keyword(k => k
                                        .Name("keyword")
                                            .IgnoreAbove(256)
                                    )
                                    .Text(tt => tt
                                        .Name("ngram")
                                        .Analyzer("ngram_analyzer")
                                    )
                                )
                            )
                        )
                    )
                );

查询:

var result = _client.Search<Package>(s => s
.From((form.Page - 1) * form.PageSize)
.Size(form.PageSize)
.Query(query => query
    .MultiMatch(m => m
        .Fields(f => f
            .Field(p => p.Title.Suffix("ngram"), 1.5)
            .Field(p => p.Summary.Suffix("ngram"), 1.1)
            .Field(p => p.PestControlledBy.Suffix("ngram"), 1.0)
            .Field(p => p.PesticideControlsThesePests.Suffix("ngram"), 1.0)
            .Field(p => p.PesticideInstructions.Suffix("ngram"), 1.0)
            .Field(p => p.PesticideActiveIngredients.Suffix("ngram"), 1.0)
            .Field(p => p.PesticidesContainingThisActiveIngredient.Suffix("ngram"), 1.0)
            .Field(p => p.PesticideSafeOn.Suffix("ngram"), 1.0)
            .Field(p => p.PesticideNotSafeOn.Suffix("ngram"), 1.0)
        )
        .Operator(Operator.Or) // https://stackoverflow.com/questions/46139028/elasticsearch-how-to-do-a-partial-match-from-your-query
        .Query(form.Query)
    )
)
.Highlight(h => h
    .PreTags("<strong>")
    .PostTags("</strong>")
    .Encoder(HighlighterEncoder.Html) //https://github.com/elastic/elasticsearch-net/issues/3091
    .Fields(fs => fs
        .Field(f => f.Summary.Suffix("ngram")),
        fs => fs
        .Field(p => p.PestControlledBy.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticideControlsThesePests.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticideInstructions.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticideActiveIngredients.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticidesContainingThisActiveIngredient.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticideSafeOn.Suffix("ngram")),
        fs => fs
        .Field(p => p.PesticideNotSafeOn.Suffix("ngram"))
        .NumberOfFragments(10)
        .FragmentSize(250)
        )
    )
);

我在正确的球场上吗?我尝试使用默认分析器,但我不匹配“猫蒲公英”和“猫耳蒲公英”之类的东西。使用默认分析器......整个单词必须匹配,但我希望部分匹配能够获得“花瓣”和“花瓣”之类的东西。朝着正确方向迈出的任何一步都值得赞赏。我对 ElasticSearch 和 NEST 完全陌生,现在只使用了一周左右。

【问题讨论】:

    标签: c# .net elasticsearch nest


    【解决方案1】:

    client.Indices.Create调用无效,原因有二:

    1. MinGramMaxGram 之间的差值不能大于 1,因此会出现此错误
    Elasticsearch.Net.ElasticsearchClientException: Request failed to execute. Call: Status code 400 from: PUT /my_index1?pretty=true&error_trace=true. ServerError: Type: illegal_argument_exception Reason: "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [1] but was [18]. This limit can be set by changing the [index.max_ngram_diff] index level setting."
    

    您可以阅读有关此错误的更多信息here

    1. 没有像nGram_filter这样的过滤器,您需要将其更改为ngram

    我通过检查 elasticsearch (localhost:9200/YOUR_INDEX_NAME/_mapping) 中的索引映射发现了这些问题,我发现未应用映射。第二步是查看DebugInformation 必须从索引创建响应中告诉我什么

    var createIndexResponse = await client.Indices.CreateAsync("my_index1", ..);
    createIndexResponse.DebugInformation
    

    希望对您有所帮助。

    【讨论】:

    • 这使我的索引可搜索并处于绿色状态,所以谢谢你。这不是我想要做的正确解决方案吗?我得到了一些不可预测的结果。我正在做 ngram 3-4,并将其隔离为仅使用 ngram 查询标题。对于查询“cat”,我得到“Bracted Planttain (Plantago aristata)”得分 0.3375051 和“Catnip (Nepeta cataria)”得分 0.33674708。
    • 你能用解释模式运行你的查询吗?这应该更多地说明它发生的原因。
    • 你能告诉我这件事吗? imgur.com/a/gHa8BJP蓝色标题是正在查询的内容。这是一个示例,其中“canada toadflax”在查询“cat”中得分高于“Cat's ear dandelion”。
    • 通过检查_analyze 端点的两个查询,我可以看到nGramFilters 过滤器在这里遇到了麻烦,尤其是ngram 一个
    • 对我可以改变什么有什么建议吗?我应该提出一个新问题吗?
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2015-09-05
    • 2021-12-13
    • 2020-11-16
    相关资源
    最近更新 更多