Lay of the Land
OK, let's get going. Try this query:
Yep, that's the classic Hello World program written in XQuery. It's so simple because XQuery is a functional language and everything is an expression. The "hello world" text is a string, and that's a valid expression and thus a valid program. Now try this:
It's OK if you don't understand how it works yet. Go ahead and run it. You can't hurt anything here. This query returns the number of documents in the database. You should see a bit over 5 million. Let's try another query:
This returns the number of documents in the database that have a <message>
root element. It's a lower number than above because it doesn't include the
binary attachments. If you've seen XPath before this should look fairly
familiar.
The xdmp:estimate()
function is a very fast counter of things. It's named
"estimate" rather than "count" because it's not always 100% accurate (although
it is fully accurate for these simple queries). More on that later.
The xdmp
part of the function name is a namespace prefix. All functions
have a namespace, similar to a package name in Java. The xdmp
namespace
holds some of MarkLogic's extension functions. The default namespace prefix
is fn
and it can usually be omitted. Technically doc()
up there could be
written as fn:doc()
and you'd get the same result. In fact, you might want to
try that yourself right now. I'll wait.
The doc()
function without any arguments returns all documents in the database
(that your user is entitled to see). You can instead pass a document's unique
identifier as an argument and then the function returns that document. A
document identifier can be any string, so long as the string is a valid URI.
Here's a call to fetch a single document:
With this query you can see the raw XML source behind the email visible at http://markmail.org/message/ultlr54iuhh4nutn.
Getting Started
Looking at a Mail Message
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.