How to retrieve partial document in elasticSearch 6.x?

Viewed 83

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

1 Answers

Using nested inner hits, you can retrieve only matching results.

As mentioned in the documentation,

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

Since the index mapping and data provided are too long, so adding a working example by just considering the merchCategories part.

Index Mapping:

{
  "mappings": {
    "properties": {
      "merchCategories": {
        "type": "nested"
      }
    }
  }
}

Index Data:

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

Search Query:

{
  "query": {
    "nested": {
      "path": "merchCategories",
      "inner_hits": {        
        "_source": [                     
          "merchCategories.pyramid"       // you can add more fields here
        ]
      },
      "query": {
        "bool": {
          "should": [
            {
              "terms": {
                "merchCategories.pyramid.keyword": [
                  "PY6"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65437532",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "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
                }
              }
            }
          ]
        },
        "inner_hits": {
          "merchCategories": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                {
                  "_index": "65437532",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "merchCategories",
                    "offset": 0
                  },
                  "_score": 1.0,
                  "_source": {
                    "pyramid": "PY6"         // note this
                  }
                }
              ]
            }
          }
        }
      }
    ]

Update 1:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "primaryChannel.keyword": [
              "online_only"
            ],
            "boost": 1
          }
        },
        {
          "range": {
            "endDate": {
              "from": 1483654399000,
              "to": null,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "startDate": {
              "from": null,
              "to": 1589049600000,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "nested": {
            "path": "merchCategories",
            "inner_hits": {
              "_source": [
                "merchCategories.pyramid"
              ]
            },
            "query": {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "merchCategories.pyramid.keyword": [
                        "PY6"
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "should": {
        "nested": {
          "path": "merchCategories",
          "query": {
            "bool": {
              "should": [
                {
                  "terms": {
                    "merchCategories.pyramid": [
                      "PYR-13"
                    ],
                    "boost": 1
                  }
                },
                {
                  "terms": {
                    "merchCategories.division": [
                      "DIV-15"
                    ],
                    "boost": 1
                  }
                },
                {
                  "terms": {
                    "merchCategories.department": [
                      "94"
                    ],
                    "boost": 1
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
Related