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; }
@Test public void testCircleContainsCircle() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testCircleContainsCircle"); QueryManager queryMgr = client.newQueryManager(); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/circle"), CoordinateSystem.WGS84DOUBLE), GeospatialOperator.CONTAINS, qb.circle(qb.point(0, -66.09375), 6.897)); // create handle JacksonHandle resultsHandle = new JacksonHandle(); queryMgr.search(t, resultsHandle); // get the result JsonNode resultNode = resultsHandle.get(); JsonNode jsonPointNodes = resultNode.path("results"); // Should have 2 nodes returned. assertEquals("Two nodes not returned from testCircleContainsCircle method ", 2, resultNode.path("total").asInt()); assertTrue("URI returned from testCircleContainsCircle method is incorrect", jsonPointNodes.get(0).path("uri").asText().contains("/Equator-json.json") || jsonPointNodes.get(1).path("uri").asText().contains("/Equator-json.json")); assertTrue("URI returned from testCircleContainsCircle method is incorrect", jsonPointNodes.get(0).path("uri").asText().contains("/Equator.xml") || jsonPointNodes.get(1).path("uri").asText().contains("/Equator.xml")); }
@Test public void testSearchLog() { QueryManager qMgr = Common.client.newQueryManager(); ByteArrayOutputStream out = null; RequestLogger logger = null; String outString = null; out = new ByteArrayOutputStream(); logger = Common.client.newLogger(out); logger.setContentMax(RequestLogger.ALL_CONTENT); qMgr.startLogging(logger); QueryDefinition querydef = qMgr.newStringDefinition(); qMgr.search(querydef, new SearchHandle()); outString = new String(out.toByteArray()); assertTrue("Search failed to log output", outString != null && outString.length() > 0); out = new ByteArrayOutputStream(); logger = Common.client.newLogger(out); logger.setContentMax(RequestLogger.ALL_CONTENT); qMgr.startLogging(logger); DeleteQueryDefinition deleteDef = qMgr.newDeleteDefinition(); deleteDef.setCollections("x"); qMgr.delete(deleteDef); outString = new String(out.toByteArray()); assertTrue("SearchDelete failed to log output", outString != null && outString.length() > 0); }
@Override public void deleteAll(Transaction transaction) { QueryManager queryMgr = client.newQueryManager(); DeleteQueryDefinition deleteQuery = queryMgr.newDeleteDefinition(); deleteQuery.setCollections(entityClass.getName()); queryMgr.delete(deleteQuery, transaction); } /* REST API does not currently support DELETE /search with multiple collection arguments
@Override public List<String> getJobNames() { List<String> jobNames = new ArrayList<>(); QueryManager queryMgr = databaseClient.newQueryManager(); ValuesDefinition valuesDef = queryMgr.newValuesDefinition("jobName", SEARCH_OPTIONS_NAME); ValuesHandle results = queryMgr.values(valuesDef, new ValuesHandle()); for (CountedDistinctValue value : results.getValues()) { jobNames.add(value.get("xs:string", String.class)); } return jobNames; }
public long getCollectionSize(String collectionName) { QueryManager queryMgr = getClient().newQueryManager(); StringQueryDefinition def = queryMgr.newStringDefinition(); def.setCollections(collectionName); SearchHandle sh = queryMgr.search(def, new SearchHandle()); return sh.getTotalResults(); }
queryMgr.setPageLength(50); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/box"), CoordinateSystem.WGS84DOUBLE), GeospatialOperator.DISJOINT, queryMgr.search(t, resultsHandle);
ValuesDefinition vdef = queryMgr.newValuesDefinition("double", optionsName); switch (i) { case 0: StringQueryDefinition stringQuery = queryMgr.newStringDefinition(); stringQuery.setCriteria("10"); vQuery = stringQuery; break; case 1: StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(null); StructuredQueryDefinition t = qb.term("10"); vQuery = t; break; case 2: RawCtsQueryDefinition query = queryMgr.newRawCtsQueryDefinition(null); query.setCriteria("10"); vQuery = query; String ctsQuery = "<word-query xmlns=\"http://marklogic.com/cts\"><text>10</text></word-query>"; StringHandle handle = new StringHandle(ctsQuery).withFormat(Format.XML); RawCtsQueryDefinition rawCtsQuery = queryMgr.newRawCtsQueryDefinition(handle); vQuery = rawCtsQuery; break; ValuesHandle v = queryMgr.values(vdef, new ValuesHandle()); CountedDistinctValue dv[] = v.getValues(); assertNotNull("There should be values", dv);
StringQueryDefinition stringQuery = queryMgr.newStringDefinition(); stringQuery.setCriteria("grandchild1 OR grandchild4"); stringQuery.setResponseTransform(transform); result = queryMgr.search(stringQuery, new DOMHandle()).get(); value = result.getDocumentElement().getAttributeNS(TEST_NS, "transformed"); assertEquals("String query read transform failed","true",value); queryMgr.newValuesDefinition("double", optionsName); stringQuery = queryMgr.newStringDefinition(); stringQuery.setCriteria("10"); stringQuery.setResponseTransform(transform); result = queryMgr.values(vdef, new DOMHandle()).get(); value = result.getDocumentElement().getAttributeNS(TEST_NS, "transformed"); assertEquals("Values query read transform failed",value,"true");
private void check(StructureWriteHandle handle, String optionsName) { RawCombinedQueryDefinition rawCombinedQueryDefinition; if (optionsName == null) { rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(handle); } else { rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(handle, optionsName); } // StringHandle stringResults = null; // stringResults = queryMgr.search(rawCombinedQueryDefinition, // new StringHandle()); // System.out.println(stringResults.get()); SearchHandle results; results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); checkResults(results); }
@Test public void testFailedSearch() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); queryMgr.setView(QueryView.RESULTS); StringQueryDefinition qdef = queryMgr.newStringDefinition(); qdef.setCriteria("criteriaThatShouldNotMatchAnyDocument"); qdef.setDirectory("/sample/"); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
@Test public void testFailedSearch() throws IOException { StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.term("criteriaThatShouldNotMatchAnyDocument"); RawCombinedQueryDefinition queryDef = queryMgr.newRawCombinedQueryDefinition( new StringHandle(head + t.serialize() + tail) .withMimetype("application/xml") ); SearchHandle results = queryMgr.search(queryDef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); StringHandle criteria = new StringHandle().withFormat(Format.XML); criteria.set("<q:query xmlns:q='" + RawQueryByExampleDefinition.QBE_NS + "'>" + "<q:word>criteriaThatShouldNotMatchAnyDocument</q:word>" + "</q:query>"); RawQueryByExampleDefinition qbe = queryMgr.newRawQueryByExampleDefinition(criteria); results = queryMgr.search(qbe, new SearchHandle()); assertNotNull(results); summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
private void check(StructureWriteHandle handle, String optionsName) { QueryManager queryMgr = Common.client.newQueryManager(); queryMgr.setPageLength(0); RawCombinedQueryDefinition query = queryMgr.newRawCombinedQueryDefinition(handle); SearchHandle response = queryMgr.search(query, new SearchHandle()); String wrappedCtsQuery = response.getQuery(new StringHandle()).get(); // remove the <search:query> wrapper element String ctsQuery = wrappedCtsQuery.replaceAll("</?search:query[^>]*>", ""); long numMatches = response.getTotalResults(); checkCts(new StringHandle(ctsQuery), optionsName, numMatches); }
@Test public void testRawCtsQueryFromFileHandle() throws Exception { Common.client.newDocumentManager().write("testRawCtsQueryFromFileHandle.xml", new FileHandle(new File("src/test/resources/constraint5.xml")).withFormat(Format.XML)); String ctsQueryFilePath = "ctsQuery1.xml"; String ctsQueryAsString = Common.testFileToString(ctsQueryFilePath); File ctsQueryFile = new File(Common.getResourceUri(ctsQueryFilePath)); FileHandle ctsHandle = new FileHandle(ctsQueryFile); QueryDefinition[] queries = new QueryDefinition[] { queryMgr.newRawCombinedQueryDefinition(ctsHandle), queryMgr.newRawCtsQueryDefinition(ctsHandle), queryMgr.newRawCombinedQueryDefinitionAs(Format.XML, ctsQueryFile), queryMgr.newRawCtsQueryDefinitionAs(Format.XML, ctsQueryFile), queryMgr.newRawCtsQueryDefinition(new InputStreamHandle(new FileInputStream(ctsQueryFile))), queryMgr.newRawCtsQueryDefinition(new StringHandle(ctsQueryAsString)), queryMgr.newRawCtsQueryDefinition(new DOMHandle(Common.testStringToDocument(ctsQueryAsString))) }; for ( QueryDefinition query : queries ) { // create result handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(query, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id' or local-name()='highlight'])", resultDoc); } }
@Test public void testStringQuery() throws Exception { StringQueryDefinition query = client.newQueryManager().newStringDefinition().withCriteria("John AND dept:HR"); query.setCollections(qhbTestCollection); query.setOptionsName("employees"); Map<String, String[]> matchesByForest = new HashMap<>(); matchesByForest.put("java-unittest-1", new String[] {uri1, uri3, uri4}); matchesByForest.put("java-unittest-2", new String[] {}); matchesByForest.put("java-unittest-3", new String[] {}); runQueryBatcher(moveMgr.newQueryBatcher(query), query, matchesByForest, 99, 17); }
@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()); } }
private void readNextPage() { queryManager.search(stringQueryDefinition, searchHandle, documentCounter); currentPage = searchHandle.getMatchResults(); }
@Override public List<JobInstance> findJobInstancesByName(String jobName, int start, int count) { List<JobInstance> jobInstances = new ArrayList<>(); QueryManager queryMgr = databaseClient.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(SEARCH_OPTIONS_NAME); StructuredQueryDefinition querydef = qb.and( qb.valueConstraint("jobName", jobName), qb.collection(COLLECTION_JOB_INSTANCE) ); queryMgr.setPageLength((long) count); SearchHandle results = queryMgr.search(querydef, new SearchHandle(), start); MatchDocumentSummary[] summaries = results.getMatchResults(); for (MatchDocumentSummary summary : summaries ) { JAXBHandle<MarkLogicJobInstance> jaxbHandle = new JAXBHandle<>(jaxbContext()); summary.getFirstSnippet(jaxbHandle); MarkLogicJobInstance mji = jaxbHandle.get(); jobInstances.add(mji.getJobInstance()); } return jobInstances; }
@Test public void testValuesPaging() throws IOException, ParserConfigurationException, SAXException { String optionsName = makeValuesOptions(); QueryManager queryMgr = Common.client.newQueryManager(); queryMgr.setPageLength(2); ValuesDefinition vdef = queryMgr.newValuesDefinition("double", optionsName); ValuesHandle v = queryMgr.values(vdef, new ValuesHandle(), 2); CountedDistinctValue dv[] = v.getValues(); assertNotNull("There should be values", dv); assertEquals("There should be 2 values", 2, dv.length); assertEquals("The first value should be '1.2'", dv[0].get("xs:double", Double.class).toString(), "1.2"); assertEquals("The second value should be '2.2'", dv[1].get("xs:double", Double.class).toString(), "2.2"); Common.adminClient.newServerConfigManager().newQueryOptionsManager().deleteOptions(optionsName); }
ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity", "aggregatesOpt.xml"); queryDef.setAggregate("correlation", "covariance"); queryDef.setName("pop-rate-tups"); queryMgr.tuples(queryDef, tuplesHandle);