Search

Beyond basic retrieval by URI, MarkLogic provides extremely robust support for search-style queries. Results can be sorted by relevance or by a scalar. Result items can be paged (viewing a small number at a time), returned documents can be snippeted (showing a short content blurb containing the matching terms) and highlighted (to perhaps bold the matching word occurrences).

When executing a search you can choose to retrieve a simple description of the matching documents, or fetch the documents as well, for the sake of efficiency to avoid repeated calls. If you fetch the documents as part of the search, you can request the same subsetting and transformation occur as for singular document retrievals.

Ok, that sounds cool. Let's move on to some sample code.

To execute any kind of query, call a QueryManager instance's search() method:

The results in the above case are stored in a SearchHandle, which lets you process the results as a POJO (Plain Old Java Object).

String Query

String queries provide a user-friendly way to specify a query as a specially marked-up string similar to those used by Google. This is something a developer could pass directly from a user interface text box to the REST API for execution.

To run a search that retrieves documents with the word, "delicious":

If you only want documents with this word that are in your drinks collection, you can do:

The string query syntax supports many but not all MarkLogic search features.

Structured Query

The Java API provides a programmer-friendly way to specify a query as a set of hierarchical query constraints, using the Builder pattern to construct search options and structured queries. The structured search vocabulary is highly expressive and supports: free-text search, text containment within a location, text equality to a location, strict value equality at a location, scalar-based range constraints, property-based search, collection-based search, directory-based search, and geospatial search -- or any boolean hierarchical mix of these.

The following structured query is built up programmatically, combining sub-queries with boolean logic:

These examples barely scratch the surface of the long list of controls and features available through the Java API.

CRUD APIs

Next Steps

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.