Search code examples
c#elasticsearchnreco

NReco Measure Name is autogenerated and not the one I choose


I'm working on a query to be executed by Elasticsearch showing results on NReco PivotData microservice.

I make the query easier just for the example purpose. There are two fields "anno" (year) and "mese" (month) and I need to sum them for each record.

This is what I'm using as a measure in the "appsettings.json" file:

{
      "Name": "SumTry",
      "LabelText": "SumTry",
      "Type": "FirstValue",
      "Params": ["{\"script\":{\"source\": \"doc['anno'].value + doc['mese'].value\"}}"]
}

Error

This is the error I get. It seems that the "Name" field doesn't work at all.

If I try to access the field differently:

{
      "Name": "SumTry",
      "LabelText": "SumTry",
      "Type": "FirstValue",
      "Params": ["{\"script\":{\"source\": \"doc[\"anno\"].value + doc[\"mese\"].value\"}}"]
}

I get this other error:

enter image description here

Any idea about how to solve it?

PS. I know I can use expressions, like the following one (it works), but I just want to test if there is any difference in performance between the two methods.

{
    "Name": "anno",
    "Type": "Sum",
    "Params": [ "anno" ]
  },
  {
    "Name": "mese",
    "Type": "Sum",
    "Params": [ "mese" ]
  },
  {
    "Name": "SumTry",
    "Type": "Expression",
    "Params": [
      "anno + mese",
      "anno",
      "mese"
    ]
  }

Solution

  • It is unclear which actually aggregation function should be used in the case of this definition:

    {
          "Name": "SumTry",
          "LabelText": "SumTry",
          "Type": "FirstValue",
          "Params": ["{\"script\":{\"source\": \"doc['anno'].value + doc['mese'].value\"}}"]
    }
    

    When measure Type is "FirstValue" you can specify custom metric JSON as a first value in "Params" - but this should be valid metric aggregation as described here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html

    In your case, there is no such aggregation as "script". If I understand your goal correctly, technically it is possible to achieve what you want via mapped field that is used as an argument for "sum": https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#_script_14

    Note that PivotDataService doesn't support 'runtime_mappings' in the query as for now.