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