繁体   English   中英

c# Nest 和 Elasticsearch 聚合

[英]c# Nest and Elasticsearch Aggregations

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

这是我所拥有的:

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

我使用邮差看到的来自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.

这是我使用 SearchDescriptors 的示例。 我唯一的问题是如何将返回的结果序列化为正确的键值列表。 循环遍历字段列表是返回结果的最佳方式。

SearchDescriptor<Advert> agghDescriptor = new SearchDescriptor<Advert>();

agghDescriptor.Aggregations(ag => ag.Terms("make", a => a.Field(f => f.Make)) &&
                                  ag.Terms("region", a => a.Field(f => f.Region)) &&
                                  ag.Terms("city", a => a.Field(f => f.City)) &&
                                  ag.Terms("category", a => a.Field(f => f.Category)) &&
                                  ag.Terms("application", a => a.Field(f => f.Application)) &&
                                  ag.Terms("portalId", a => a.Field(f => f.PortalId)) &&
                                  ag.Terms("isActiveAuctionAdvert", a => a.Field(f => f.IsActiveAuctionAdvert)) &&
                                  ag.Terms("isBargainAccount", a => a.Field(f => f.IsBargainAccount)) &&
                                  ag.Terms("condition", a => a.Field(f => f.Condition))
);
agghDescriptor.Size(0);

var json2 = _client.RequestResponseSerializer.SerializeToString(agghDescriptor);
var aggregationResult = _client.Search<Advert>(agghDescriptor);

List<string> fields = new List<string>();
fields.Add("make");
fields.Add("category");
fields.Add("region");

List<Aggregation> aggregations = new List<Aggregation>();

foreach (var field in fields)
{
    var aggrs = aggregationResult.Aggregations.Terms(field);
    List<AggregateItem> aggregateItems = new List<AggregateItem>();
    
    foreach (var item in aggrs.Buckets)
    {
        aggregateItems.Add(new AggregateItem()
        {
            Count = item.DocCount ?? 0,
            Key = item.Key
        });
    }

    aggregations.Add(new Aggregation()
    {
        Name = field,
        Aggregates = aggregateItems
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM