【问题标题】:How can I perform math operations on fields in elasticsearch while searching?如何在搜索时对弹性搜索中的字段执行数学运算?
【发布时间】:2018-11-09 08:57:39
【问题描述】:

是否有任何方法可以在 Elasticsearch 数学运算中执行如下所述的 SQL 运算?如果是,执行此类搜索操作的最佳方式是什么?

select * from products where price + 50 > 20

【问题讨论】:

    标签: elasticsearch math search


    【解决方案1】:

    使用无痛脚本查询 (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
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2021-01-21
      • 2020-02-04
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多