用于弹性搜索的嵌套5.1如何制作基本的aggs?

时间:2016-12-29 19:14:23

标签: c# elasticsearch nest

在NEST Elastic Search 5.1中,我希望能够制作一个与此http直接请求等效的基本aggs:

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#nest尝试:

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

但它给了我一个空洞的结果:

enter image description here

我给你上下文的样本:

我用:

创建索引和映射
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
    }

任何人都可以解释我这样做以及它如何运作?

1 个答案:

答案 0 :(得分:1)

在序列化C#POCO属性名称并将它们发送到Elasticsearch时,默认情况下是NEST camelcases字段名称。因此,.Field(f => f.Age)将序列化为"age",但Elasticsearch中的字段为"Age",因此不会返回任何结果。

要更改字段名称序列化行为,您可以将委托传递给DefaultFieldNameInferrer()上的ConnectionSettings

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的字段名称中逐字反映,因此委托只返回属性名称以用作字段名称的字符串。

相关问题