1

我有一些移动库存表,如下所示。

OSType   DeviceType  Make     year     Country    Value 
(attr1)   (attr2)   (attr3)   (attr4)  (attr5)   (result)
BADA        Galaxy   Samsung   2014     IN        100
Android     null     Samsung   2015     China     150
null        J7       Samsung   2018     IN        80
Android     Note10   Samsung   2019     null      500 
Android     Note10   Samsung   2019     null      500
IOS         I7       APPLE     null     USA       500 
IOS         null     null      2019     ALL       1000

我必须编写一个规则引擎类型的查询来找出以下组合的最佳匹配

第一个查询如果所有给定的参数都匹配

if the query input is  OStype=BADA,DeviceType=Galaxy, Make=Samsung, year=2014 Country=IN 

由于它是直接查询,所以我可以编写直接查询

第二个查询如果所有给定的参数也必须用空值检查

如果查询 OStype=Android,DeviceType=Note10, Make=Samsung, year=2019 Country=IN

那么我应该返回 500(因为根据我们的业务要求,如果表包含 null 或 ALL,我必须将该字段视为有效)

db.inventory.find( {
    $and : [
        { $or : [ { OStype : Android }, { OStype : null } ] },
        { $or : [ { DeviceType : Note10 }, { DeviceType : null } ] },
        { $or : [ { Make : Samsung }, { Make : null } ] },
        { $or : [ { year : 2019 }, { year : null } ] },
        { $or : [ { Country : IN }, { Country : null } ] }
    ]
} )

 This query ends up with returning more than one value because I have to consider **null** as per business requirement 

那么如何运行这个查询只匹配一个呢?

第三种查询是返回最佳匹配:

如果查询是 OStype=Android,DeviceType=J7, Make=Samsung, year=2019 Country=IN 在这种情况下年份不匹配,但我仍然必须以最佳匹配描述向用户返回值 80。

帮助如何处理空情况以及如何编写查询以找到最佳匹配将有很大帮助。

我尝试了流口水规则引擎,但它不符合我的要求..

4

0 回答 0