Scripted filter with fields from the top AND the nested document


Suppose I have an order index with the following mapping:

  "leadTime": 2,
  "shipments": [{
    "packageId": 1,
    "shipped": "2019-06-10",
    "received": null
  }, {
    "packageId": 2,
    "shipped": "2019-06-08",
    "received": null

Now I want to answer the question:

Which packages are shipped before now - leadTime days but not yet received as of now?

The leadTime varies for different orders.

Now I find myself stuck between two dead ends:

  • If I try to use a script filter on the nested shipments document, the script won't have access the leadTime field on the top document.

  • If I try to put the script filter on the outer query, the script can not access the nested shipments document since doc['shipments'] is missing.

Is there any way for a script in the filter context to access both the top document fields AND the fields from the nested document?

Thanks for any advice.

There isn't a way, since filter scripts operate on each individual document within lucene, and the parent document, as well as each inner object, are all separate documents (to lucene).

One way to handle this is to add the leadTime field to all your nested documents. You could do this when building the document for indexing, or in an ingest pipeline.

Thanks for the clear explanation.

Now I understand that a workaround has to be implemented on the application level to ensure all fields used in a query be available on the same lucene document.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.