Nest for elastic search 5.1 如何制作基本的 aggs?
Nest for elastic search 5.1 how make a basic aggs?
在 NEST Elastic Search 5.1 中,我希望能够制作一个基本的 aggs 等效于这个有效的 http 直接请求:
POST /base_well/person/_search
{
"aggs": {
"all_words" : {
"terms" : {
"field" : "Age"
}
}
}
}
它给我这个答案:
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAnskcR_Z5VPUXUCs",
"_score": 1,
"_source": {
"first_name": "Polo",
"last_name": "Rodriguez",
"Age": 36
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAo0NcR_Z5VPUXUCu",
"_score": 1,
"_source": {
"first_name": "Mustapha",
"last_name": "Bulutu M'Bo",
"Age": 26
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAnPFcR_Z5VPUXUCr",
"_score": 1,
"_source": {
"first_name": "James",
"last_name": "Mopo",
"Age": 21
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAoO8cR_Z5VPUXUCt",
"_score": 1,
"_source": {
"first_name": "Marc Aurelien",
"last_name": "Poisson",
"Age": 26
}
}
]
},
"aggregations": {
"all_words": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 26,
"doc_count": 2
},
{
"key": 21,
"doc_count": 1
},
{
"key": 36,
"doc_count": 1
}
]
}
}
}
我做了这个 c# 嵌套尝试:
public class Person
{
public string first_name {get;set;}
public string last_name { get; set; }
public int Age { get; set; }
}
var uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri);
setting.DisableDirectStreaming(true);
setting.DefaultIndex("base_well");
var Client = new ElasticClient(setting);
var response = Client.Search<Person>(s => s
.Type("person")
.Aggregations(p => p
.Terms(ageCodeAggregation, m => m
.Field(f => f.Age))));
但它给了我一个空的结果:
我给你示例上下文:
我创建索引和映射:
PUT /base_well
{
"mappings": {
"person": {
"properties": {
"first_name":{
"type": "string",
"store": true
},
"last_name":{
"type": "string",
"store": true
},
"Age":{
"type": "long",
"store": true
}
}
}
}
}
我填充:
POST /base_well/person
{
"first_name":"James",
"last_name" : "Mopo",
"Age" : 21
}
POST /base_well/person
{
"first_name":"Polo",
"last_name" : "Rodriguez",
"Age" : 36
}
POST /base_well/person
{
"first_name":"Marc Aurelien",
"last_name" : "Poisson",
"Age" : 26
}
POST /base_well/person
{
"first_name":"Mustapha",
"last_name" : "Bulutu M'Bo",
"Age" : 26
}
任何人都可以向我解释如何做到这一点以及它是如何工作的?
NEST 在序列化 C# POCO 属性 名称并将它们发送到 Elasticsearch 时默认驼峰式字段名称。因此,.Field(f => f.Age)
将序列化为 "age"
,但 Elasticsearch 中的字段是 "Age"
,因此不会返回任何结果。
为了更改字段名称序列化行为,您可以在 ConnectionSettings
上将委托传递给 DefaultFieldNameInferrer()
var uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri)
.DefaultFieldNameInferrer(s => s)
.DefaultIndex("base_well");
var client = new ElasticClient(setting);
由于您希望您的 POCO 属性 名称在发送到 Elasticsearch 的字段名称中逐字反映,因此委托仅 returns 属性 名称用作字段名称的字符串.
在 NEST Elastic Search 5.1 中,我希望能够制作一个基本的 aggs 等效于这个有效的 http 直接请求:
POST /base_well/person/_search
{
"aggs": {
"all_words" : {
"terms" : {
"field" : "Age"
}
}
}
}
它给我这个答案:
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAnskcR_Z5VPUXUCs",
"_score": 1,
"_source": {
"first_name": "Polo",
"last_name": "Rodriguez",
"Age": 36
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAo0NcR_Z5VPUXUCu",
"_score": 1,
"_source": {
"first_name": "Mustapha",
"last_name": "Bulutu M'Bo",
"Age": 26
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAnPFcR_Z5VPUXUCr",
"_score": 1,
"_source": {
"first_name": "James",
"last_name": "Mopo",
"Age": 21
}
},
{
"_index": "base_well",
"_type": "person",
"_id": "AVlMAoO8cR_Z5VPUXUCt",
"_score": 1,
"_source": {
"first_name": "Marc Aurelien",
"last_name": "Poisson",
"Age": 26
}
}
]
},
"aggregations": {
"all_words": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 26,
"doc_count": 2
},
{
"key": 21,
"doc_count": 1
},
{
"key": 36,
"doc_count": 1
}
]
}
}
}
我做了这个 c# 嵌套尝试:
public class Person
{
public string first_name {get;set;}
public string last_name { get; set; }
public int Age { get; set; }
}
var uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri);
setting.DisableDirectStreaming(true);
setting.DefaultIndex("base_well");
var Client = new ElasticClient(setting);
var response = Client.Search<Person>(s => s
.Type("person")
.Aggregations(p => p
.Terms(ageCodeAggregation, m => m
.Field(f => f.Age))));
但它给了我一个空的结果:
我给你示例上下文:
我创建索引和映射:
PUT /base_well
{
"mappings": {
"person": {
"properties": {
"first_name":{
"type": "string",
"store": true
},
"last_name":{
"type": "string",
"store": true
},
"Age":{
"type": "long",
"store": true
}
}
}
}
}
我填充:
POST /base_well/person
{
"first_name":"James",
"last_name" : "Mopo",
"Age" : 21
}
POST /base_well/person
{
"first_name":"Polo",
"last_name" : "Rodriguez",
"Age" : 36
}
POST /base_well/person
{
"first_name":"Marc Aurelien",
"last_name" : "Poisson",
"Age" : 26
}
POST /base_well/person
{
"first_name":"Mustapha",
"last_name" : "Bulutu M'Bo",
"Age" : 26
}
任何人都可以向我解释如何做到这一点以及它是如何工作的?
NEST 在序列化 C# POCO 属性 名称并将它们发送到 Elasticsearch 时默认驼峰式字段名称。因此,.Field(f => f.Age)
将序列化为 "age"
,但 Elasticsearch 中的字段是 "Age"
,因此不会返回任何结果。
为了更改字段名称序列化行为,您可以在 ConnectionSettings
DefaultFieldNameInferrer()
var uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri)
.DefaultFieldNameInferrer(s => s)
.DefaultIndex("base_well");
var client = new ElasticClient(setting);
由于您希望您的 POCO 属性 名称在发送到 Elasticsearch 的字段名称中逐字反映,因此委托仅 returns 属性 名称用作字段名称的字符串.