1

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?

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.