【问题标题】:How to calculate data in elastic search如何在弹性搜索中计算数据
【发布时间】:2020-09-22 07:03:51
【问题描述】:

我正在尝试计算数据并在搜索结果后在同一字段中分配。

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

我正在获取搜索结果。

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

这里我想在搜索过程中修改ma​​x_benchmark。所以像 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",
                   }
              }
          }
      }
  }

}

但它不会更新到字段 我不想更新 elasticsearch 中的实际字段值。我只想在搜索后在逻辑上更改值,以便将其显示给用户。基本上我正在尝试计算下面的公式并想要更新字段。

        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";
        }

谁能帮帮我

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-5


    【解决方案1】:

    您已接近最佳解决方案。 答案是使用脚本字段。 你可以在这里找到文档

    [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
            }
          }
        }
      }
    }
    

    编辑:回复您的评论。 您不能将字段添加到 _source,但您可以通过在源中指定所需的字段来获取 _source 和脚本字段。 (* 被接受)。

    举个例子:

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

    【讨论】:

    • 是的,但它清除了 _source 的完整 json。我正在尝试在 _source 中附加一个参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2012-03-06
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多