kibana – Elasticsearch: multiply field in hits and field in inner hits

 

I’m trying to multiply two fields where one field is in the “hits” and another is in the “inner_hits”.

Let’s say I have following data.

{
  "cost": 2.26,
  "sizes": [
    {
      "ratio": 1,
      "guid": "sfgsfdaa-bec9-42ef-8b3e-54957acc97f3"
    },
    {
      "ratio": 2,
      "guid": "sfgsadfg-1cc2-4c73-84e9-fa6c180e5874"
    },
    {
      "ratio": 3,
      "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
    }
  ]
}

the “sizes” field are in nested type. I am using the following query to filter by guid.

{
  "query" : { 
    "nested": {
      "path": "sizes",
      "query": {
        "bool" : {
            "must" : [
                { "match": { "sizes.guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46" }}
            ]
        }
      },
      "inner_hits": {}
    }
  }
}

The result of the query is as follows

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 4.9041457,
    "hits" : [
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "3eEs43sBtPPJDcTfcaVG",
        "_score" : 4.9041457,
        "_source" : {
          "cost": 2.26, //field to be multipled
          "sizes": [
            {
              "ratio": 1,
              "guid": "sfgsfdaa-bec9-42ef-8b3e-54957acc97f3"
            },
            {
              "ratio": 2,
              "guid": "sfgsadfg-1cc2-4c73-84e9-fa6c180e5874"
            },
            {
              "ratio": 3,
              "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
            }
          ]
        },
        "inner_hits" : {
          "sizes" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 4.9041457,
              "hits" : [
                {
                  "_index" : "test-index",
                  "_type" : "_doc",
                  "_id" : "3eEs43sBtPPJDcTfcaVG",
                  "_nested" : {
                    "field" : "sizes",
                    "offset" : 2
                  },
                  "_score" : 4.9041457,
                  "_source" : {
                    "ratio": 3, //field to be multipled
                    "guid": "dhsfsfgs-c099-4c83-abbc-cc45a2ecaa46"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

So here is my question. How can I multiply “ratio” (inside “hits”=> “hits” => “inner_hits”=>”sizes”=>”hits”=>”hits”=>”_source”=>”ratio”) with “cost” (inside “hits”=> “hits” => “_source” => “cost”)? Is that possible?

Thanks in advance!

Source link