gpt4 book ai didi

c# Nest 和 Elasticsearch 聚合

转载 作者:行者123 更新时间:2023-12-02 22:45:48 26 4
gpt4 key购买 nike

有谁知道如何使用嵌套进行多次聚合?
不幸的是,我发现了很多示例,但它们都不起作用。

这是我所拥有的:

Vehicles fields = new Vehicles();

//create a terms query
var query = new TermsQuery
{
IsVerbatim = true,
Field = "VehicleOwnerId",
Terms = new string[] { 25 },
};

var aggregations = new Dictionary<string, IAggregationContainer>
{
{ "years", new AggregationContainer
{
Terms = new TermsAggregation(nameof(fields.Year))
{
Field = new Field(nameof(fields.Year))
}
}
}
//,
//{ "makes", new AggregationContainer
// {
// Terms = new TermsAggregation("Make")
// {
// Field = new Field(nameof(fields.Make))
// }
// }
//}
};

//create the search request
var searchRequest = new SearchRequest
{
Query = query,
From = 0,
Size = 100,
Aggregations = aggregations
};

var result = client.SearchAsync<InventoryLiveView>(searchRequest).Result;

var years = result.Aggregations.Terms("years");
Dictionary<string, long> yearCounts = new Dictionary<string, long>();
foreach (var item in years.Buckets)
{
yearCounts.Add(item.Key, item.DocCount ?? 0);
}

如果我只是执行这样的代码,它就可以工作。年按预期返回聚合。如果我尝试添加另一个字段(如上面注释的那个),它会失败并且我得到零记录。
如何在一个查询中获得多个聚合?我到处都看到了它的例子,但我尝试过的例子似乎都没有工作,而且大多数似乎已经过时了(包括 Nest 文档中的一些)。
我也尝试过这种与文档非常接近的方法。
//create the search request
var searchRequest = new SearchRequest
{
Query = query,
From = 0,
Size = 100,
//Aggregations = aggregations
Aggregations = new AggregationDictionary
{
{
"childAgg", new ChildrenAggregation("childAgg", typeof(Vehicles ))
{
Aggregations = new AggregationDictionary
{
{"years", new TermsAggregation(nameof(fields.VehicleYear))},
{"makes", new TermsAggregation(nameof(fields.VehicleMakeName))},
{"models", new TermsAggregation(nameof(fields.VehicleModelName))},
}
}
}
}
};

var result = client.SearchAsync<Vehicles>(searchRequest).Result;

这只会产生一个空引用异常。

最佳答案

我想我永远不会太担心成为一名程序员而感到自豪:)
很多时候,当问题的解决方案暴露出来时,我会觉得自己很愚蠢。

所以我的问题是我试图在聚合中使用的字段是文本并且无法使用。我将所有内容都切换到 ID 字段,多个聚合按预期工作。

所以这个版本的代码就像一个冠军:

Vehicle fields = new Vehicle ();

//create a terms query
var query = new TermsQuery
{
IsVerbatim = true,
Field = "VehicleOwnerId",
Terms = new string[] { "30" },
};

string[] Fields = new[]
{
nameof(fields.Year),
nameof(fields.MakeId),
nameof(fields.ModelId)
};

var aggregations = new Dictionary<string, IAggregationContainer>();
foreach (string sField in Fields)
{
var termsAggregation = new TermsAggregation(sField)
{
Field = sField
};

aggregations.Add(sField, new AggregationContainer { Terms = termsAggregation });
}

//create the search request
var searchRequest = new SearchRequest
{
Query = query,
From = 0,
Size = 10,
Aggregations = aggregations
};

var result = client.SearchAsync<InventoryLiveView>(searchRequest).Result;

var years = result.Aggregations.Terms(nameof(fields.Year));
Dictionary<string, long> yearCounts = new Dictionary<string, long>();
foreach (var item in years.Buckets)
{
yearCounts.Add(item.Key, item.DocCount ?? 0);
}

我使用 postman 看到的来自 elasticsearch 的确切错误是:
Fielddata is disabled on text fields by default. Set fielddata=true on [MakeName] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

关于c# Nest 和 Elasticsearch 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897871/

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