2016-12-29 101 views
1

巢弹性搜索5.1,我想能够做一个基本的AGGS相当于它的工作原理是HTTP的直接请求:嵌套用于弹性搜索5.1如何使基本的aggs?

POST /base_well/person/_search 
{ 
    "aggs": { 
     "all_words" : { 
      "terms" : { 
       "field" : "Age"     
      } 
     } 
    } 
} 

它给我这样的answere:

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

但它给我一个空的结果:

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

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

为了改变字段名称序列化行为,你可以在ConnectionSettings

var uri = new Uri("http://localhost:9200"); 
var setting = new ConnectionSettings(uri) 
    .DefaultFieldNameInferrer(s => s) 
    .DefaultIndex("base_well"); 

var client = new ElasticClient(setting); 

传递一个委托DefaultFieldNameInferrer()既然你想让你的POCO属性名称发送到Elasticsearch字段名逐字反映,委托返回属性名称以用作字段名称的字符串。

+0

谢谢您的提示!但它的工作方式与你的解决方法一样,甚至在任何地方都可以用年龄代替(在人员类别和代码中) –

+0

对不起,你是对的,非常感谢你,没有很多人能够回答关于这个话题 –