# Search by script with field range + docs without exesting fields

**URL:** <https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring\
**Created:** [July 14, 2023, 10:43am UTC](https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403 "2023-07-14T10:43:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [July 14, 2023, 10:43am UTC](https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403/1 "2023-07-14T10:43:16Z")

</div>

Hi, I try to make a query using template. Here are docs in my index:

```auto
{
        "_index" : "instruments",
        "_type" : "_doc",
        "_id" : "721905",
        "_score" : null,
        "_source" : {
          "sessionDate" : "2023-07-14",
          "dailyPiPercent" : "0.0",
          "exTime" : "2023-07-13T18:24:41.410",
          "volToday" : "21",
          "yield" : "7.98",
          "valToday" : "900.0",
          "idfi" : "721905",
          "turnover" : "1402431.7164",
          "low" : "89.0",
          "high" : "92.0",
          "price" : "89.5",
          "idTradePeriodStatus" : "6",
          "faceUunit" : "174366",
          "nkd" : "8.6416667",
          "actId" : "121336",
          "regCode" : "",
          "emCode" : "MMCF",
          "nominalCurr" : "USD",
          "lot" : "1",
          "emShortName" : "MMC Finance DAC",
          "nominal" : "1000.0",
          "isLiquidBoard" : "true",
          "currency" : "USD",
          "isStruct" : "false",
          "cupSize" : "12.75",
          "expared" : "false",
          "objectId" : "635099",
          "ticker" : "do-5285",
          "hellType" : "Облигации купонные",
          "assetCode" : "XS2134628069",
          "riskCurrency" : "USD",
          "universalMarketCode" : "MOEX_TQ_USD",
          "isDefaultUniversalExchange" : "true",
          "displayGroup" : "106",
          "emitent" : "485285",
          "hellGroup" : "Облигации",
          "boardName" : "МБ ЦК USD",
          "allowNonQualInvest" : "false",
          "bondTypeName" : "Еврооблигации",
          "adfrontType" : "еврооблигации",
          "cupDate" : "2022-09-11",
          "isEnabled" : "true",
          "name" : "MMC Finance DAC 11/09/25",
          "adfrontGroup" : "Bonds",
          "universalExchangeId" : "1",
          "nkdCurr" : "USD",
          "cupPeriod" : "180",
          "isin" : "XS2134628069",
          "matDate" : "2025-09-11"
        },
        "sort" : [
          7.98
        ]
      },
      {
        "_index" : "instruments",
        "_type" : "_doc",
        "_id" : "1963712",
        "_score" : null,
        "_source" : {
          "sessionDate" : "2023-07-14",
          "dailyPiPercent" : "-0.094439",
          "exTime" : "2023-07-14T13:01:32.540",
          "volToday" : "2",
          "yield" : "7.37",
          "valToday" : "970.0",
          "idfi" : "1963712",
          "turnover" : "142245.3648",
          "low" : "95.21",
          "high" : "95.21",
          "price" : "95.21",
          "idTradePeriodStatus" : "6",
          "faceUunit" : "174366",
          "nkd" : "7.03125",
          "actId" : "123065",
          "emCode" : "MMCF",
          "nominalCurr" : "USD",
          "lot" : "1",
          "emShortName" : "MMC Finance DAC",
          "nominal" : "1000.0",
          "isLiquidBoard" : "true",
          "currency" : "USD",
          "isStruct" : "false",
          "cupSize" : "16.88",
          "expared" : "false",
          "objectId" : "806931",
          "ticker" : "MMCF24",
          "hellType" : "Облигации купонные",
          "assetCode" : "XS2069992258",
          "riskCurrency" : "USD",
          "universalMarketCode" : "MOEX_TQ_USD",
          "isDefaultUniversalExchange" : "true",
          "displayGroup" : "106",
          "emitent" : "485285",
          "hellGroup" : "Облигации",
          "boardName" : "МБ ЦК USD",
          "allowNonQualInvest" : "false",
          "bondTypeName" : "Еврооблигации",
          "adfrontType" : "еврооблигации",
          "cupDate" : "2023-10-28",
          "isEnabled" : "true",
          "name" : "MMC Finance DAC",
          "adfrontGroup" : "Bonds",
          "universalExchangeId" : "1",
          "nkdCurr" : "USD",
          "cupPeriod" : "182",
          "isin" : "XS2069992258",
          "matDate" : "2024-10-28"
        },
        "sort" : [
          7.37
        ]
      },
      {
        "_index" : "instruments",
        "_type" : "_doc",
        "_id" : "766324",
        "_score" : null,
        "_source" : {
          "sessionDate" : "2023-07-14",
          "dailyPiPercent" : "0.0",
          "exTime" : "2023-07-05T14:50:52.990",
          "volToday" : "2",
          "valToday" : "499.0",
          "idfi" : "766324",
          "turnover" : "70973.265",
          "low" : "47.51",
          "high" : "47.51",
          "price" : "47.51",
          "idTradePeriodStatus" : "6",
          "faceUunit" : "174366",
          "nkd" : "12.666667",
          "actId" : "121989",
          "emCode" : "SCCD",
          "nominalCurr" : "USD",
          "lot" : "1",
          "emShortName" : "SovCom Capital D.A.C.",
          "nominal" : "1000.0",
          "isLiquidBoard" : "true",
          "currency" : "USD",
          "isStruct" : "false",
          "cupSize" : "19.0",
          "expared" : "false",
          "objectId" : "654477",
          "ticker" : "SCCDd070426",
          "hellType" : "Облигации купонные",
          "assetCode" : "XS2393688598",
          "riskCurrency" : "USD",
          "universalMarketCode" : "MOEX_TQ_USD",
          "isDefaultUniversalExchange" : "true",
          "displayGroup" : "106",
          "emitent" : "548791",
          "hellGroup" : "Облигации",
          "boardName" : "МБ ЦК USD",
          "allowNonQualInvest" : "false",
          "bondTypeName" : "Еврооблигации",
          "adfrontType" : "еврооблигации",
          "cupDate" : "2022-08-17",
          "isEnabled" : "true",
          "name" : "SovCom Capital D.A.C. о.к.07.04.2026",
          "adfrontGroup" : "Bonds",
          "universalExchangeId" : "1",
          "nkdCurr" : "USD",
          "cupPeriod" : "0",
          "isin" : "XS2393688598",
          "matDate" : "2026-04-07"
        }

```

I have this template:

```auto
PUT _scripts/multipurpose_template
{
"script": {
"lang": "mustache",
"source": """{"sort": {{#toJson}}sort{{/toJson}},"from":"{{from}}","size":"{{size}}","query":{"bool":{"must":{{#toJson}}clauses{{/toJson}}}}}"""
}
}

```

And now I need to get docs according to clauses below:

```auto
GET instruments/_search/template
{
"id": "multipurpose_template",
"params": {
"from": 0,
"size": 100,
"sort": {
"yield": {
"order": "desc"
}
},
"clauses": [
{
"match":{
"adfrontGroup":"Bonds"
}
},
{
"match":{
"isDefaultUniversalExchange":"true"
}
},
{
"match":{
"isLiquidBoard":"true"
}
},
{
"match":{
"bondTypeName":"Еврооблигации"
}
},
{
"range": {
"yield": {
"gte": "0",
"lte": "15.00"
}
}
},
{
"match": {
"currency": "RUB USD EUR"
}
},
{
"match":{
"isEnabled":"true"
}
},
{
"range": {
"matDate": {
"gte": "2023-05-17",
"lte": "2100-01-01"
}
}
}
]
}
}

```

- all docs without field "yield". So how can I add condition:

```auto
"must_not": {
"exists": {
"field": "your_field"
}
}

```

to a query above?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 14, 2023, 3:35pm UTC](https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403/2 "2023-07-14T15:35:57Z")

</div>

Hi @orlenkoda5

Maybe this works:

```auto
"clauses": [
        {
          "match": {
            "adfrontGroup": "Bonds"
          }
        },
        {
          "match": {
            "isDefaultUniversalExchange": "true"
          }
        },
        {
          "match": {
            "isLiquidBoard": "true"
          }
        },
        {
          "match": {
            "bondTypeName": "Еврооблигации"
          }
        },
        {
          "range": {
            "yield": {
              "gte": "0",
              "lte": "15.00"
            }
          }
        },
        {
          "match": {
            "currency": "RUB USD EUR"
          }
        },
        {
          "match": {
            "isEnabled": "true"
          }
        },
        {
          "range": {
            "matDate": {
              "gte": "2023-05-17",
              "lte": "2100-01-01"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "your_field"
              }
            }
          }
        }
      ]

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [July 17, 2023, 9:54am UTC](https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403/3 "2023-07-17T09:54:24Z")

</div>

Thank you for your answer, but it doesn't work. I've tried it already. I suppose that there is a conflict between clauses:

```auto
{
          "range": {
            "yield": {
              "gte": "0",
              "lte": "15.00"
            }
          }
        }

```

and

```auto
{
          "bool": {
            "must_not": {
              "exists": {
                "field": "your_field"
              }
            }
          }
        }

```

in this case

---

<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:** [August 14, 2023, 9:54am UTC](https://discuss.elastic.co/t/search-by-script-with-field-range-docs-without-exesting-fields/338403/4 "2023-08-14T09:54:44Z")

</div>

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