Query and Search

Beyond basic retrieval by URI, MarkLogic provides extremely robust support for both database and 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 the endpoints and some sample code.

Query by Example

The Query by Example Endpoint provides a simple way to specify a query by providing an example document that should be returned. This allows you to scope queries to specific JSON keys and XML elements and attribute.

For exampler, say you want to find all documents representing drinks that are hot. You could do this by posting up a query like:

Beyond simple equality, you can express full-text search style matching, mathematical comparison, boolean combinations of these examples, and more.

String Query

The Search Endpoint provides 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, "mocha":

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. The API also provides a way for you to modify and extend that syntax.

Structured Query

The REST API provides a programmer-friendly way to specify a query as a set of hierarchical query constraints, expressed in XML or JSON. The structured search syntax 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.

For example, you can query properties and combine queries with boolean logic like the following:

These examples barely scratch the surface of the long list of controls and features available through the Search Endpoint and Search Configuration Endpoint.

CRUD APIs

Next Steps

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