![](/img/trans.png)
[英]How to query for a specific document by _id using the elasticsearch Nest client
[英]How to compare decimal values of two properties of a document in an Elasticsearch query with Nest?
我有鍵入在Elasticsearch索引中索引的產品的文檔。
這些產品文檔具有2個十進制值: NormalPrice
, DiscountPrice
。
我想搜索具有NormalPrice > DiscountPrice
文檔。
我試圖構造這樣的查詢:
q &= Query<ProductModel>.Range(u => u.Field(f => f.NormalPrice).GreaterThan(u.Field(f => f.DiscountPrice)));
首先,我不確定查詢是否正確,但是如我GreaterThan
, GreaterThan
函數需要雙GreaterThan
值。
我該怎么辦? 是否有其他方法可以與十進制值進行此比較?
BTW不能將屬性類型更改為double。 我必須使用十進制。
Elasticsearch支持long
, integer
, short
, byte
, double
和float
數值數據類型 ,因此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.