Reputation: 135
Sorry, this might be a stupid question, but I wasn't able to find this in MarkLogic's docs:
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:
Updated:
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.
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);
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);
If yes - how can I use it with my further search queries (I'm using StructuredQueryBuilder)?
Upvotes: 1
Views: 909
Reputation: 7335
The Search API (and thus the REST API and Java API layers above it) can only sort on indexes over the returned fragments.
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.
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
Hoping that's useful,
Erik Hennum
Upvotes: 2
Reputation: 7842
You can set the sort order using QueryOptions.QuerySortOrder. The tutorial at https://developer.marklogic.com/learn/java/custom-search explains how to set query options. There's no example for QuerySortOrder
, but using it should be pretty straightforward.
Upvotes: 1