marklogic Very simple CRUD examples for XML documents Bonus: Simple Search example (Another way to read)


MarkLogic is first and foremost a search engine, so let's use two different methods to search for this document.

Using search:search()

This gives a peek into using search:search() to develop search applications. This library provides Google-like search results and will likely speed up your development of simple search tools. More information and a deeper dive can be found here.

xquery version "1.0-ml";
import module namespace search = ""
     at "/MarkLogic/appservices/search/search.xqy";
(: What is search without a keyword? :)
let $term := "very simple"
return search:search($term)

The result looks a bit confusing, but you can see that it returns one result, our example document.

Using cts:search()

More advanced search situations might call for more granular search capabilities. This is just to whet your appetite for what is available in search. More detailed information is found here.

xquery version "1.0-ml";
(: What is search without a keyword? :)
let $term := "very simple"
(: Complex queries can be made from individual cts queries. Here, we just have one simple query :)
let $query := cts:word-query($term,"case-insensitive")
(: Return the documents that match the query :)
return cts:search(fn:doc(),$query)

This is an incredibly simple example. BTW, if we want to get back the URI for the matching documents, instead of the documents themselves, we can change the last line of this snippet to-

  for $result in cts:search(fn:doc(),$query)
    return fn:base-uri($result)