首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹性搜索中数据的计算

弹性搜索中数据的计算
EN

Stack Overflow用户
提问于 2020-09-22 07:03:51
回答 1查看 813关注 0票数 0

我试图计算数据和分配在相同的字段后,搜索结果。

代码语言:javascript
复制
{
  query: {
    "query_string": {
      "query": req.body.query
    }
  }
}

我正在得到搜索结果。

代码语言:javascript
复制
"results": [
  {
    "_index": "test_index",
    "_type": "_doc",
    "_id": "34",
    "_score": 1.8216469,
    "_source": {
      "pre_function_area": "100",
      "property_id": 46,
      "max_benchmark": 0,
    }
  }
]

在这里,我想在搜索期间修改max_benchmark。所以发送像as这样的查询。

代码语言:javascript
复制
{
  "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中的实际字段值。我只想在搜索后逻辑上更改值,这样它就会显示给用户。基本上,我试图计算下面的公式,并希望更新字段。

代码语言:javascript
复制
        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";
        }

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-22 08:27:07

你已经接近好的解决方案了。管理员将使用一个脚本字段。你可以在这里找到医生

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

代码语言:javascript
复制
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和脚本字段。(*已接受)。

例如:

代码语言:javascript
复制
GET test/_search
{
  "query": {
    "match_all": {}
  },
  "_source": "*", 
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "if(doc['title.keyword'].size()>0 ){doc['title.keyword'].value}"
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64004615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档