Skip to content

Instantly share code, notes, and snippets.

@evanlenz
Created August 7, 2012 22:25
Show Gist options
  • Save evanlenz/3290041 to your computer and use it in GitHub Desktop.
Save evanlenz/3290041 to your computer and use it in GitHub Desktop.
Code samples for MarkLogic for Java Developers
# properties to configure the examples
example.writer_user=rest-writer
example.writer_password=x
example.admin_user=rest-admin
example.admin_password=x
example.host=localhost
example.port=8011
example.authentication_type=digest
# properties to configure the examples
example.writer_user=rest-writer
example.writer_password=x
example.admin_user=rest-admin
example.admin_password=x
example.host=localhost
example.port=8011
example.authentication_type=digest
// create the client
DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType);
# properties to configure the examples
example.writer_user=rest-writer
example.writer_password=x
example.admin_user=rest-admin
example.admin_password=x
example.host=localhost
example.port=8011
example.authentication_type=digest
// release the client
client.release();
# properties to configure the examples
example.writer_user=rest-writer
example.writer_password=x
example.admin_user=rest-admin
example.admin_password=x
example.host=localhost
example.port=8011
example.authentication_type=digest
// release the client
client.release();
// create a manager for JSON documents
JSONDocumentManager docMgr = client.newJSONDocumentManager();
// create a handle on the content
InputStreamHandle handle = new InputStreamHandle(docStream);
// write the document content
docMgr.write("/example/flipper.json", handle);
// create a manager for XML documents
XMLDocumentManager docMgr = client.newXMLDocumentManager();
// create a handle on the content
InputStreamHandle handle = new InputStreamHandle(docStream);
// write the document content
docMgr.write("/example/flipper.xml", handle);
// expose unique combinations (co-occurrences) of size and exposure
QueryOptionsHandle options = new QueryOptionsHandle().withTuples(
qob.tuples("size-exposure",
qob.tupleSources(
qob.range(
qob.elementRangeIndex(new QName("http://marklogic.com/filter","size"),
qob.rangeType("xs:unsignedLong"))),
qob.range(
qob.elementRangeIndex(new QName("http://marklogic.com/filter","Exposure_Time"),
qob.stringRangeType(QueryOptions.DEFAULT_COLLATION))))));
// create a values definition
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("size-exposure", optionsName);
// retrieve the tuples
TuplesHandle tuplesHandle = queryMgr.tuples(valuesDef, new TuplesHandle());
// print out each size/exposure co-occurrence
for (Tuple tuple : tuplesHandle.getTuples()) {
System.out.println("Size: " + tuple.getValues()[0].get(Long.class)
+ "\nExposure: " + tuple.getValues()[1].get(String.class));
System.out.println();
}
QueryOptionsHandle options = new QueryOptionsHandle().withConstraints(
// expose the "contentRating" JSON key range index as "rating" values
qob.constraint("rating",
qob.range(
qob.jsonRangeIndex("contentRating",
qob.rangeType("xs:int")),
Facets.FACETED,
FragmentScope.DOCUMENTS,
qob.buckets(),
"descending")), // highest ratings first
// expose the "affiliation" JSON key range index as "company" values
qob.constraint("company",
qob.range(
qob.jsonRangeIndex("affiliation",
qob.stringRangeType(QueryOptions.DEFAULT_COLLATION)),
Facets.FACETED,
FragmentScope.DOCUMENTS,
qob.buckets(),
"frequency-order"))); // most common values first
String[] searches = {"", // empty search; return all results
"company:MarkLogic",
"company:MarkLogic rating:5",
"java rating GE 4"};
// run the search
queryMgr.search(query, resultsHandle);
// Show the resulting facets & their values
for (FacetResult facet : resultsHandle.getFacetResults()) {
System.out.println(facet.getName() + ":");
for (FacetValue value : facet.getFacetValues()) {
System.out.println(" " + value.getCount() + " occurrences of " + value.getName());
}
}
// run the search
queryMgr.search(query, resultsHandle);
// Show the resulting facets & their values
for (FacetResult facet : resultsHandle.getFacetResults()) {
System.out.println(facet.getName() + ":");
for (FacetValue value : facet.getFacetValues()) {
System.out.println(" " + value.getCount() + " occurrences of " + value.getName());
}
}
String[] searches = {"", // empty search; return all results
"company:MarkLogic",
"company:MarkLogic rating:5",
"java rating GE 4"};
// run the search
queryMgr.search(query, resultsHandle);
// Show the resulting facets & their values
for (FacetResult facet : resultsHandle.getFacetResults()) {
System.out.println(facet.getName() + ":");
for (FacetValue value : facet.getFacetValues()) {
System.out.println(" " + value.getCount() + " occurrences of " + value.getName());
}
}
// create a manager for text documents
TextDocumentManager docMgr = client.newTextDocumentManager();
// create a handle on the document's content
StringHandle content = new StringHandle("some text");
// create a handle for the document's associated metadata
DocumentMetadataHandle metadata = new DocumentMetadataHandle();
// add a collection tag
metadata.getCollections().addAll("myCollection");
// write the document content & metadata
docMgr.write("/example/foo.txt", metadata, content);
// create a manager for binary documents
BinaryDocumentManager docMgr = client.newBinaryDocumentManager();
// enable automatic metadata extraction into properties
docMgr.setMetadataExtraction(MetadataExtraction.PROPERTIES);
// create a handle on the document's content
InputStreamHandle content = new InputStreamHandle(pngStream);
// write the document content
docMgr.write("/example/mlfavicon.png", content);
// create a handle to receive the document content
StringHandle handle = new StringHandle();
// read the document content
docMgr.read("/example/flipper.json", handle);
// print out the document content
System.out.println(handle.get());
// create a handle to receive the document content
DOMHandle handle = new DOMHandle();
// read the document content
docMgr.read("/example/flipper.xml", handle);
// access the document content
Document document = handle.get();
String rootName = document.getDocumentElement().getTagName();
System.out.println("Read /example/flipper.xml content with the <"+rootName+"/> root element");
// create a handle to receive the document content
StringHandle content = new StringHandle();
// create a handle to receive the document metadata
DocumentMetadataHandle metadata = new DocumentMetadataHandle();
// read the document content
docMgr.read("/example/foo.txt", metadata, content);
// print the document content
System.out.println(content.get());
// iterate over the collections and print each one
for (String collection : metadata.getCollections()) {
System.out.println("Collection: " + collection);
}
// get a manager for binary documents
BinaryDocumentManager docMgr = client.newBinaryDocumentManager();
// create a handle to receive the document content
BytesHandle content = new BytesHandle();
// create a handle to receive the document metadata
DocumentMetadataHandle metadata = new DocumentMetadataHandle();
// read the document content & metadata
docMgr.read("/example/mlfavicon.png", metadata, content);
// get the document content as a byte array
byte[] contentBytes = content.get();
// print the image size to the console
System.out.println("Binary document size in bytes: " + contentBytes.length);
// iterate over the properties and print each one
for (Map.Entry<QName,Object> prop : metadata.getProperties().entrySet()) {
System.out.println(prop.getKey().getLocalPart() + ": " + prop.getValue());
}
// read just the document's metadata
docMgr.readMetadata("/example/mlfavicon.png", metadata);
// create a handle to receive the document metadata as XML
StringHandle metadata = new StringHandle();
// read just the document's metadata
docMgr.readMetadata("/example/mlfavicon.png", metadata);
// dump the metadata as raw XML
System.out.println(metadata);
// create a handle to receive the document metadata as JSON
StringHandle metadata = new StringHandle().withFormat(Format.JSON);
// create a generic manager for documents
GenericDocumentManager docMgr = client.newDocumentManager();
// delete the documents
docMgr.delete("/example/flipper.json");
docMgr.delete("/example/flipper.xml");
docMgr.delete("/example/foo.txt");
docMgr.delete("/example/mlfavicon.png");
// start the transaction
Transaction transaction = client.openTransaction();
// load each document in the same transaction
mgr.write(uri, metadata, doc, transaction);
// Commit the transaction
transaction.commit();
// create a manager for searching
QueryManager queryMgr = client.newQueryManager();
// run the search
queryMgr.search(query, resultsHandle);
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(queryMgr.newKeyLocator("affiliation"),"MarkLogic");
// create a handle for the search results
SearchHandle resultsHandle = new SearchHandle();
// run the search
queryMgr.search(query, resultsHandle);
// Retrieve and display the documents in this result set
TutorialUtil.displayJSONResultDocs(resultsHandle, client);
// create a search definition
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(queryMgr.newElementLocator(new QName("PERSONA")),"KING OF FRANCE");
// create a search definition
StringQueryDefinition query = queryMgr.newStringDefinition();
query.setCriteria("index OR Cassel NEAR Hare");
public static int PAGE_SIZE = 5;
// Set page size to 5 results
queryMgr.setPageLength(PAGE_SIZE);
// get the 3rd page of search results
int pageNum = 3;
int start = PAGE_SIZE * (pageNum - 1) + 1;
// get search results starting with the nth result
queryMgr.search(query, resultsHandle, start);
// get a structured query builder
StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder();
// Find all documents that have a property value containing the word "fish"
StructuredQueryDefinition query = qb.properties(qb.term("fish"));
// Restrict the search to a specific directory
query.setDirectory("/images/2012/02/14/");
// empty search defaults to returning all results
query.setCriteria("");
// Restrict the search to the "shakespeare" collection
query.setCollections("shakespeare");
// Search for the term "flower"
query.setCriteria("flower");
// create a handle for the search results to be received as raw XML
StringHandle resultsHandle = new StringHandle();
// run the search
queryMgr.search(query, resultsHandle);
// dump the XML results to the console
System.out.println(resultsHandle);
// create a handle for the search results to be received as raw JSON
StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON);
// Get the list of matching documents in this page of results
MatchDocumentSummary[] results = resultsHandle.getMatchResults();
// Iterate over the results
for (MatchDocumentSummary result: results) {
// get the list of match locations for this result
MatchLocation[] locations = result.getMatchLocations();
// iterate over the match locations
for (MatchLocation location: locations) {
// iterate over the snippets at a match location
for (MatchSnippet snippet : location.getSnippets()) {
// handle for list of named option sets
QueryOptionsListHandle listHandle = new QueryOptionsListHandle();
// get the list of named option sets
queryMgr.optionsList(listHandle);
// iterate over the option sets; print each's name & URI
for (Map.Entry<String,String> optionsSet : listHandle.getValuesMap().entrySet()) {
System.out.println(optionsSet.getKey() + ": " + optionsSet.getValue());
}
// create the client, connecting as the rest-admin user
DatabaseClient client = DatabaseClientFactory.newClient(
Config.host,
Config.port,
Config.admin_user,
Config.admin_password,
Config.authType);
// get an options manager
QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager();
// Create a builder for constructing query configurations.
QueryOptionsBuilder qob = new QueryOptionsBuilder();
// create the query options, defining a collection constraint
QueryOptionsHandle optsHandle = new QueryOptionsHandle().withConstraints(
qob.constraint("tag",
qob.collection("")));
qob.constraint("tag",
qob.collection(""))
// write the query options to the database
optionsMgr.writeOptions("tutorial", optsHandle);
// use the server's "tutorial" options set
query.setOptionsName("tutorial");
query.setCriteria("flower tag:shakespeare");
// run the search
SearchHandle resultsHandle = queryMgr.search(query, new SearchHandle());
// get the existing tutorial options
QueryOptionsHandle tutorialOpts =
optionsMgr.readOptions("tutorial", new QueryOptionsHandle());
// add a JSON value constraint
tutorialOpts.addConstraint(
qob.constraint("company",
qob.value(
qob.jsonTermIndex("affiliation"))));
// write the query options back to the server
optionsMgr.writeOptions("tutorial", tutorialOpts);
// create a search definition using the "tutorial" options
StringQueryDefinition query = queryMgr.newStringDefinition("tutorial");
// find talks with MarkLogic engineers
query.setCriteria("engineer company:marklogic");
tutorialOpts.addConstraint(
qob.constraint("person",
qob.value(
qob.elementTermIndex(new QName("PERSONA")))));
// find plays featuring the King of France
query.setCriteria("person:\"KING OF FRANCE\"");
tutorialOpts.addConstraint(
qob.constraint("bio",
qob.word(
qob.jsonTermIndex("bio"))));
// search for speakers whose bio mentions "strategy"
query.setCriteria("bio:strategy");
tutorialOpts.addConstraint(
qob.constraint("stagedir",
qob.word(
qob.elementTermIndex(new QName("STAGEDIR")))));
// search for stage directions involving swords
query.setCriteria("stagedir:sword");
tutorialOpts.addConstraint(
qob.constraint("spoken",
qob.elementQuery(new QName("SPEECH"))));
// search for mentions of swords in the script itself
query.setCriteria("spoken:sword");
tutorialOpts.addConstraint(
qob.constraint("image",
qob.properties()));
// find photos of fish
query.setCriteria("image:fish");
// create a query builder using the "tutorial" options
StructuredQueryBuilder qb = new StructuredQueryBuilder("tutorial");
// build a search definition
StructuredQueryDefinition query =
qb.or(
// find MarkLogic speakers whose bio mentions "product"
qb.and(
qb.wordConstraint("bio","product"),
qb.valueConstraint("company","MarkLogic")),
// find plays matching all three of these constraints
qb.and(
qb.elementConstraint("spoken", qb.term("fie")),
qb.wordConstraint("stagedir", "fall"),
qb.valueConstraint("person", "GRUMIO")),
// find photos of fish taken on February 27th
qb.and(
qb.properties(qb.term("fish")),
qb.directory(true, "/images/2012/02/27/")),
// find conference docs mentioning "fun"
qb.and(
qb.collection("mlw2012"),
qb.term("fun")));
// run the search
queryMgr.search(query, resultsHandle);
// create a builder for constructing query options
QueryOptionsBuilder qob = new QueryOptionsBuilder();
// expose the collection lexicon as "tag" values
QueryOptionsHandle options = new QueryOptionsHandle().withValues(
qob.values("tag",
qob.collection("")));
// write the query options to the database
optionsMgr.writeOptions(optionsName, options);
// create a values definition
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("tag", optionsName);
// retrieve the values
ValuesHandle valuesHandle = queryMgr.values(valuesDef, new ValuesHandle());
// print out the values and their frequencies
for (CountedDistinctValue value : valuesHandle.getValues()) {
System.out.println(
value.get("xs:string",String.class) + ": " + value.getCount());
}
// expose the URI lexicon as "uri" values
QueryOptionsHandle options = new QueryOptionsHandle().withValues(
qob.values("uri",
qob.uri()));
// expose the "affiliation" JSON key range index as "company" values
QueryOptionsHandle options = new QueryOptionsHandle().withValues(
qob.values("company",
qob.range(
qob.jsonRangeIndex("affiliation",
qob.stringRangeType(QueryOptions.DEFAULT_COLLATION))),
"frequency-order"));
// write the query options to the database
optionsMgr.writeOptions(optionsName, options);
// create a values definition
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("company", optionsName);
// retrieve the values
ValuesHandle valuesHandle = queryMgr.values(valuesDef, new ValuesHandle());
// expose the "SPEAKER" element range index as "speaker" values
QueryOptionsHandle options = new QueryOptionsHandle().withValues(
qob.values("speaker",
qob.range(
qob.elementRangeIndex(new QName("SPEAKER"),
qob.stringRangeType(QueryOptions.DEFAULT_COLLATION))),
"frequency-order"));
// expose the "contentRating" JSON key range index as "rating" values
QueryOptionsHandle options = new QueryOptionsHandle().withValues(
qob.values("rating",
qob.range(
qob.jsonRangeIndex("contentRating",
qob.rangeType("xs:int")))));
// create a values definition
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("rating", optionsName);
// also retrieve the averages of all ratings
valuesDef.setAggregate("avg","median");
// retrieve values in descending order
valuesDef.setDirection(Direction.DESCENDING);
QueryOptionsHandle options = new QueryOptionsHandle()
// expose the "contentRating" JSON key range index as "rating" values
.withValues(
qob.values("rating",
qob.range(
qob.jsonRangeIndex("contentRating",
qob.rangeType("xs:int")))))
// optionally constrain results by affiliation
.withConstraints(
qob.constraint("company",
qob.value(
qob.jsonTermIndex("affiliation")
)));
// create a values definition
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("rating", optionsName);
// create a search definition
StringQueryDefinition companyQuery = queryMgr.newStringDefinition("tutorial");
companyQuery.setCriteria("company:marklogic");
// return only those values from fragments (documents) matching this query
valuesDef.setQueryDefinition(companyQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment