c# - How to compare decimal values of two properties of a document in an Elasticsearch query with Nest? -


i have documents typed product indexed in elasticsearch index.

these product documents have 2 decimal values: normalprice, discountprice.

i want search documents have normalprice > discountprice.

i tried construct query this:

q &= query<productmodel>.range(u => u.field(f => f.normalprice).greaterthan(u.field(f => f.discountprice))); 

first of i'm not sure if query correct if is, greaterthan function requires double values see.

what should do? there alternative way of doing comparison decimal values?

btw changing property types double not option. have use decimal.

elasticsearch supports long, integer, short, byte, double , float numeric data types, nest maps decimal types double default.

to perform comparison across document fields can achieved script query

client.search<productmodel>(s => s     .query(q => q         .script(sn => sn             .inline("doc['normalprice'].value > doc['discountprice'].value")         )     ) ); 

bear in mind script queries can expensive , potentially slower other queries depending on you're doing. if query need run lot, might consider storing comparison boolean field in document , setting property on productmodel type

public class productmodel {     public decimal normalprice { get; set:}     public decimal discountprice { get; set:}     public bool normalpricegreaterthandiscountprice      {          { return normalprice > discountprice; }      } } 

and querying on that.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -