gpt4 book ai didi

sorting - 使用NEST 7进行ElasticSearch排序

转载 作者:行者123 更新时间:2023-12-03 02:21:56 27 4
gpt4 key购买 nike

我正在使用ElarsticSearch 7.7和NEST 7.7,并且正在尝试使用sort函数。但是我遇到了一个错误

我收到一个错误消息,
“类型:invalid_argument_exception原因:“文本字段并未针对需要每个文档字段数据的操作(如聚合和排序)进行优化,因此默认情况下将禁用这些操作。请改用关键字字段。或者,在[name]上设置fielddata = true,以通过反转取反的索引来加载字段数据。请注意,这可能会占用大量内存。”引起原因:“类型:非法_自变量_异常原因:”文本字段并未针对需要按文档的字段数据进行聚合和排序的操作进行优化,因此默认情况下,这些操作处于禁用状态。请使用关键字或者,在[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);
}

这是错误
enter image description here

最佳答案

找到了解决方案。需要通过在[关键字]上设置fielddata = true来通过反转反相索引将字段数据加载到内存中。

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

关于sorting - 使用NEST 7进行ElasticSearch排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com