Elasticsearch完全匹配和提升相关性(NEST)

时间:2017-11-23 14:45:38

标签: elasticsearch elasticsearch-5 elasticsearch-net

我确信我想做的事情是可能的,但对于我的生活,我无法弄明白。

我正在使用ngrams忙于自动填充功能。

我遇到的问题是确切的匹配并不总是先出现,但对FullName来说确实如此。

但是当我搜索7061之类的内容并且有7061123这样的值后,后者会有一个beter得分,但我会搜索7061所以我总是想要完全匹配顶部,如果他们是任何。

我知道有很多关于类似事情的SO问题,但我无法弄清楚。我想我错过了一些愚蠢的事情。

我的设置如下:

请注意,我为employeeId

设置了不同的分析器
 var response = this.client.CreateIndex(
                this.config.IndexName,
                index => index.Mappings(
                        ms => ms.Map<EmployeeDocument>(
                            m => m.Properties(
                                p => p
                                    .Text(t => t.Name(n => n.FullName).Analyzer("auto-complete").Fields(ff => ff.Keyword(k => k.Name("keyword"))))
                                    .Text(t => t.Name(n => n.EmployeeId).Analyzer("auto-complete-id").Fields(ff => ff.Keyword(k => k.Name("keyword"))))
                                    .Text(t => t.Name(n => n.Email).Analyzer("auto-complete").Fields(ff => ff.Keyword(k => k.Name("keyword"))))
                                    .Text(t => t.Name(n => n.CompanyName).Analyzer("auto-complete").Fields(ff => ff.Keyword(k => k.Name("keyword"))))
                                    .Text(t => t.Name(n => n.JobNumber).Analyzer("auto-complete").Fields(ff => ff.Keyword(k => k.Name("keyword"))))
                                    .Text(t => t.Name(n => n.Description).Analyzer("auto-complete").Fields(ff => ff.Keyword(k => k.Name("keyword")))))))
                    .Settings(f => f.Analysis(
                        analysis => analysis
                            .Analyzers(
                                analyzers => analyzers
                                    .Custom("auto-complete", a => a.Tokenizer("standard").Filters("lowercase", "auto-complete-filter"))
                                    .Custom("auto-complete-id", a => a.Tokenizer("standard").Filters("lowercase", "auto-complete-id-filter")))
                            .TokenFilters(tokenFilter => tokenFilter
                                .EdgeNGram("auto-complete-filter", t => t.MinGram(3).MaxGram(30))
                                .EdgeNGram("auto-complete-id-filter", t => t.MinGram(1).MaxGram(5))))));

然后实际搜索看起来像

var response = await this.client.SearchAsync<EmployeeDocument>(
                           x => x.Index(this.config.IndexName).Type("employee").From(page - 1).Size(pageSize)
                                .Query(q => q
                                    .MultiMatch(m => m
                                        .Query(query)
                                        .Type(TextQueryType.MostFields)
                                        .Fields(f => f.Field(_ => _.FullName, 3) // The boosting here i think works**
                                                        .Field(_ => _.EmployeeId, 3) // Not sure if this is relevent seeing that it is suppose to work of an different analyzer.**
                                                        .Field(_ => _.Email)
                                                        .Field(_ => _.CompanyName)
                                                        .Field(_ => _.JobNumber)
                                                        .Field(_ => _.Description))))
                                         .Highlight(
                                            h => h.PreTags("<mark>").PostTags("</mark>").Fields(
                                                f => f.Field(p => p.FullName),
                                                f => f.Field(p => p.CompanyName),
                                                f => f.Field(p => p.JobNumber),
                                                f => f.Field(p => p.EmployeeId),
                                                f => f.Field(p => p.Email),
                                                f => f.Field(p => p.Description))));

0 个答案:

没有答案