ElasticSearch - NEST 搜索部分字符串多匹配

时间:2021-08-01 20:36:12

标签: .net elasticsearch nest

我有一个应用程序(.net core + ReactJS),我必须弄清楚并制作一个 mod,但后来发现它使用了很多我不熟悉的技术,在这种情况下是 ElasticSearch . 目前,搜索术语 Micro 不会返回任何内容,因为显然它正在寻找完全匹配,但我想要实现的是,对 Micro 的搜索应该返回 Microsoft、MicroStrategy...以及任何包含名称中的“micro”,我如何在以下代码中实现它?我试着做一些研究,我看到像 ngram、分析器、query_string 或 QueryString 之类的东西......不知道从哪里开始。请帮忙。

public SearchDescriptor<ProductRecord> BuildSearchDescriptor(
            string productType,
            string name,
            int page ,
            int pageSize ,
            Dictionary<string, List<string>> filters)
        {
            ValidateProductType(productType);
            var collection = ProductTypeToCollection[productType];

            var searchDescriptor = new SearchDescriptor<ProductRecord>()
                .Index(collection)
                .From(page * pageSize)
                .Size(pageSize)
                .Query(q => q
                    .Bool(bq => bq
                        .Must(m => m
                            .MultiMatch(c => c
                                    .Query(name)
                                    .Fields(ff => ff
                                        .Field(f => f.Product, boost: ProductBoost)
                                        .Field(f => f.Manufacturer, boost: ManufacturerBoost)
                                        .Field(f => f.Version)
                                        .Field(f => f.Id, boost: 50)
                                    )
                                    .Type(TextQueryType.CrossFields)
                                //.Type(TextQueryType.BestFields)
                            ))
                        .Filter(fq => AddFilters(fq, filters))
                    )
                )
                .Sort(ss => ss
                        .Descending(SortSpecialField.Score)
                        .Ascending(p => p.Product.Suffix("keyword"))
                        .Ascending(p => p.Manufacturer.Suffix("keyword"))
                        .Descending(p => p.Version.Suffix("keyword"))
                    /*.Script(sc => sc
                        .Type("number")
                        .Descending()
                        .Script(script => script
                            .Source("doc['VERSION'].value")))
                            */
                );
            return searchDescriptor;
        }
}

0 个答案:

没有答案