使用 NEST 7 进行 ElasticSearch 排序

ElasticSearch Sorting using NEST 7

我正在使用 ElarsticSearch 7.7 和 NEST 7.7,并且正在尝试使用排序功能。但是我收到一个错误,

我收到一条错误消息, "Type: illegal_argument_exception Reason: "文本字段未针对需要每个文档字段数据的操作(如聚合和排序)进行优化,因此默认情况下禁用这些操作。请改用关键字字段。或者,在 [name] 上设置 fielddata=true 以通过反转倒排索引来加载字段数据。请注意,这会占用大量内存。”原因是:"Type: illegal_argument_exception Reason: "文本字段未针对需要聚合和排序等每个文档字段数据的操作进行优化,因此默认情况下禁用这些操作。请改用关键字字段. 或者,在 [name] 上设置 fielddata=true 以通过反转倒置的

来加载字段数据

下面是我的代码

public SearchResult Search(string searchQuery, int storeId, int pageNumber = 1, int pageSize = 10, IList<SearchFilter> requestFilter = null, decimal? priceFrom = null, decimal? priceTo = null, string sortBy = null)
    {
        var queryContainer = new QueryContainer(); 
        var multiMatch = new QueryStringQuery
        {
            Fields = Infer.Field<ElasticIndexGroupProduct>(p => p.Name)
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.CategoryName))
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.VendorName))                        
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x => x.Name)))
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x => x.CategoryName)))                        
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x => x.ManufacturerName)))
                     .And(Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x => x.ShortDescription))),
            Boost = 1.1,
            Name = "named_query",
            Query = searchQuery,
            DefaultOperator = Operator.Or,
            Analyzer = "standard",
            QuoteAnalyzer = "keyword",
            AllowLeadingWildcard = true,
            MaximumDeterminizedStates = 2,
            Escape = true,
            FuzzyPrefixLength = 2,
            FuzzyMaxExpansions = 3,
            FuzzyRewrite = MultiTermQueryRewrite.ConstantScore,
            Rewrite = MultiTermQueryRewrite.ConstantScore,
            Fuzziness = Fuzziness.Auto,
            TieBreaker = 1,
            AnalyzeWildcard = true,
            MinimumShouldMatch = 2,
            QuoteFieldSuffix = "'",
            Lenient = true,
            AutoGenerateSynonymsPhraseQuery = false
        };
        queryContainer &= multiMatch;        

        //sorting 
        var sorts = new List<ISort>();
        switch (sortBy)
        {
            case "z-a":
                sorts.Add(new FieldSort { Field = Infer.Field<ElasticIndexGroupProduct>(p => p.Name), Order = SortOrder.Descending });
                sorts.Add(new FieldSort { Field = Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x=>x.Name)), Order = SortOrder.Descending });
                break;
            default:
                sorts.Add(new FieldSort { Field = Infer.Field<ElasticIndexGroupProduct>(p => p.Name), Order = SortOrder.Ascending });
                sorts.Add(new FieldSort { Field = Infer.Field<ElasticIndexGroupProduct>(p => p.AssociatedProducts.Select(x => x.Name)), Order = SortOrder.Ascending });
                break;
        }

       var searchRequest = new SearchRequest<ElasticIndexGroupProduct>()
        {
            Profile = true,
            Query = queryContainer,
            From = (pageNumber - 1) * pageSize,
            Size = pageSize,
            Version = true,
            Sort = sorts
        };

        var searchResponse = _client.Search<ElasticIndexGroupProduct>(searchRequest);
        return GenerateSearchResult(searchQuery, searchResponse);
    }

这是错误

找到解决办法。需要在 [Keywords] 上设置 fielddata=true 以便通过反转倒排索引将字段数据加载到内存中。

[ElasticsearchType(RelationName = "searchproduct")]
    public class ElasticIndexGroupProduct
    {
        [Text(Fielddata = true)]
        public string Name { get; set; }
    }

如果您希望能够对 Name 执行全文搜索并对其进行排序和聚合,那么您可能希望将其索引为 multi_field 和 [=12] =] 和 keyword 数据类型。