@Override public List<JobInstance> getJobInstances(String jobName, int start, int count) { QueryManager queryMgr = databaseClient.newQueryManager(); StringQueryDefinition querydef = queryMgr.newStringDefinition(SEARCH_OPTIONS_NAME); querydef.setCriteria("jobName: " + jobName + " AND sort:date"); logger.info(querydef.getCriteria()); SearchHandle results = queryMgr.search(querydef, new SearchHandle()); List<JobInstance> jobInstances = new ArrayList<>(); MatchDocumentSummary[] summaries = results.getMatchResults(); MarkLogicJobInstance mji; if (start+count > summaries.length) { return jobInstances; } for (int i = start; i < start+count; i++) { JAXBHandle<MarkLogicJobInstance> jaxbHandle = new JAXBHandle<>(jaxbContext()); summaries[i].getFirstSnippet(jaxbHandle); mji = jaxbHandle.get(); jobInstances.add(mji.getJobInstance()); } return jobInstances; }
public SearchHandle runSearch(DatabaseClient client, String queryOptionName, String criteria) { // create a manager for searching QueryManager queryMgr = client.newQueryManager(); // create a search definition StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria(criteria); // create a handle for the search results SearchHandle resultsHandle = new SearchHandle(); // run the search return queryMgr.search(querydef, resultsHandle); }
@Test public void testFacetSearch() throws IOException, ParserConfigurationException, SAXException, FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(options))); mgr = Common.adminClient.newServerConfigManager().newQueryOptionsManager(); mgr.writeOptions("photos", new DOMHandle(document)); QueryManager queryMgr = Common.client.newQueryManager(); StringQueryDefinition qdef = queryMgr.newStringDefinition("photos"); qdef.setCriteria("Grand"); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); FacetResult[] facets = results.getFacetResults(); assertNotNull(facets); }
@Test public void testBug19016() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException { System.out.println("Running testBug19016"); String[] filenames = { "bug19016.xml" }; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/bug19016/", "XML"); } QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(); querydef.setCriteria("this\"is\"an%33odd string"); String result = queryMgr.search(querydef, new StringHandle()).get(); System.out.println(result); assertTrue("qtext is not correct", result.contains("<search:qtext>this\"is\"an%33odd string</search:qtext>")); // release client client.release(); }
@Test public void testPOJORepoSearchWithUTF8NoTransaction() { PojoRepository<SpecialArtifact, String> pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); SpecialArtifact searchSpArtifact = null; // Load the object into database long longId = getOneNegativeLongId(); String artifactName = new String("万里长城" + longId); pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName), "odd", "numbers"); assertTrue(pojoReposProductsString.exists(artifactName)); // Validate the artifact search. StringQueryDefinition query = client.newQueryManager().newStringDefinition(); query.setCriteria(artifactName); PojoPage<SpecialArtifact> pojoSpecialArtifactPage = pojoReposProductsString.search(query, 1); Iterator<SpecialArtifact> iter = pojoSpecialArtifactPage.iterator(); while (iter.hasNext()) { searchSpArtifact = iter.next(); validateSpecialArtifactWithSpecialCharacter(searchSpArtifact, artifactName, longId); } }
@Test public void testBulkSearchSQDwithNoResults() throws KeyManagementException, NoSuchAlgorithmException, Exception { loadTxtDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("zzzz"); SearchHandle results = new SearchHandle(); DocumentPage page = docMgr.search(qd, 1, results); assertFalse("Should return no results", page.hasNext()); }
@Test public void testJSON() throws FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { String optionsName = writeOptions(); QueryManager queryMgr = Common.client.newQueryManager(); StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName); qdef.setCriteria("grandchild1 OR grandchild4"); qdef.setDirectory("/sample/"); queryMgr.setView(QueryView.FACETS); // create a handle for the search results StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON); // run the search queryMgr.search(qdef, resultsHandle); assertEquals("{", resultsHandle.get().substring(0, 1)); // It's JSON, right? }
@Test public void testMatchGetQuery() { StringQueryDefinition qtext = queryManager .newStringDefinition("alerts"); qtext.setCriteria("favorited:true"); RuleDefinitionList answer = ruleManager.match(qtext, new RuleDefinitionList()); assertEquals("One answer", 1, answer.size()); qtext.setCriteria("favorited:false"); answer = ruleManager.match(qtext, 1L, 10L, new String[] { "favorites", "notfavorited" }, answer); assertEquals("One answer", answer.size(), 1); assertEquals("Right answer", "notfavorited", answer.iterator().next() .getName()); answer = ruleManager.match(qtext, 1L, 0L, new String[] { "favorites", "notfavorited" }, answer); assertEquals("Zero answers (pageLength 0)", answer.size(), 0); answer = ruleManager.match(qtext, 3L, QueryManager.DEFAULT_PAGE_LENGTH, new String[] { "favorites", "notfavorited" }, answer); assertEquals("Zero answers (default pageLength, but start beyond result size)", answer.size(), 0); }
@Test(expected = UnsupportedOperationException.class) public void testBulkSearchSQDwithWrongResponseFormat() throws KeyManagementException, NoSuchAlgorithmException, Exception { loadTxtDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); docMgr.setNonDocumentFormat(Format.JSON); SearchHandle results = new SearchHandle(); DocumentPage page = docMgr.search(qd, 1, results); MatchDocumentSummary[] summaries = results.getMatchResults(); for (MatchDocumentSummary summary : summaries) { MatchLocation[] locations = summary.getMatchLocations(); for (MatchLocation location : locations) { System.out.println(location.getAllSnippetText()); // do something with the snippet text } } }
@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 testH_DeletePojos() throws Exception { cities.delete(1185098, 2239076); StringQueryDefinition query = Common.client.newQueryManager().newStringDefinition(); query.setCriteria("Tungi OR Dalatando OR Chittagong"); try ( PojoPage<City> page = cities.search(query, 1) ) { assertEquals("Failed to read number of records expected", 1, page.getTotalSize()); } // now delete them all cities.deleteAll(); long count = cities.count(); assertEquals("Failed to read number of records expected", 0, count); }
@Test public void testPointPositiveLangLat() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPointPositiveLangLat"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-pair:\"12,5\""); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); // release client client.release(); }
@Test public void testPointNegativeLatPositiveLang() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPointNegativeLatPositiveLang"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-pair:\"-12,5\""); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); // release client client.release(); }
@Test public void testBoxAndWord() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBoxAndWord"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\" AND karl_kara"); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("/geo-constraint/geo-constraint20.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); // release client client.release(); }
@Test public void testPointPositiveLangLat() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPointPositiveLangLat"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-child:\"12,5\""); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); // release client client.release(); }
@Test public void testBoxAndWord() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPointAndWord"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-child:\"[-12,-5,-11,-4]\" AND karl_kara"); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("/geo-constraint/geo-constraint2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); // release client client.release(); }
@Test public void testBoxNegativeLatPositiveLang() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("testBoxNegativeLatPositiveLang"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\""); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); assertXpathEvaluatesTo("jack_kara -11,5 -11,5 -11 5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); assertXpathEvaluatesTo("karl_jill -12,4 -12,4 -12 4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); // release client client.release(); }
@Test public void testStringSearch2() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); StringQueryDefinition qdef = queryMgr.newStringDefinition(); qdef.setCriteria("10"); qdef.setDirectory("/sample/"); SearchHandle handle = new SearchHandle(); handle = queryMgr.search(qdef, handle); assertNotNull(handle); MatchDocumentSummary[] summaries = handle.getMatchResults(); assertNotNull(summaries); assertEquals("expected 2 results", 2, summaries.length); for ( MatchDocumentSummary summary : summaries ) { MatchLocation[] locations = summary.getMatchLocations(); assertEquals("expected 1 match location", 1, locations.length); for ( MatchLocation location : locations ) { assertNotNull(location.getAllSnippetText()); } } }
@Test public void testBulkSearchSQDwithResponseFormatandStringHandle() throws KeyManagementException, NoSuchAlgorithmException, Exception { loadTxtDocuments(); loadJSONDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); docMgr.setNonDocumentFormat(Format.JSON); docMgr.setSearchView(QueryView.METADATA); docMgr.setMetadataCategories(Metadata.PERMISSIONS); StringHandle results = new StringHandle().withFormat(Format.JSON); DocumentPage page = docMgr.search(qd, 1, results); DocumentMetadataHandle mh = new DocumentMetadataHandle(); while (page.hasNext()) { DocumentRecord rec = page.next(); validateRecord(rec, Format.TEXT); docMgr.readMetadata(rec.getUri(), mh); assertTrue("Records has permissions? ", mh.getPermissions().containsKey("flexrep-eval")); assertTrue("Record has collections ?", mh.getCollections().isEmpty()); } assertFalse("Search handle contains", results.get().isEmpty()); }
@Test public void testStringSearch() throws IOException, ParserConfigurationException, SAXException, FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { String optionsName = writeOptions(); QueryManager queryMgr = Common.client.newQueryManager(); StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName); qdef.setCriteria("grandchild1 OR grandchild4"); qdef.setDirectory("/sample/"); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); assertFalse(results.getMetrics().getTotalTime() == -1); FacetResult[] facets = results.getFacetResults(); assertNotNull(facets); assertEquals("expected 1 facet", 1, facets.length); FacetValue[] facetVals = facets[0].getFacetValues(); assertEquals("expected 6 facet values", 6, facetVals.length); MatchDocumentSummary[] summaries = results.getMatchResults(); assertNotNull(summaries); assertEquals("expected 2 results", 2, summaries.length); }