Home > Mobile >  How to create a range query on array
How to create a range query on array

Time:10-16

Here is the result of the query:

{
  "query": {
    "match_all": {}
  }
}
{
  "data": [
    {
      "hits": [
        {
          "_source": {
            "AR_UnitePoids": 3,
            "CatTarif": [
              {
                "PVTTCSite": 1
              },
              {
                "PVTTCSite": 2
              }
            ]
          }
        },
        {
          "_source": {
            "AR_UnitePoids": 5,
            "CatTarif": [
              {
                "PVTTCSite": 3
              },
              {
                "PVTTCSite": 4
              }
            ]
          }
        }
      ]
    }
  ]
}

If I want to get documents with AR_UnitePoids >= 5, I can do:

{
  "query": {
    "range": {
      "AR_UnitePoids": {
        "gte": 5
      }
    }
  }
}
{
  "data": [
    {
      "hits": [
        {
          "_source": {
            "AR_UnitePoids": 5,
            "CatTarif": [
              {
                "PVTTCSite": 3
              },
              {
                "PVTTCSite": 4
              }
            ]
          }
        }
      ]
    }
  ]
}

But now I want all documents where the second PVTTCSite of CatTarif is higher than 4:

{
  "query": {
    "range": {
      "CatTarif.1.PVTTCSite": {
        "gte": 4
      }
    }
  }
}

But this query doesn't work, I get 0 hits.

How can I do this ?

Edit:

Thank to @TusharShahi comment and this doc I managed to create this query:

{
  "query": {
    "nested": {
      "path": "CatTarif",
      "query": {
        "bool": {
          "must": [
            { "range": { "CatTarif.PVTTCSite": { "gte": 4 } } }
          ]
        }
      }
    }
  }
}

which works but I still can't query on a specific index of the CatTarif.

CodePudding user response:

{
  "query": {
    "nested": {
      "path": "CatTarif",
      "query": {
        "bool": {
          "must": {
            "range": {
              "CatTarif.PVTTCSite": {
                "gte": 4
              }
            }
          },
          "filter": {
            "term": {
              "CatTarif.CatTarif": 1
            }
          }
        }
      }
    }
  }
}
  • Related