#gStore-weekly | gStore新版本0.9.1之Filter算术及逻辑运算
在gStore内核版本v0.9.1中,新增了Filter语句中对算术及逻辑运算的支持,我们来详细介绍下如何运用在SPARQL查询中。
1. 示例数据
为了更好的演示Filter语句中对算术及逻辑运算的支持功能,使用以下的数据作为示例数据:
<Alice><姓名>"Alice" .<Alice> <年龄>"24"^^<http://www.w3.org/2001/XMLSchema#integer> .<Alice> <资产分>"30.2"^^<http://www.w3.org/2001/XMLSchema#float> .<Alice> <行为分>"90.5"^^<http://www.w3.org/2001/XMLSchema#float> .<Alice> <身份分>"98.0"^^<http://www.w3.org/2001/XMLSchema#float> .<Alice> <申请次数>"0"^^<http://www.w3.org/2001/XMLSchema#integer> .<Bob> <姓名> "Bob" .<Bob> <年龄>"28"^^<http://www.w3.org/2001/XMLSchema#integer> .<Bob> <资产分>"81.8"^^<http://www.w3.org/2001/XMLSchema#float> .<Bob> <行为分> "60.5"^^<http://www.w3.org/2001/XMLSchema#float>.<Bob> <身份分>"68.0"^^<http://www.w3.org/2001/XMLSchema#float> .<Bob> <申请次数>"3"^^<http://www.w3.org/2001/XMLSchema#integer> .<Eve> <姓名> "Eve" .<Eve> <年龄>"20"^^<http://www.w3.org/2001/XMLSchema#integer> .<Eve> <资产分> "10.0"^^<http://www.w3.org/2001/XMLSchema#float>.<Eve> <行为分>"10.0"^^<http://www.w3.org/2001/XMLSchema#float> .<Eve> <身份分>"60.0"^^<http://www.w3.org/2001/XMLSchema#float> .<Eve> <申请次数>"5"^^<http://www.w3.org/2001/XMLSchema#integer> .<Francis> <姓名> "Francis" .<Francis> <年龄>"35"^^<http://www.w3.org/2001/XMLSchema#integer> .<Francis> <资产分>"94.6"^^<http://www.w3.org/2001/XMLSchema#float> .<Francis> <行为分>"10.0"^^<http://www.w3.org/2001/XMLSchema#float> .<Francis> <身份分> "75.6"^^<http://www.w3.org/2001/XMLSchema#float>.<Francis> <申请次数>"10"^^<http://www.w3.org/2001/XMLSchema#integer> .
2. 算术运算
算术运算即Filter语句中支持加、减、乘、除运算
示例:查询评估总分(资产分+行为分+身份分)小于100的用户姓名和年龄
SELECT?name ?ageWHERE{?person <姓名>?name .?person <年龄>?age .?person <资产分>?x .?person <行为分>?y .?person <身份分>?z .FILTER(?x+?y+?z <"100"^^<http://www.w3.org/2001/XMLSchema#float>)}
结果:根据示例数据,我们查询到的结果为Eve
{"results": {"bindings": [{"name": {"type": "literal","value": "Eve"},"age": {"type": "typed-literal","datatype": "http://www.w3.org/2001/XMLSchema#integer","value": "20"}}]}}
3. 逻辑运算
逻辑运算即Filter语句中支持与(&&)或(||)运算
示例:查询年龄小于30,且申请次数小于5次的用户姓名和年龄
SELECT ?name ?ageWHERE{?person <姓名>?name .?person <年龄>?age .?person <申请次数>?total .FILTER(?age <"30"^^<http://www.w3.org/2001/XMLSchema#integer> &&?total < "5"^^<http://www.w3.org/2001/XMLSchema#integer>)}
结果:根据示例数据,我们查询到的结果为Alice和Bob
{"results": {"bindings": [{"name": {"type":"literal","value": "Alice"},"age": {"type":"typed-literal","datatype":"http://www.w3.org/2001/XMLSchema#integer","value":"24"}},{"name": {"type":"literal","value": "Bob"},"age": {"type":"typed-literal","datatype":"http://www.w3.org/2001/XMLSchema#integer","value":"28"}}]}}
以上仅为简单的查询示例,在实际业务场景中会有更多复杂的运算条件,我们通过Filter的算术及逻辑运算,进一步丰富了组图模式的范围过滤方式。
相关文章