2016-09-09 76 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为了这个工作。

相关问题