Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can use Source Filtering feature to exclude _source (object/nested data) in elasticsearch query results. This examples excludes child _source object completely.


Mapping


As you can see below, 1 parent product document can have many child tags documents in it.


{
"product_idx": {
"mappings": {
"product": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
},
"tags": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
}
}
}
}
}
}
}
}

Including tags


curl -XPOST 'http://localhost:9200/product_idx/_search' -d '{
"query": {
"term": {
"id": 1
}
}
}'

# Result
{
"hits": {
"hits": [
{
"_index": "product_idx",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "Phone",
"tags": [
{
"id": 1,
"name": "T1"
},
{
"id": 2,
"name": "T2"
}
]
}
}
]
}
}

Excluding tags


curl -XPOST 'http://localhost:9200/product_idx/_search' -d '{
"query": {
"term": {
"id": 1
}
},
"_source": {
"excludes": [
"tags"
]
}
}'

# Result
{
"hits": {
"hits": [
{
"_index": "product_idx",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "Phone"
}
}
]
}
}