如何为SPARQL查询设置排序规则? [英] How do you set the Collation for a SPARQL query?

查看:425
本文介绍了如何为SPARQL查询设置排序规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用MarkLogic数据库的Java开发人员.我的代码的关键功能是它具有动态生成4-6个SPARQL查询并通过HTTP GET请求运行它们的能力.每个结果加在一起,然后返回.现在,我需要对这些结果进行一致的排序.

I am a Java developer working with a MarkLogic database. A key function of my code is its capacity to dynamically generate 4-6 SPARQL queries and run them via HTTP GET requests. The results of each are added together and then returned. I now need these results sorted consistently.

由于我正在分页每个查询的结果(使用LIMIT和OFFSET语句),所以每个查询都有自己的ORDER BY语句.如果不将排序嵌入查询中,则结果页面将不按顺序返回.

Since I am paging the results of each query (using the LIMIT and OFFSET statements) each query has its own ORDER BY statement. Without embedding sorting into the queries the pages of results will be returned out of order.

但是,每个查询返回其自己的结果,这些结果分别进行排序,并且需要合并到一个排序列表中.我希望使用字母数字排序,在考虑大小写之前先考虑字符,然后将空值和空值排序到末尾. (例如:"0123456789AaBbCc…WwXxYyZz"))

However, each query returns its own results which are individually sorted and need to be merged into a single sorted list. My preference would to be an alphanumeric sort that considers characters before considering case and that sorts empty and null values to the end. (Example: "0123456789AaBbCc…WwXxYyZz ")

我已经在Java代码中使用自定义的compare方法完成了此操作,但是最近遇到了一个问题:我的结果仍然没有返回排序.我遇到的问题是由于我的自定义订购方案与SPARQL所使用的订购方案完全分开的事实,导致得出的结果显然是未分类的.尽管我曾考虑过在返回结果之前从头开始对结果进行排序,而不是假设MarkLogic在返回已排序的结果,但这似乎不必要地浪费了,甚至可能无法解决我的问题.

I have already done this in my Java code using a custom compare method, but I recently ran into a problem: my results still aren’t returning sorted. The issue I’m having stems from the fact that my custom ordering scheme is completely separate from the one used by SPARQL, resulting in a decidedly unsorted set of results. While I have considered sorting the results from scratch before returning them instead of assuming MarkLogic is returning sorted results, this seems unnecessarily wasteful and it may not even fix my problem.

在我的研究中,我找不到任何方法可以为SPARQL设置排序规则,也没有找到编写自定义排序规则的方法.本页上的文档( https://www.w3.org/TR/rdf-sparql-query/#modOrderBy )特别指出,SPARQL的ORDER BY基于XPATH的fn:compare驱动的比较方法.该函数引用了此页面( https://www.w3.org/TR/xpath-函数/#collat​​ions ),其中特别提到了用于指定归类以及使用Unicode归类算法的替代实现的选项.我找不到任何详细说明如何实际执行此操作的内容.

In my research I have not been able to find any way to set the Collation for SPARQL, nor have I found a way to write a custom Collation. The documentation on this page (https://www.w3.org/TR/rdf-sparql-query/#modOrderBy) specifically states that SPARQL’s ORDER BY is based on a comparison method driven by XPATH’s fn:compare. That function references this page (https://www.w3.org/TR/xpath-functions/#collations) which specifically mentions options for specifying the Collation as well as using alternative implementations of the of the Unicode Collation Algorithm. What I can’t find is anything detailing how to actually do this.

简而言之,我有什么方法可以操纵或控制SPARQL查询如何比较字符以影响最终顺序?

In short, is there any way for me to manipulate or control how a SPARQL query compares characters to affect the final order?

推荐答案

如果我了解您的要求,则想使用ORDER BY,OFFSET和LIMIT选择哪个结果重新显示,然后您希望另一个ORDER BY确定显示结果的顺序(可能与选择结果的顺序不同).您可以使用嵌套查询来做到这一点:

If I understand what you're asking, you want to use ORDER BY, OFFSET, and LIMIT to select which results you're going to show, and then you want another ORDER BY to determine the order in which you'll show those results (which might be different than the order that you used to select them). You can do that with a nested query:

select ?result {
  { select ?result where {
      #-- ...
    }
    order by #-- ...
    offset #-- ...
    limit #-- ...
  }
}
order by #-- ...

custom 排序的支持不多,但是您可以在订单表达式中使用函数,并且可以提供多个表达式,以按一件事先排序,然后再按另一件事排序.在您的情况下,您可能想要执行 order lcase(?value)之类的命令来区分大小写. (当然,这并不完美.例如,我不清楚您是否要对数字前缀进行数字排序(例如,顺序应为1、10、2或1、2、10). )

There's not a whole lot of support for custom orderings, but you can use functions in the order expressions, and you can provide multiple expressions to sort first by one thing, then by another. In your case, it looks like you might want to do something like order lcase(?value) to order case-insensitively. (That won't be perfect, of course. For instances, it's not clear to me whether you want numeric sort for numeric prefixes or not (e.g., should the order be 1, 10, 2, or 1, 2, 10).)

这篇关于如何为SPARQL查询设置排序规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆