@Override public IndexedRecord getCurrent() throws NoSuchElementException { ++documentCounter; MatchDocumentSummary currentSummary = currentPage[pageCounter]; current = new GenericData.Record(settings.outputSchema); try { String docId = currentSummary.getUri(); current = docContentReader.readDocument(docId); result.totalCountLong++; result.successCountLong++; pageCounter++; return current; } catch (Exception e) { throw new MarkLogicException(new MarkLogicErrorCode("Can't read document from MarkLogic database"), e); } }
@Override public IndexedRecord getCurrent() throws NoSuchElementException { ++documentCounter; MatchDocumentSummary currentSummary = currentPage[pageCounter]; current = new GenericData.Record(settings.outputSchema); try { String docId = currentSummary.getUri(); current = docContentReader.readDocument(docId); result.totalCountLong++; result.successCountLong++; pageCounter++; return current; } catch (Exception e) { throw new MarkLogicException(new MarkLogicErrorCode("Can't read document from MarkLogic database"), e); } }
public List<String> getUrisInCollection(String collectionName, int pageLength) { QueryManager mgr = getClient().newQueryManager(); mgr.setPageLength(pageLength); StringQueryDefinition def = mgr.newStringDefinition(); def.setCollections(collectionName); SearchHandle h = mgr.search(def, new SearchHandle()); List<String> uris = new ArrayList<String>(); for (MatchDocumentSummary s : h.getMatchResults()) { uris.add(s.getUri()); } return uris; }
public String returnSearchResult(SearchHandle resultsHandle) { String matchedDoc = ""; // iterate over the result documents MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); for (MatchDocumentSummary docSummary : docSummaries) { String uri = docSummary.getUri(); // iterate over the match locations within a result document MatchLocation[] locations = docSummary.getMatchLocations(); matchedDoc = matchedDoc + "|" + "Matched " + locations.length + " locations in " + uri; } return matchedDoc; }
@Test public void testQueryByExampleXMLPayload() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPayload"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); assertEquals("Wrong Document Searched", result.getUri(), "/qbe/constraint1.xml"); } // release client client.release(); }
@Test public void testQueryByExampleXMLPermission() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPermission"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query try { File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe2.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); assertEquals("Wrong Document Searched", result.getUri(), "/qbe/constraint1.xml"); } } catch (Exception e) { System.out.println("Negative Test Passed of executing nonreadable file"); } // release client client.release(); }
static public void cleanUp() { DatabaseClient client = DatabaseClientFactory.newClient(Common.HOST, Common.PORT, new DigestAuthContext(Common.SERVER_ADMIN_USER, Common.SERVER_ADMIN_PASS)); try { QueryManager queryMgr = client.newQueryManager(); queryMgr.setPageLength(1000); QueryDefinition query = queryMgr.newStringDefinition(); query.setCollections(temporalCollection); // DeleteQueryDefinition deleteQuery = client.newQueryManager().newDeleteDefinition(); // deleteQuery.setCollections(temporalCollection); // client.newQueryManager().delete(deleteQuery); SearchHandle handle = queryMgr.search(query, new SearchHandle()); MatchDocumentSummary[] docs = handle.getMatchResults(); for ( MatchDocumentSummary doc : docs ) { if ( ! (temporalCollection + ".lsqt").equals(doc.getUri()) ) { client.newXMLDocumentManager().delete(doc.getUri()); } } } finally { client.release(); } } }
@Test public void testQueryByExampleWrongXML() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPayload"); String filename = "WrongFormat.xml"; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); try { // write docs writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); // assertEquals("Wrong Document Searched",result.getUri() , // "/qbe/constraint1.xml"); } } catch (FailedRequestException e) { System.out.println("Negative test passed as XML with invalid structure gave FailedRequestException "); } // release client client.release(); }
System.out.println(result.getPath() + ": Path"); System.out.println(result.getFormat() + ": Format"); System.out.println(result.getUri() + ": Uri"); assertTrue("Uri is Wrong", result.getPath().contains("/mime-type/constraint4.json") || result.getPath().contains("/mime-type/constraint4.xml"));
@Ignore public void testGeospatialRegionQuery() { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qdef; qdef = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/country/region")), GeospatialOperator.CONTAINS, qb.point(19.429297983081977, -99.140625)); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); MatchDocumentSummary[] summaries = results.getMatchResults(); for (MatchDocumentSummary summary : summaries) { assertEquals("mexico.xml", summary.getUri()); } }
@Test public void testStructuredSearch1() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); for (QueryDefinition t:new QueryDefinition[]{ qb.term("leaf3"), qb.build(qb.value(qb.element("leaf"), "leaf3")) }) { MatchDocumentSummary summary = queryMgr.findOne(t); assertNotNull(summary); GenericDocumentManager docMgr = Common.client.newDocumentManager(); assertNotNull("Document exists", docMgr.exists(summary.getUri())); } }
assertEquals("Size of the results summary", 10, mds.length); for (MatchDocumentSummary md : mds) { assertTrue("every uri should contain the class name", md.getUri().contains("Artifact"));
fail("unexpected search result:" + summary.getUri());
@Ignore public void testGeospatialRegionQueryConstraint() { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition qdef; String options = "<options xmlns=\"http://marklogic.com/appservices/search\">" + " <constraint name='geo'>" + " <geo-region-path coord='wgs84'>" + " <path-index>/country/region</path-index>" + " </geo-region-path>" + " </constraint>" + "</options>"; qdef = qb.geospatialRegionConstraint("geo", GeospatialOperator.CONTAINS, qb.point(19.429297983081977, -99.140625)); String combinedQuery = "<search xmlns=\"http://marklogic.com/appservices/search\">" + qdef.serialize() + options + "</search>"; RawCombinedQueryDefinition query = queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedQuery)); SearchHandle results = queryMgr.search(query, new SearchHandle()); MatchDocumentSummary[] summaries = results.getMatchResults(); assertEquals(1, summaries.length); for (MatchDocumentSummary summary : summaries) { assertEquals("mexico.xml", summary.getUri()); } }
fail("unexpected search result:" + summary.getUri());
fail("unexpected search result:" + summary.getUri());
fail("unexpected search result:" + summary.getUri());
@Test public void testQueryBatcher() { client.newDocumentManager().writeAs(collection + "/test1.json", meta, "[true]"); client.newDocumentManager().writeAs(collection + "/test1.xml", meta, "<xml/>"); client.newDocumentManager().writeAs(collection + "/test1.txt", meta, "text"); assertEquals(3, client.newQueryManager().search(collectionQuery, new SearchHandle()).getTotalResults()); StructuredQueryDefinition query = collectionQuery; // begin copy from "Using QueryBatcher" in src/main/java/com/marklogic/datamovement/package-info.java QueryBatcher qhb = dataMovementManager.newQueryBatcher(query) .withBatchSize(1000) .withThreadCount(20) .withConsistentSnapshot() .onUrisReady(batch -> { for ( String uri : batch.getItems() ) { if ( uri.endsWith(".txt") ) { client.newDocumentManager().delete(uri); } } }) .onQueryFailure(queryBatchException -> queryBatchException.printStackTrace()); JobTicket ticket = dataMovementManager.startJob(qhb); qhb.awaitCompletion(); dataMovementManager.stopJob(ticket); // end copy from "Using QueryBatcher" in src/main/java/com/marklogic/datamovement/package-info.java SearchHandle results = client.newQueryManager().search(collectionQuery, new SearchHandle()); assertEquals(2, results.getTotalResults()); for ( MatchDocumentSummary match : results.getMatchResults() ) { assertTrue(match.getUri().matches(".*/test1.(json|xml)")); } }
assertEquals(1, summaries.length); for (MatchDocumentSummary summary : summaries) { assertEquals("cuba.xml", summary.getUri());