First of all let me share you the mapping and few document example of my index
{
"umbrella_dev": {
"aliases": {
},
"mappings": {
"_doc": {
"dynamic_templates": [
{
"strings_as_keywords": {
"unmatch": "umbrellaName",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"createdBy": {
"type": "keyword"
},
"createdDate": {
"type": "date",
"format": "epoch_millis"
},
"description": {
"type": "keyword"
},
"doc_id": {
"type": "object",
"enabled": false
},
"endDate": {
"type": "date",
"format": "epoch_millis"
},
"forecastSummary": {
"properties": {
"totalDmarkdown": {
"type": "long"
},
"totalIncrementalSales": {
"type": "long"
},
"totalIncrementalUnits": {
"type": "long"
},
"totalPromoSales": {
"type": "long"
},
"totalPromoUnits": {
"type": "long"
}
}
},
"lastModifiedBy": {
"type": "keyword"
},
"lastModifiedDate": {
"type": "date",
"format": "epoch_millis"
},
"marketingEvent": {
"properties": {
"createdBy": {
"type": "keyword"
},
"createdDate": {
"type": "long"
},
"lastModifiedBy": {
"type": "keyword"
},
"lastModifiedDate": {
"type": "long"
},
"marketingEventId": {
"type": "keyword"
},
"marketingEventName": {
"type": "keyword"
}
}
},
"merchCategories": {
"properties": {
"department": {
"type": "keyword"
},
"division": {
"type": "keyword"
},
"forecasts": {
"properties": {
"online_and_store": {
"properties": {
"dmarkdown": {
"type": "float"
},
"forecastId": {
"type": "long"
},
"incrementalSales": {
"type": "float"
},
"incrementalUnits": {
"type": "long"
},
"promoSales": {
"type": "float"
},
"promoTotalUnits": {
"type": "long"
}
}
},
"online_only": {
"properties": {
"dmarkdown": {
"type": "long"
},
"forecastId": {
"type": "long"
},
"incrementalSales": {
"type": "long"
},
"incrementalUnits": {
"type": "long"
},
"promoSales": {
"type": "long"
},
"promoTotalUnits": {
"type": "long"
}
}
},
"store_only": {
"properties": {
"dmarkdown": {
"type": "long"
},
"forecastId": {
"type": "long"
},
"incrementalSales": {
"type": "long"
},
"incrementalUnits": {
"type": "long"
},
"promoSales": {
"type": "long"
},
"promoTotalUnits": {
"type": "long"
}
}
}
}
},
"label": {
"type": "keyword"
},
"merchCategoryId": {
"type": "keyword"
},
"merchCategoryName": {
"type": "keyword"
},
"productClass": {
"type": "keyword"
},
"pyramid": {
"type": "keyword"
},
"subclass": {
"type": "keyword"
},
"umbrellaMerchCategoryId": {
"type": "long"
}
}
},
"primaryChannel": {
"type": "keyword"
},
"primaryIntent": {
"type": "keyword"
},
"priorityId": {
"type": "long"
},
"reason": {
"type": "keyword"
},
"startDate": {
"type": "date",
"format": "epoch_millis"
},
"status": {
"type": "keyword"
},
"umbrellaId": {
"type": "keyword"
},
"umbrellaName": {
"type": "text",
"fields": {
"normalized": {
"type": "keyword",
"normalizer": "case_insensitive"
}
},
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"vehicles": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"max_ngram_diff": "50",
"number_of_shards": "2",
"provided_name": "umbrella_dev",
"creation_date": "1606219640501",
"analysis": {
"filter": {
"autocomplete_filter": {
"token_chars": [
"letter",
"digit",
"symbol",
"punctuation"
],
"min_gram": "1",
"type": "ngram",
"max_gram": "30"
}
},
"normalizer": {
"case_insensitive": {
"filter": "lowercase"
}
},
"analyzer": {
"autocomplete": {
"filter": [
"lowercase",
"autocomplete_filter"
],
"type": "custom",
"tokenizer": "standard"
}
}
},
"number_of_replicas": "1",
"uuid": "cszeJu3AShSP5VN4W7MDug",
"version": {
"created": "6040399"
}
}
}
}
}
document example:
{
"umbrellaId": "6928",
"umbrellaName": "Tesumbrella1",
"startDate": 1583049600000,
"endDate": 1583654399000,
"description": null,
"primaryIntent": "DRIVE_SALES",
"reason": "REGULAR_PROMOTION",
"primaryChannel": "online_only",
"status": "ACTIVE",
"primaryPromotionClass": null,
"createdDate": 1585051624920,
"createdBy": "productplanstorepub",
"lastModifiedDate": 1597673631723,
"lastModifiedBy": "product",
"marketingEvent": null,
"priorityId": 6748,
"vehicles": null,
"merchCategories": [
{
"umbrellaMerchCategoryId": 6929,
"merchCategoryId": "PY6",
"merchCategoryName": "ESSENTIALS",
"label": "PYRAMID",
"pyramid": "PY6",
"division": null,
"department": null,
"productClass": null,
"subclass": null,
"forecasts": {
"online_only": {
"forecastId": 6930,
"promoSales": 1,
"dmarkdown": 2,
"incrementalSales": null,
"incrementalUnits": null,
"promoTotalUnits": null
}
}
}
],
"promotionIds": null,
"forecastSummary": {
"totalPromoSales": 1,
"totalDmarkdown": 2,
"totalIncrementalSales": 0,
"totalIncrementalUnits": 0,
"totalPromoUnits": 0
}
}
so under every document there is a field called merchCategories which is an array of objects. I want to fetch only the merchCategories which matches my query For example if any document containes 10 object under merchCategories array then i want to fetch only those merchCategories which matches my query and my query can contain any matching term of the document.
sample query
{
"from": 0,
"size": 100,
"query": {
"bool": {
"must": [
{
"terms": {
"primaryChannel": [
"online_only"
],
"boost": 1
}
},
{
"range": {
"endDate": {
"from": 1572764400000,
"to": null,
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"range": {
"startDate": {
"from": null,
"to": 1573372799000,
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"should": [
{
"terms": {
"merchCategories.pyramid": [
"PYR-13"
],
"boost": 1
}
},
{
"terms": {
"merchCategories.division": [
"DIV-15"
],
"boost": 1
}
},
{
"terms": {
"merchCategories.department": [
"94"
],
"boost": 1
}
}
],
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1
}
},
"_source": {
"includes": [],
"excludes": []
},
"sort": [
{
"startDate": {
"order": "asc"
}
},
{
"umbrellaId": {
"order": "asc"
}
}
]
}
but this is return whole documents which contains merchCategories matching to the terms provided in the should clause but i don't want to whole document i just want those particular merchCateogries not all the categories only those which are matching the terms One desired way whole document with specific merchCategories truncating others or returning only the specific merchCategories as a result