CPA
CPA

Reputation: 3053

Lucene query where two fields will be compared

I have an elasticsearch cluster. All documents in the cluster have the same index and type. Each document has two number fields -> field1 and field2.

I want to display all documents in Grafana, where value of field1 > value of field2.

Is there a query like:

document_type:test AND field1 > field2 ?

Upvotes: 3

Views: 3968

Answers (2)

femtoRgon
femtoRgon

Reputation: 33341

You can do this with a (groovy) script query, like this:

{
    "query" : {
        "term" : {
            "document_type" : "test"
        }
    },
    "filter" : {
        "script" : {
            "script" : "doc['field1'].value > doc['field2'].value"
        }
    }
}

See also, more documentation on what is available from the Elasticsearch scripting module.

Upvotes: 2

AussieDan
AussieDan

Reputation: 2176

As far as I'm aware there is no way to perform that sort of query using elasticsearch (lucene). It does support range queries, but not comparison between different fields in the document.

Upvotes: 3

Related Questions