Home > OS >  Elastic search source filtering query for nested JSON document
Elastic search source filtering query for nested JSON document

Time:01-10

I have the below JSON object as _source in Elastic search. I need to filter the source object based on conditions. For example, I need only JSON with applied_as == "COMMISSION"

  "_source": {
      "factor" : [
        {
          "some_amount_usd" : [
            {
              "applied_as" : "TCKT_CNT",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-01-01"
            },
            {
              "applied_as" : "TCKT_CNT",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-02-01"
            },
            {
              "applied_as" : "COMMISSION",
              "version" : "8",
              "factor_value" : "1.12",
              "start_date" : "2022-02-01"
            },
          ]
        }
      ]
    }

I am using this documentation. https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-fields.html#source-filtering

I am currently using this query with no luck. What am I missing?

GET form_some_index/_search
{
  "query": {
    "match": {
      "factor.some_amount_usd.applied_as": "COMMISSION"
    }
  }

}

CodePudding user response:

considering that the factor object is a single document and the (factor ,some_amount_usd) are nested field type, according to the official document, it is not possible to do what you expect with multi level nested queries. Refer to the mentioned link for more information.

CodePudding user response:

You can start using Nested Query.

{
  "query": {
    "nested": {
      "path": "factor",
      "query": {
        "nested": {
          "path": "factor.some_amount_usd",
          "query": {
            "match": {
              "factor.some_amount_usd.applied_as": {
                "query": "COMMISSION"
              }
            }
          }
        }
      }
    }
  }
}
  • Related