0

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!

Daniel
  • 338
  • 3
  • 13
  • 1
    Would it be conceivable to index another boolean field that contains the information (i.e. true if the fields match and false otherwise)? Or do you have too many field combinations to check? – Val Oct 02 '17 at 09:39
  • There are only 2 fields I need to check but this would have to be updated frequently. Looks like a solution. Thanks. – Daniel Oct 02 '17 at 09:41
  • When you update one or the other field, you can also update the boolean flag. Without that, you'll have to resort to using a script query which is much less performant and optimal. – Val Oct 02 '17 at 09:42
  • 1
    Please check [this thread](https://stackoverflow.com/questions/27708612/elasticsearch-comparison-between-fields) – Eli Oct 02 '17 at 09:52

1 Answers1

2

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"
                     }
                }
            }
        }
    }
}
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124