。NET ElasticSearch NEST-多个部分匹配的NGram分析器

问题描述 投票:1回答:1

我正在尝试使用NGram在多个字段上使用ElasticSearch进行部分匹配,但是在建立索引后我将匹配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个回答
0
投票

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

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.