elasticsearch - 如何在搜索时对弹性搜索中的字段执行数学运算?
问题描述
有没有什么方法可以在 Elasticsearch 数学运算中执行,就像下面在 SQL 中描述的那样?如果是,执行此类搜索操作的最佳方法是什么?
select * from products where price + 50 > 20
解决方案
使用无痛脚本查询(https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html)是可行的。
试试这个:
GET /products/_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "(doc['price'].value + 50) > 20",
"lang": "painless"
}
}
}
}
}
}
类似行为的准备好的语句可以这样完成:
GET /products/_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "(doc['price'].value + params.foo) > params.bar",
"lang": "painless",
"params" : {
"foo" : 50,
"bar" : 20
}
}
}
}
}
}
}
推荐阅读
- android - 将 JSON 字符串转换为对象列表
- .net - ASP.NET Core application not starting in IIS (targets net461)
- javascript - React & Webpack - 为多页面应用程序设置项目
- visio - 编辑 UML 图表时 MS Visio 2010 挂起
- powershell - delete everything after keyword
- google-bigquery - Check if a dataset has been updated with new table BigQuery
- ntp - Should I specify regional NTP pool or just pool.ntp.org?
- powershell - 希望使用 powershell 从文本文件中删除文本
- python - pip install 2 packages from requirements one after the other
- python - Mayavi - 在 imshow() 图形的数据范围之外选择返回 (0,0,0);如何区分?