No more _source if script_fields is used in elasticsearch query

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I am running a simple query like so:

{   "query": {     "term": {       "statuses": "active"     }   },   "script_fields": {     "test": {       "script": "_source.name"     }   } } 

The problem is that once I introduce the script_fields, I no longer get _source in my results.

I have tried:

{   "fields": [     "_all"   ],   "query": {     "term": {       "statuses": "active"     }   },   "script_fields": {     "email": {       "script": "_source.name"     }   } } 

and

{   "fields": [     "*"   ],   "query": {     "term": {       "statuses": "active"     }   },   "script_fields": {     "email": {       "script": "_source.name"     }   } } 

But they did not make any difference. Is there a way to get _source returned in addition to the script_fields?

回答1:

In the fields array, make it load _source:

{   "stored_fields": [     "_source"   ],   "query": {     "term": {       "statuses": "active"     }   },   "script_fields": {     "email": {       "script": "_source.name"     }   } } 


回答2:

This works for me:

curl -X DELETE localhost:9200/a  curl -X POST localhost:9200/a/b/c -d '{"title" : "foo"}'  curl -X POST localhost:9200/a/_refresh  echo;  curl localhost:9200/a/_search?pretty -d '{   "fields": [     "_source"   ],   "query": {     "match_all": {}   },   "script_fields": {     "title_script": {       "script": "_source.title"     }   } }' 

Output:

"hits" : {   # ...   "hits" : [ {     # ...     "_source" : {"title" : "foo"},     "fields" : {       "title_script" : "foo"     }   } ] } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!