Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#Nest和Elasticsearch聚合_C#_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Nest - Fatal编程技术网 elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

c#Nest和Elasticsearch聚合

c#Nest和Elasticsearch聚合,c#,elasticsearch,nest,C#,elasticsearch,Nest,有人知道如何使用nest进行多个聚合吗? 我发现了很多例子,不幸的是它们都不起作用 以下是我所拥有的: Vehicles fields = new Vehicles(); //create a terms query var query = new TermsQuery { IsVerbatim = true, Field = "VehicleOwnerId", Terms = new string[] { 25 }, }; var aggregations = ne

有人知道如何使用nest进行多个聚合吗? 我发现了很多例子,不幸的是它们都不起作用

以下是我所拥有的:

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);
}
Vehicles字段=新车();
//创建术语查询
变量查询=新术语查询
{
IsVerbatim=true,
Field=“VehicleOwnerId”,
Terms=新字符串[]{25},
};
var聚合=新字典
{
{“年”,新的聚合容器
{
术语=新术语分类(名称(字段.年份))
{
字段=新字段(名称(字段.年份))
}
}
}
//,
//{“makes”,新的AggregationContainer
//    {
//术语=新术语分类(“制造”)
//        {
//字段=新字段(名称(fields.Make))
//        }
//    }
//}
};
//创建搜索请求
var searchRequest=新的searchRequest
{
Query=Query,
From=0,
尺寸=100,
聚合=聚合
};
var result=client.SearchAsync(searchRequest).result;
var年=结果、合计、期限(“年”);
字典年数=新字典();
foreach(以年为单位的var项目。桶)
{
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;
//创建搜索请求
var searchRequest=新的searchRequest
{
Query=Query,
From=0,
尺寸=100,
//聚合=聚合
Aggregations=新建AggregationDictionary
{
{ 
“childAgg”,新儿童集合(“childAgg”,类型(车辆))
{
Aggregations=新建AggregationDictionary
{
{“年”,新术语分类(名称(fields.VehicleYear))},
{“makes”,新术语聚合(nameof(fields.VehicleMakeName))},
{“车型”,新术语分类(名称(fields.VehicleModelName))},
}
}
}
}
};
var result=client.SearchAsync(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);
}
Vehicle字段=新车();
//创建术语查询
变量查询=新术语查询
{
IsVerbatim=true,
Field=“VehicleOwnerId”,
Terms=新字符串[]{“30”},
};
字符串[]字段=新建[]
{
名称(字段。年份),
nameof(fields.MakeId),
nameof(fields.ModelId)
};
var aggregations=newdictionary();
foreach(字段中的字符串字段)
{
var termsAggregation=新的termsAggregation(sField)
{
字段=sField
};
Add(sField,newaggregationcontainer{Terms=termsAggregation});
}
//创建搜索请求
var searchRequest=新的searchRequest
{
Query=Query,
From=0,
尺寸=10,
聚合=聚合
};
var result=client.SearchAsync(searchRequest).result;
var years=结果.聚合.术语(名称(字段.年份));
字典年数=新字典();
foreach(以年为单位的var项目。桶)
{
yearCounts.Add(item.Key,item.DocCount±0);
}
elasticsearch的确切错误是,我在使用postman时看到:


默认情况下,文本字段上禁用了字段数据。在[MakeName]上设置fielddata=true,以便通过取消反转索引将fielddata加载到内存中。请注意,这可能会占用大量内存。或者使用关键字字段。

我想作为一名程序员,我永远不会太担心自己会感到骄傲:) 当问题暴露出来时,问题的解决方案常常让我觉得自己很愚蠢

所以我的问题是,我试图在聚合中使用的字段是文本,无法使用。我将所有内容切换到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);
}
Vehicle字段=新车();
//创建术语查询
变量查询=新术语查询
{
IsVerbatim=true,
Field=“VehicleOwnerId”,
Terms=新字符串[]{“30”},
};
字符串[]字段=新建[]
{
名称(字段。年份),
nameof(fields.MakeId),
nameof(fields.ModelId)
};
var aggregations=newdictionary();
foreach(字段中的字符串字段)
{
var termsAggregation=新的termsAggregation(sField)
{
字段=sField
};
Add(sField,newaggregationcontainer{Terms=termsAggregation});
}
//创建搜索请求
var searchRequest=新的searchRequest
{
Query=Query,
From=0,
尺寸=10,
聚合=聚合
};
var result=client.SearchAsync(searchRequest).result;
var years=结果.聚合.术语(名称(字段.年份));
字典年数=新字典();
foreach(以年为单位的var项目。桶)
{
yearCounts.Add(item.Key,item.DocCount±0);
}
elasticsearch的确切错误是,我在使用postman时看到:


默认情况下,文本字段上禁用了字段数据。在[MakeName]上设置fielddata=true,以便通过取消反转索引将fielddata加载到内存中。请注意,这可能会占用大量内存。或者使用关键字字段。

哪个嵌套文档已过时?你看到了吗