PROBLEM: ElasticSearch - POST JSON to Remove hits .total, .max_score, ._index, ._type, _id, ._score
I have to be honest, this one really upset and frustrated me because I couldn't find an easy solution/answer using Google or ElasticSearch Documentation on how to only return back _source from a search POST json body. When I finally figured it out, I think it wasn't implemented correctly.
SOLUTION using QUERYSTRING:
First, off if you finally find in it in the ElasticSearch documentation, you can use the query string to remove extra metadata fields like _id, max_score, _index, _type, etc... Correction, you can tell it which fields to include through a querystring:
GET /_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score
reference: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/common-options.html
You can see from the example that you need to set the filter_path and pass in the fields you want returned back.
HOWEVER!!!!, if you are using the ElasticSearch-JS client for Node/browser, it doesn't really tell you how to do it in the API examples or in a POST json body example.
If this is documented, I sure as heck couldn't find it.
THE POST BODY SOLUTION
I'm going to tell you how to do it and there is a bug.
This is the normal client json request
this.client.search({
index: index,
type: type,
from: (page - 1) * pageSize,
size: pageSize,
body: body
})
But what you can actually do it pass it filter_path in the search method options
this.client.search({
index: index,
type: type,
from: (page - 1) * pageSize,
size: pageSize,
body: body,
filter_path: [
'hits.total,hits.max_score,hits.hits._id,hits.hits._score,hits.hits._source'
]
})
But if you look at it, you're like, WTF? it's an array, however he's passing in a string delimited by comma(,)
Passing in the different values individually returns back weird results. For example, if i pass in
[
'hits.total',
'hits.max_score'
]
On the results back, total will be undefined or max_score will be set.
By doing it all in a single string delimited by comma(,) it worked as expected. I should submit a bug but it's late, and i'm tired.
Hopefully, this helps somebody!!!