我试图使用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完全陌生,现在只花了一个星期左右的时间。
发布于 2020-02-11 10:52:51
client.Indices.Create调用无效,有两个原因:
MinGram和MaxGram之间的差不能大于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."您可以阅读有关此错误这里的更多信息。
nGram_filter这样的过滤器,您需要将此过滤器更改为ngram我通过检查elasticsearch (localhost:9200/YOUR_ index _NAME/_NAME)中的索引映射发现了这些问题,在这里我发现没有应用映射。第二步是看看DebugInformation从索引创建响应中要告诉我什么。
var createIndexResponse = await client.Indices.CreateAsync("my_index1", ..);
createIndexResponse.DebugInformation希望这能有所帮助。
https://stackoverflow.com/questions/60160108
复制相似问题