Daniel
Daniel

Reputation: 338

Query string query - two fields must match

I'm using ElasticSearch 5.1 and I wonder if it's possible to build query string query which will match all records where fields field_1 and field_2 are the same.

At the time of executing query I don't know what value they store. All I know is mapping which is keyword.

So for example data:

{"id": 1, "field_1": "foo", "field_2": "foo"}
{"id": 2, "field_1": "foo", "field_2": "bar"}

when I execute such query I want to get only record with id 1 because field_1===field_2

Thanks!

Upvotes: 0

Views: 1077

Answers (1)

Thomas Decaux
Thomas Decaux

Reputation: 22691

If you can use another query than query string, what about a script query?

From https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-script-query.html

GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['field_1'].value == doc['field_2'].value"
                     }
                }
            }
        }
    }
}

Upvotes: 2

Related Questions