Search by script with field range + docs without exesting fields

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

{
        "_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:

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:

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:
"must_not": {
"exists": {
"field": "your_field"
}
}

to a query above?

Hi @orlenkoda5

Maybe this works:

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

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

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

and

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

in this case

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