2016-09-09 74 views
0

有沒有辦法處理(例如連接)來自查詢的返回字段?在elasticsearch中處理返回的字段

這是我建立了我的指標:

PUT /megacorp/employee/1 
{ 
    "first_name" : "John", 
    "last_name" : "Smith", 
    "age" :  25, 
    "about" :  "I love to go rock climbing", 
    "interests": [ "sports", "music" ] 
} 

這就是我如何查詢它:

GET /megacorp/employee/_search 
{ 
    "query": {"match_all": {}} 
} 

的迴應是這樣的:

{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "megacorp", 
     "_type": "employee", 
     "_id": "1", 
     "_score": 1, 
     "_source": { 
      "first_name": "John", 
      "last_name": "Smith", 
      "age": 25, 
      "about": "I love to go rock climbing", 
      "interests": [ 
      "sports", 
      "music" 
      ] 
     } 
     } 
    ] 
    } 
} 

這是所有工作正常。

我想要的是連接_source中的兩個字段,並將其作爲新字段顯示在輸出中。

first_name和last_name應合併爲一個新字段「full_name」。我無法弄清楚如何在索引中創建一個新字段。我已經看過「copy_to」,但它需要您在映射中顯式設置store屬性,並且您必須顯式詢問查詢中存儲的字段。但主要的缺點是,當你這樣做時,first_name和last_name將以逗號分隔。我想要一個漂亮的字符串:「John Smith」

回答

0
GET /megacorp/employee/_search 
{ 
    "query": {"match_all": {}}, 
    "script_fields": { 
    "combined": { 
     "script": "_source['first_name'] + ' ' + _source['last_name']" 
    } 
    } 
} 

而且您需要啓用dynamic scripting

0

您可以使用script_fields實現這一

GET /megacorp/employee/_search 
{ 
    "query": {"match_all": {}}, 
    "script_fields" : { 
     "full_name" : { 
      "script" : "[doc['first_name'].value, doc['last_name'].value].join(' ')" 
     } 
    } 
} 

你需要確保enable dynamic scripting爲了這個工作。

相關問題