0
votes

I'm using ElarsticSearch 7.7 & NEST 7.7 and I'm trying to use the sort function. But I'm getting an error,

I'm getting an error saying, "Type: illegal_argument_exception Reason: "Text fields are not optimized for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index. Note that this can use significant memory." caused by: "Type: illegal_argument_exception Reason: "Text fields are not optimized for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted

this is my code below

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);
    }

this is the error enter image description here

2

2 Answers

0
votes

Found a solution. Need to set fielddata=true on [Keywords] in order to load fielddata in memory by uninverting the inverted index.

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

If you want to be able to perform full text search on Name as well as sort and aggregate on it, then you probably want to index it as a multi_field with both text and keyword data type.