0

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.

3
  • 1
    Please add index. Is the CarTarif field a nested field? Because if not then it is not possible. Normal objects are flattened in ES Commented Oct 11, 2021 at 17:50
  • @TusharShahi, what do you mean by "add index" ? Yes CatTarif is a nested field Commented Oct 11, 2021 at 19:11
  • Take a look at stackoverflow.com/a/56948351/4800344 Commented Oct 11, 2021 at 20:34

1 Answer 1

0
{
  "query": {
    "nested": {
      "path": "CatTarif",
      "query": {
        "bool": {
          "must": {
            "range": {
              "CatTarif.PVTTCSite": {
                "gte": 4
              }
            }
          },
          "filter": {
            "term": {
              "CatTarif.CatTarif": 1
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.