gpt4 book ai didi

c# - .NET ElasticSearch NEST-用于部分匹配的多个字段上的NGram分析器

转载 作者:行者123 更新时间:2023-12-02 22:47:39 24 4
gpt4 key购买 nike

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

索引创建:

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的新手,现在已经使用了一周左右。

最佳答案

client.Indices.Create调用无效,其原因有两个:

  • 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
  • 没有像nGram_filter这样的过滤器,您需要将其更改为ngram


  • 我通过检查Elasticsearch(localhost:9200 / YOUR_INDEX_NAME / _mapping)中的索引映射发现了这些问题,在该索引中我发现未应用映射。第二步是从索引创建响应中了解 DebugInformation必须告诉我什么
    var createIndexResponse = await client.Indices.CreateAsync("my_index1", ..);
    createIndexResponse.DebugInformation

    希望能有所帮助。

    关于c# - .NET ElasticSearch NEST-用于部分匹配的多个字段上的NGram分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60160108/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com