Search code examples
elasticsearchelastic-stackelasticsearch-5

How to calculate data in elastic search


I am trying to calculate data and assign in same field after search result.

{
  query: {
    "query_string": {
      "query": req.body.query
    }
  }
}

I am getting search result.

"results": [
  {
    "_index": "test_index",
    "_type": "_doc",
    "_id": "34",
    "_score": 1.8216469,
    "_source": {
      "pre_function_area": "100",
      "property_id": 46,
      "max_benchmark": 0,
    }
  }
]

Here i want to modified max_benchmark during search. So sending query like as.

{
  "query": {
      "bool" : {
        "must" : {
          "query_string": {
            "query": "test"
          }
        },
          "filter" : {
              "script" : {
                  "script" : { //Math.round((pow *  doc['max_benchmark'].value) * 10) / 10
                      "lang": "expression",
                    //  "lang": "painless",
                      "source": "doc['max_benchmark'].value * 5",
                   }
              }
          }
      }
  }

}

but it does not update to field i don't want to update actually field value in elasticsearch. I just want logically change value after search so it will display to user. Basically I am trying to calculate below formula and want to update field.

        let months = 0;
          if(event_date != "") {
            let ai_date = moment(); 
            ai_date.month(obj._source.month);
            ai_date.year(obj._source.year);
            months = ai_date.diff(event_date, 'months');
          }
          console.log("months "+months);
          let pow = Math.pow(1.009,months);
          obj._source.max_benchmark_cal = Math.round((pow *  obj._source.max_benchmark) * 10) / 10;
          obj._source.min_benchmark_cal = Math.round((pow *  obj._source.min_benchmark) * 10) / 10;
        } else {
          obj._source.max_benchmark_cal = "NA";
          obj._source.min_benchmark_cal = "NA";
        }

can anyone please help me


Solution

  • your are near the good solution. The asswer is to use a script field. You can find the doc here

    [https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-fields.html#script-fields]1

    GET /_search
    {
      "query": {
        "match_all": {}
      },
      "script_fields": {
        "test1": {
          "script": {
            "lang": "painless",
            "source": "doc['price'].value * 2"
          }
        },
        "test2": {
          "script": {
            "lang": "painless",
            "source": "doc['price'].value * params.factor",
            "params": {
              "factor": 2.0
            }
          }
        }
      }
    }
    

    EDIT: To respond to your comment. You can not add a field to _source, but you can get the _source and the scripted field by specifying the fields you want in the source. (* are accepted).

    As an example:

    GET test/_search
    {
      "query": {
        "match_all": {}
      },
      "_source": "*", 
      "script_fields": {
        "test1": {
          "script": {
            "lang": "painless",
            "source": "if(doc['title.keyword'].size()>0 ){doc['title.keyword'].value}"
          }
        }
      }
    }