2012-08-08 11 views
18

Tôi đang chạy một truy vấn đơn giản như vậy:Không còn _source nếu script_fields được sử dụng trong truy vấn elasticsearch

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

Vấn đề là một khi tôi giới thiệu script_fields, tôi không còn nhận được _source trong kết quả của tôi.

Tôi đã thử:

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

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

Nhưng họ đã không thực hiện bất kỳ sự khác biệt. Có cách nào để nhận được _source được trả lại ngoài số script_fields không?

Trả lời

20

Trong mảng fields, làm cho nó load _source:

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

Có ai biết chính xác _why_ điều này xảy ra? Có liên quan đến https://github.com/elastic/elasticsearch/issues/20068 không? –

0

này làm việc cho tôi:

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" 
    } 
    } ] 
}