MarkLogic,Java API.获取排序的搜索结果 [英] MarkLogic, java API. Get search results sorted

查看:139
本文介绍了MarkLogic,Java API.获取排序的搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这可能是一个愚蠢的问题,但是我在MarkLogic的文档中找不到该问题:

Sorry, this might be a stupid question, but I wasn't able to find this in MarkLogic's docs:

我正在从某个收藏集中选择所有商品,并希望获得最新的商品.因此,我正在寻找某种功能来对搜索结果进行排序.我看到可以通过XQuery来实现,但是无法在Java API中找到它.

I'm selecting all items from some collection and would like to get the newest first. So I'm looking for some ability to get search results sorted. I saw it's possible via XQuery but wasn't able to find this in Java API.

总而言之,我的问题是:

So, to summarize, my questions are:

  1. 是否可以通过Java API获取按"lastModifyTime"排序的搜索结果(据我了解-这是一些自动创建/更新的字段)?
  2. 是否可以通过Java API获得按我的自定义属性排序的搜索结果?

已更新:

正如@mblakele在他的答案中提到的-QueryOptions.QuerySortOrder可用于此目的.我检查了他们的教程,对我来说,如何使用它并不是那么简单.

As @mblakele mentioned in his answer - there is QueryOptions.QuerySortOrder, which can be used for this purposes. I checked their tutorials and it's not so straightforward for me, how to use it.

我在数据库中为自己的属性"LAST_MODIFICATION_TIME"创建了范围索引,然后使用以下代码创建了QueryOptions:

I created range index in my DB for my own property "LAST_MODIFICATION_TIME", then I created QueryOptions using following code:

QueryOptionsBuilder qob = new QueryOptionsBuilder();
RangeSpec rangeSpec = qob.fieldRangeIndex("LAST_MODIFICATION_TIME", qob.rangeType("xs:dateTime"));
QueryOptions.QuerySortOrder querySortOrder = qob.sortOrder(rangeSpec, QueryOptions.QuerySortOrder.Direction.DESCENDING);

接下来我该怎么办?只需使用以下命令将其写入REST服务器即可:

And what should I do next? Just write it to REST server using:

QueryOptionsHandle optsHandle = new QueryOptionsHandle().withSortOrders(querySortOrder);
databaseClient.newServerConfigManager().newQueryOptionsManager().writeOptions("myConstraintName", optsHandle);

如果可以,如何将其与其他搜索查询一起使用(我正在使用StructuredQueryBuilder)?

If yes - how can I use it with my further search queries (I'm using StructuredQueryBuilder)?

推荐答案

Search API(以及其上方的REST API和Java API层)只能对返回的片段上的索引进行排序.

The Search API (and thus the REST API and Java API layers above it) can only sort on indexes over the returned fragments.

MarkLogic确实提供了最后修改的属性,该属性默认情况下处于禁用状态.您可以通过管理界面的数据库配置页面中的保持上次修改时间"复选框来启用它.但是,此值存储在属性片段中.因此,检索文档时无法对该属性进行排序.

MarkLogic does provide a last-modified property, which is off by default. You can enable it with the "maintain last modified" checkbox in the database configuration page of the Admin UI. However, this value is stored in the properties fragment. So, you can't sort on that property when retrieving documents.

但是,您可以在编写文档时将最后修改的元素添加到文档中.文档写转换提供了一种插入该元素的方法:

You can, however, add a last-modified element to the document when you write it. A document write transform provides one way to insert that element:

http://docs.marklogic.com/guide/java/transforms

然后您可以在管理界面中的元素上创建日期时间范围索引.

You can then create a datetime range index on element in the Admin UI.

要在搜索时使用范围索引,请首先创建指定排序顺序的查询选项. QueryOptions和QueryOptionsBuilder类已弃用,因此最好编写原始查询选项:

To use the range index when searching, first create query options that specify the sort order. The QueryOptions and QueryOptionsBuilder classes are deprecated, so it's best to write raw query options:

http://docs.marklogic.com/guide/java/query- options#id_20346

对于排序,您的查询选项将类似于以下内容

For sorting, your query options would resemble the following

<search:options xmlns:search="http://marklogic.com/appservices/search">
<search:sort-order type="xs:dateTime" direction="ascending">
  <search:element ns="" name="my-last-modified"/>
</search:sort-order>
</search:options>

顺便说一下,这里描述了完整的查询选项集:

By the way, the full set of query options are described here:

http://docs.marklogic.com/guide/rest-dev/appendixb#id_33716

希望有用

埃里克·亨纳姆(Erik Hennum)

Erik Hennum

这篇关于MarkLogic,Java API.获取排序的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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