Or Assayag
Or Assayag

Reputation: 6336

Elasticsearch C# NEST not match other field value

I'm trying to build a query with Elasticsearch C# NEST library. I have a table in SQL, let's say it called Mail. I need to make sure that one field does not equals to another in a query.

In SQL:

SELECT * FROM MAILS
WHERE Field_A != Field_B

How should I do it with C# NEST?

Upvotes: 1

Views: 1140

Answers (1)

Elasticsearch is not intended for this type of functionality, you may be better served looking into more efficient ways to setup your project to be able to hand this, however there are some tools which can allow you to shoehorn in this form of a query.

While the basic query syntax doesn't encompass comparing fields, Scripts can allow you work around this.

Here is an example of a script using NEST:

.Query(q=>q
    .Term(w => w.MatchAll())
        .Filter(s => s.Script(sf => sf.Script("doc['mails.field_A'].value == doc['mails.field_B'].value"))
)

Here is an example of a script without using NEST:

{
    "query": {
        "filtered": {
            "filter": {
                "script": {
                    "script": "doc['mails.field_A'].value == doc['mails.field_B'].value"
                }
            }
        }
    }
}

This will function provided script.disable_dynamic is set to false. Some security issues can arise.

Upvotes: 2

Related Questions