Created
August 7, 2012 22:25
-
-
Save evanlenz/3290041 to your computer and use it in GitHub Desktop.
Code samples for MarkLogic for Java Developers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the client | |
DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// release the client | |
client.release(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// release the client | |
client.release(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a manager for JSON documents | |
JSONDocumentManager docMgr = client.newJSONDocumentManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle on the content | |
InputStreamHandle handle = new InputStreamHandle(docStream); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the document content | |
docMgr.write("/example/flipper.json", handle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a manager for XML documents | |
XMLDocumentManager docMgr = client.newXMLDocumentManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle on the content | |
InputStreamHandle handle = new InputStreamHandle(docStream); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the document content | |
docMgr.write("/example/flipper.xml", handle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)))))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a values definition | |
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("size-exposure", optionsName); | |
// retrieve the tuples | |
TuplesHandle tuplesHandle = queryMgr.tuples(valuesDef, new TuplesHandle()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String[] searches = {"", // empty search; return all results | |
"company:MarkLogic", | |
"company:MarkLogic rating:5", | |
"java rating GE 4"}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String[] searches = {"", // empty search; return all results | |
"company:MarkLogic", | |
"company:MarkLogic rating:5", | |
"java rating GE 4"}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a manager for text documents | |
TextDocumentManager docMgr = client.newTextDocumentManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle on the document's content | |
StringHandle content = new StringHandle("some text"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle for the document's associated metadata | |
DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add a collection tag | |
metadata.getCollections().addAll("myCollection"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the document content & metadata | |
docMgr.write("/example/foo.txt", metadata, content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a manager for binary documents | |
BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); | |
// enable automatic metadata extraction into properties | |
docMgr.setMetadataExtraction(MetadataExtraction.PROPERTIES); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle on the document's content | |
InputStreamHandle content = new InputStreamHandle(pngStream); | |
// write the document content | |
docMgr.write("/example/mlfavicon.png", content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle to receive the document content | |
StringHandle handle = new StringHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read the document content | |
docMgr.read("/example/flipper.json", handle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// print out the document content | |
System.out.println(handle.get()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle to receive the document content | |
DOMHandle handle = new DOMHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read the document content | |
docMgr.read("/example/flipper.xml", handle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle to receive the document content | |
StringHandle content = new StringHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle to receive the document metadata | |
DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read the document content | |
docMgr.read("/example/foo.txt", metadata, content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read the document content & metadata | |
docMgr.read("/example/mlfavicon.png", metadata, content); | |
// get the document content as a byte array | |
byte[] contentBytes = content.get(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read just the document's metadata | |
docMgr.readMetadata("/example/mlfavicon.png", metadata); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle to receive the document metadata as JSON | |
StringHandle metadata = new StringHandle().withFormat(Format.JSON); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start the transaction | |
Transaction transaction = client.openTransaction(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load each document in the same transaction | |
mgr.write(uri, metadata, doc, transaction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Commit the transaction | |
transaction.commit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a manager for searching | |
QueryManager queryMgr = client.newQueryManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run the search | |
queryMgr.search(query, resultsHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query.put(queryMgr.newKeyLocator("affiliation"),"MarkLogic"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle for the search results | |
SearchHandle resultsHandle = new SearchHandle(); | |
// run the search | |
queryMgr.search(query, resultsHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrieve and display the documents in this result set | |
TutorialUtil.displayJSONResultDocs(resultsHandle, client); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a search definition | |
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition(); | |
query.put(queryMgr.newElementLocator(new QName("PERSONA")),"KING OF FRANCE"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a search definition | |
StringQueryDefinition query = queryMgr.newStringDefinition(); | |
query.setCriteria("index OR Cassel NEAR Hare"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int PAGE_SIZE = 5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set page size to 5 results | |
queryMgr.setPageLength(PAGE_SIZE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Restrict the search to a specific directory | |
query.setDirectory("/images/2012/02/14/"); | |
// empty search defaults to returning all results | |
query.setCriteria(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Restrict the search to the "shakespeare" collection | |
query.setCollections("shakespeare"); | |
// Search for the term "flower" | |
query.setCriteria("flower"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a handle for the search results to be received as raw JSON | |
StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the list of matching documents in this page of results | |
MatchDocumentSummary[] results = resultsHandle.getMatchResults(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Iterate over the results | |
for (MatchDocumentSummary result: results) { | |
// get the list of match locations for this result | |
MatchLocation[] locations = result.getMatchLocations(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// iterate over the match locations | |
for (MatchLocation location: locations) { | |
// iterate over the snippets at a match location | |
for (MatchSnippet snippet : location.getSnippets()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// handle for list of named option sets | |
QueryOptionsListHandle listHandle = new QueryOptionsListHandle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get the list of named option sets | |
queryMgr.optionsList(listHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get an options manager | |
QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a builder for constructing query configurations. | |
QueryOptionsBuilder qob = new QueryOptionsBuilder(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the query options, defining a collection constraint | |
QueryOptionsHandle optsHandle = new QueryOptionsHandle().withConstraints( | |
qob.constraint("tag", | |
qob.collection(""))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qob.constraint("tag", | |
qob.collection("")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the query options to the database | |
optionsMgr.writeOptions("tutorial", optsHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use the server's "tutorial" options set | |
query.setOptionsName("tutorial"); | |
query.setCriteria("flower tag:shakespeare"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run the search | |
SearchHandle resultsHandle = queryMgr.search(query, new SearchHandle()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get the existing tutorial options | |
QueryOptionsHandle tutorialOpts = | |
optionsMgr.readOptions("tutorial", new QueryOptionsHandle()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add a JSON value constraint | |
tutorialOpts.addConstraint( | |
qob.constraint("company", | |
qob.value( | |
qob.jsonTermIndex("affiliation")))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the query options back to the server | |
optionsMgr.writeOptions("tutorial", tutorialOpts); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a search definition using the "tutorial" options | |
StringQueryDefinition query = queryMgr.newStringDefinition("tutorial"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find talks with MarkLogic engineers | |
query.setCriteria("engineer company:marklogic"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tutorialOpts.addConstraint( | |
qob.constraint("person", | |
qob.value( | |
qob.elementTermIndex(new QName("PERSONA"))))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find plays featuring the King of France | |
query.setCriteria("person:\"KING OF FRANCE\""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tutorialOpts.addConstraint( | |
qob.constraint("bio", | |
qob.word( | |
qob.jsonTermIndex("bio")))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// search for speakers whose bio mentions "strategy" | |
query.setCriteria("bio:strategy"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tutorialOpts.addConstraint( | |
qob.constraint("stagedir", | |
qob.word( | |
qob.elementTermIndex(new QName("STAGEDIR"))))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// search for stage directions involving swords | |
query.setCriteria("stagedir:sword"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tutorialOpts.addConstraint( | |
qob.constraint("spoken", | |
qob.elementQuery(new QName("SPEECH")))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// search for mentions of swords in the script itself | |
query.setCriteria("spoken:sword"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tutorialOpts.addConstraint( | |
qob.constraint("image", | |
qob.properties())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find photos of fish | |
query.setCriteria("image:fish"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a query builder using the "tutorial" options | |
StructuredQueryBuilder qb = new StructuredQueryBuilder("tutorial"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run the search | |
queryMgr.search(query, resultsHandle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(""))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write the query options to the database | |
optionsMgr.writeOptions(optionsName, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a values definition | |
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("tag", optionsName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// retrieve the values | |
ValuesHandle valuesHandle = queryMgr.values(valuesDef, new ValuesHandle()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// print out the values and their frequencies | |
for (CountedDistinctValue value : valuesHandle.getValues()) { | |
System.out.println( | |
value.get("xs:string",String.class) + ": " + value.getCount()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// expose the URI lexicon as "uri" values | |
QueryOptionsHandle options = new QueryOptionsHandle().withValues( | |
qob.values("uri", | |
qob.uri())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a values definition | |
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("company", optionsName); | |
// retrieve the values | |
ValuesHandle valuesHandle = queryMgr.values(valuesDef, new ValuesHandle()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"))))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a values definition | |
ValuesDefinition valuesDef = queryMgr.newValuesDefinition("rating", optionsName); | |
// also retrieve the averages of all ratings | |
valuesDef.setAggregate("avg","median"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// retrieve values in descending order | |
valuesDef.setDirection(Direction.DESCENDING); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment