Given the following Elasticsearch query that uses source filtering, what fields will be included in the response's _source?
{
"_source": ["user", "post_date"],
"query": {
"match": {"message": "search"}
}
}Think about what the _source array specifies in the query.
The _source field in the query specifies which fields to include in the response. Here, only user and post_date are included.
Consider this Elasticsearch query snippet:
{
"_source": { "excludes": ["comments", "metadata"] },
"query": { "match_all": {} }
}Which fields will not appear in the _source of the response?
Look at what the excludes array does in _source.
The excludes array tells Elasticsearch to omit those fields from the _source in the response.
Examine this Elasticsearch query:
{
"_source": ["user", {"post_date"}],
"query": { "match": {"message": "error"} }
}What is the cause of the error?
Check the format of the _source array elements.
The _source array must contain only strings representing field names. Here, {"post_date"} is an invalid object, causing a syntax error.
_source: false in a query?In Elasticsearch, what happens if you set "_source": false in your search query?
Think about what disabling _source means for the response.
Setting "_source": false tells Elasticsearch not to include the document source in the response, only metadata like _id and _score are returned.
Given this Elasticsearch query:
{
"_source": {
"includes": ["user", "comments.text"],
"excludes": ["user.email"]
},
"query": { "match_all": {} }
}Assuming documents have fields user.name, user.email, comments.text, and comments.date, how many fields will appear in the _source of each hit?
Remember that excludes remove fields even if includes select their parent.
The includes select user and comments.text. The excludes remove user.email. So user.name and comments.text remain.