# Dynamic filter according to the presence of a field

**URL:** <https://discuss.elastic.co/t/dynamic-filter-according-to-the-presence-of-a-field/312944>\
**Category:** Elasticsearch\
**Created:** [August 25, 2022, 4:10pm UTC](https://discuss.elastic.co/t/dynamic-filter-according-to-the-presence-of-a-field/312944 "2022-08-25T16:10:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arnoz/32/110023_2.png) [@arnoz](https://discuss.elastic.co/u/arnoz)\
**Post date:** [August 25, 2022, 4:10pm UTC](https://discuss.elastic.co/t/dynamic-filter-according-to-the-presence-of-a-field/312944/1 "2022-08-25T16:10:55Z")

</div>

I have for example this type of documents.

```json
{
  "name": "sylvie dupont",
  "age": 25,
  "accounts": [
    {
      "name": "BankOfAmerica",
      "amount": 33
    },
    {
      "name": "GoldBank",
      "amount": 578
    }
  ]
},
{
  "name": "marie friconnet",
  "age": 40,
  "accounts": [
    {
      "name": "SaxonBank",
      "amount": 896
    },
    {
      "name": "BankOfAmerica",
      "amount": 89
    },
    {
      "name": "GoldBank",
      "amount": 64
    }
  ]
},
{
  "name": "sylvie houmeau",
  "age": 23,
  "accounts": [
    {
      "name": "BankOfJapan",
      "amount": 677
    },
    {
      "name": "BankOfAmerica",
      "amount": 556
    },
    {
      "name": "BankOfEurope",
      "amount": 653
    },
    {
      "name": "GoldBank",
      "amount": 267
    }
  ]
}

```

I have to filter and order them dynamically according to whether the field exists or not (inside **accounts** field).  
For example if there is **SaxonBank** I have to filter on **SaxonBank** but if there is **BankOfAmerica** then I forget **SaxonBank** for use **BankOfAmerica** , if **BankOfEurope** exists then I filter on **BankOfEurope** etc, and not on the others.  
If none of these three banks exist then I order my documents using the **GoldBank** field **which always exists**. 🙂

I don't have to deal with the other bank fields at all if when the "priority bank" is present.  
_(here are example data to illustrate my need)_

At this stage, I understood that I could use scripts to filter and order but it seems to me complex that's why I would like to take your opinion on the method to use before doing tedious research

thank you very much for your help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 22, 2022, 4:11pm UTC](https://discuss.elastic.co/t/dynamic-filter-according-to-the-presence-of-a-field/312944/2 "2022-09-22T16:11:17Z")

</div>

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