Reputation: 407
I have a MarkLogic database containing news articles in NITF format, each of which has the following information.
<date.issue norm="20140321T000000" news:date="2014-03-21" news:time="00:00:00" news:origin="Generated"/>
I am using the inbuilt REST API to access this database. I would like to constrain results by date in my query, i.e get all documents between a date range, or before or after a particular date. How is this possible with MarkLogic's REST API?
Upvotes: 0
Views: 183
Reputation: 8422
There are three steps:
For #1, you'll need an xs:date range index on news:date. Range indexes allow >, ≥, =, ≤, < comparisons.
For #2, you'll need to build a constraint using the news:date index. It will look something like this:
<constraint name="news-date">
<range type="xs:date">
<element ns="news" name="date"/>
</range>
</constraint>
Note that the value of element/@ns needs to be the full namespace URI, not just the prefix. This constraint configuration will be part of your REST API configuration. (You also have the option to use QBE instead.)
For #3, the query will be built client side and sent to the REST API on the server. The query can be expressed as either a query string or as a structured query. As a query string, you could do a between like this:
news-date GT 2010-01-01 AND news-date LT 2010-12-31
For this to work, make sure your REST API configuration includes LT, GT, and AND, which are shown in the default query string grammar.
A structured query is represented as JSON or XML. Here's the same query as JSON:
{
"query": {
"queries": [
{
"and-query": {
"queries": [
"range-constraint-query": {
"value": [ "2010-01-01" ],
"constraint-name": "news-date",
"range-operator": ">"
},
"range-constraint-query": {
"value": [ "2010-12-31" ],
"constraint-name": "news-date",
"range-operator": "<"
}
]
}
}
]
}
}
Upvotes: 3