簡體   English   中英

如何在帶有Nest的Elasticsearch查詢中比較文檔的兩個屬性的十進制值?

[英]How to compare decimal values of two properties of a document in an Elasticsearch query with Nest?

我有鍵入在Elasticsearch索引中索引的產品的文檔。

這些產品文檔具有2個十進制值: NormalPriceDiscountPrice

我想搜索具有NormalPrice > DiscountPrice文檔。

我試圖構造這樣的查詢:

q &= Query<ProductModel>.Range(u => u.Field(f => f.NormalPrice).GreaterThan(u.Field(f => f.DiscountPrice)));

首先,我不確定查詢是否正確,但是如我GreaterThanGreaterThan函數需要雙GreaterThan值。

我該怎么辦? 是否有其他方法可以與十進制值進行此比較?

BTW不能將屬性類型更改為double。 我必須使用十進制。

Elasticsearch支持longintegershortbytedoublefloat數值數據類型 ,因此NEST默認將decimal類型映射為double

通過script查詢可以實現跨文檔字段的比較

client.Search<ProductModel>(s => s
    .Query(q => q
        .Script(sn => sn
            .Inline("doc['normalPrice'].value > doc['discountPrice'].value")
        )
    )
);

請記住, script查詢可能比其他查詢昂貴,並且速度可能慢得多,具體取決於您在做什么。 如果這是一個需要大量運行的查詢,則可以考慮將比較存儲為文檔中的布爾值字段並將其設置為ProductModel類型的Property

public class ProductModel
{
    public decimal NormalPrice { get; set:}
    public decimal DiscountPrice { get; set:}
    public bool NormalPriceGreaterThanDiscountPrice 
    { 
        get { return NormalPrice > DiscountPrice; } 
    }
}

然后對此進行查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM