Search code examples
c#elasticsearchnest

Elastic Search Nest- Compare two fields


I'm struggling to compare two fields using Elastic Search Nest client for c#, can somebody help me to achieve this? For Example : I've two fields like FirstName & Name in my index, I want to compare these fields.


Solution

  • You can make this kind of comparison with a Script query

    var client = new ElasticClient();
    
    var response = client.Search<Question>(s => s
        .Query(q => q
            .Script(sq => sq
                .Source("doc['name'].value == doc['fullName'].value")
            )
        )
    );
    

    This retrieves the values from doc values and makes the assumption that both fields are indexed as keyword fields; doc values are not supported with text fields.