Elasticsearch比较两个字段

时间:2017-03-28 08:54:09

标签: elasticsearch

例如MySQL查询

SELECT fields_a, fields_b FROM table WHERE fields_a > fields_b;

我正在尝试为elasticsearch实施。我试过了,如下:

$where = [ "query" => [ "filter" => [ "script" => [ "script" => "doc[\"fields_a\"].value > doc[\"fields_b\"].value" ] ] ] ];

我错过了什么?

1 个答案:

答案 0 :(得分:2)

这对我有用

{
    "query": {
        "bool": {
            "must": [{
                "script": {
                    "script": "doc['field_a'].value > doc['field_b'].value"
                }
            }]
        }
    }
}

要仅选择少数字段而不是整个源文档,请使用stored_fields "stored_fields": ["field_a","field_b"],并确保在映射中将这些字段设为store=true

{
stored_fields": ["field_a","field_b"]
    "query": {
        "bool": {
            "must": [{
                "script": {
                    "script": "doc['field_a'].value > doc['field_b']"
                }
            }]
        }
    }
}