@Override public QueryBatcher buildQueryBatcher(DatabaseClient databaseClient, DataMovementManager dataMovementManager) { StructuredQueryDefinition query = databaseClient.newQueryManager().newStructuredQueryBuilder().collection(collectionUris); return dataMovementManager.newQueryBatcher(query); } }
@Override public QueryBatcher buildQueryBatcher(DatabaseClient databaseClient, DataMovementManager dataMovementManager) { StructuredQueryDefinition query = databaseClient.newQueryManager().newStructuredQueryBuilder().document(documentUris); return dataMovementManager.newQueryBatcher(query); } }
@Test public void testPolygonTouchesPoint() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPolygonTouchesPoint"); QueryManager queryMgr = client.newQueryManager(); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/polygon")), GeospatialOperator.TOUCHES, qb.point(-26.0, 100.89) ); // 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 testPolygonTouchesPoint method ", 2, resultNode.path("total").asInt()); String exptdString = "POLYGON((153.65 -8.35,170.57 -26.0,162.52 -52.52,136.0 -56.35,111.0 -51.0,100.89 -26.0,108.18 1.82,136.0 10.26,153.65 -8.35))"; String polygondesc1 = jsonPointNodes.get(0).path("matches").get(0).path("match-text").get(0).path("highlight").asText(); String polygondesc2 = jsonPointNodes.get(1).path("matches").get(0).path("match-text").get(0).path("highlight").asText(); assertTrue("Polygon Node not returned from testPolygonTouchesPoint method ", exptdString.equalsIgnoreCase(polygondesc1)); assertTrue("Polygon Node not returned from testPolygonTouchesPoint method ", exptdString.equalsIgnoreCase(polygondesc2)); }
@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 testBoxContainsPolygon() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBoxContainsPolygon"); QueryManager queryMgr = client.newQueryManager(); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/box"), CoordinateSystem.WGS84DOUBLE), GeospatialOperator.CONTAINS, qb.polygon(qb.point(-5, -70), qb.point(4, -70), qb.point(3, -60), qb.point(-3, -65), qb.point(-5, -70))); // 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 testBoxContainsPolygon method ", 2, resultNode.path("total").asInt()); assertTrue("URI returned from testBoxContainsPolygon 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 testBoxContainsPolygon method is incorrect", jsonPointNodes.get(0).path("uri").asText().contains("/Equator.xml") || jsonPointNodes.get(1).path("uri").asText().contains("/Equator.xml")); }
@Test public void testJSON() { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.term("leaf3"); // create a handle for the search results StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON); // run the search queryMgr.search(t, resultsHandle); assertEquals("{", resultsHandle.get().substring(0, 1)); // It's JSON, right? }
@Ignore public void testLinestringCrossesBox() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testLinestringCrossesBox"); QueryManager queryMgr = client.newQueryManager(); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/linestring"), CoordinateSystem.WGS84DOUBLE), GeospatialOperator.CROSSES, qb.box(-5.45, -76.35643, 5.35, -54.636) ); // create handle JacksonHandle resultsHandle = new JacksonHandle(); queryMgr.search(t, resultsHandle); // get the result JsonNode resultNode = resultsHandle.get(); // Should have 2 nodes returned. assertEquals("Two nodes not returned from testPolygonCoversBox method ", 2, resultNode.path("total").asInt()); }
private RawStructuredQueryDefinition allButCollectors() { StructuredQueryBuilder sqb = jobClient.newQueryManager().newStructuredQueryBuilder(); RawStructuredQueryDefinition allButCollectors = sqb.build( sqb.and( sqb.containerQuery(sqb.element("trace"), sqb.not(sqb.value(sqb.jsonProperty("label"), "collector"))))); return allButCollectors; }
@Test public void testFailedSearch() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition qdef = qb.term( "criteriaThatShouldNotMatchAnyDocument"); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
@Test public void test105Searches() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); for (int i=0;i<105;i++) { for (QueryDefinition t : new QueryDefinition[] { qb.term("leaf3"), qb.build(qb.value(qb.element("leaf"), "leaf3")) }) { JacksonHandle results = queryMgr.search(t, new JacksonHandle()); assertNotNull(results); JsonNode jsonResults =results.get(); @SuppressWarnings("unused") String resultString = results.getMapper().writeValueAsString(jsonResults); // ignore. } } }
@Test public void testPolygonDisjointPolygon() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPolygonDisjointPolygon"); QueryManager queryMgr = client.newQueryManager(); queryMgr.setPageLength(50); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.geospatial(qb.geoRegionPath(qb.pathIndex("/root/item/polygon"), CoordinateSystem.WGS84), GeospatialOperator.DISJOINT, qb.polygon( qb.point(-90, 131), qb.point(-85, 133), qb.point(-87, 134), qb.point(-88, 135), qb.point(-90, 131)) ); // create handle JacksonHandle resultsHandlejh = new JacksonHandle(); queryMgr.search(t, resultsHandlejh); // get the result JsonNode resultNodejh = resultsHandlejh.get(); // Should have 20 nodes returned. assertEquals("Twenty nodes not returned from testPolygonCoversBox method ", 20, resultNodejh.path("total").asInt()); }
@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())); } }
@Test public void testTestStructuredSearchGeoBox() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testTestStructuredSearchGeoBox"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs loadGeoData(); setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); StructuredQueryDefinition geoElementConstraintQuery = qb.geospatialConstraint("geo-elem-child", qb.box(-12, -5, -11, -4)); StructuredQueryDefinition termQuery = qb.term("karl_kara"); StructuredQueryDefinition finalAndQuery = qb.and(geoElementConstraintQuery, termQuery); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(finalAndQuery, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); System.out.println("Output : " + convertXMLDocumentToString(resultDoc)); 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 testBug22037() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBug22037"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; String queryOptionName = "rangeConstraintIntOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); } setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); StructuredQueryDefinition rangeQuery = qb.range(qb.element("popularity"), "xs:int", Operator.GE, 4); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(rangeQuery, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); // System.out.println(convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); // release client client.release(); }
@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()); } }
@Test public void buildCombinedQuery() throws Exception { String qtext = "hello"; String collection = "myCollection"; QueryOptionsWriteHandle options = buildOptions(); StructuredQueryBuilder sqb = Common.client.newQueryManager().newStructuredQueryBuilder(); StructuredQueryDefinition structuredQuery = sqb.collection(collection); // NOTICE!! CombinedQueryBuilderImpl is for internal use only for now--it's undocmented and unsupported. CombinedQueryBuilderImpl cqb = new CombinedQueryBuilderImpl(); CombinedQueryDefinition query = cqb.combine(structuredQuery, options, qtext); String expected = buildExpected(qtext, collection); String serialized = query.serialize(); assertXMLEqual("Output not as expected", expected, serialized); PojoRepository<City, Integer> cities = Common.client.newPojoRepository(City.class, Integer.class); PojoQueryBuilder<City> qb = cities.getQueryBuilder(); query = (CombinedQueryDefinition) qb.filteredQuery(qb.word("asciiName", new String[] {"wildcarded"}, 1, "Chittagong*")); expected = "<?xml version=\"1.0\" ?>" + "<search xmlns=\"http://marklogic.com/appservices/search\">" + "<query>" + "<word-query>" + "<json-property>asciiName</json-property><text>Chittagong*</text>" + "<term-option>wildcarded</term-option><weight>1.0</weight>" + "</word-query>" + "</query>" + "<options><search-option>filtered</search-option></options>" + "</search>"; serialized = query.serialize(); assertXMLEqual("Output not as expected", expected, serialized); }
@Test public void testConvertedSearches() throws IOException { // Structured Query, No Options StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(null); StructuredQueryDefinition t = qb.term("leaf3"); check(new StringHandle(head + t.serialize() + returnQueryOption + tail)); // String query no options String str = head + qtext1 + returnQueryOption + tail; check(new StringHandle(str).withMimetype("application/xml")); // String query plus options str = head + qtext2 + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str), "alerts"); // All three str = head + qtext3 + t.serialize() + optionsString + tail; check(new StringHandle(str)); }
@Test public void testStructuredSearch() 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")) }) { SearchHandle results = queryMgr.search(t, new SearchHandle()); assertNotNull(results); assertFalse(results.getMetrics().getTotalTime() == -1); MatchDocumentSummary[] summaries = results.getMatchResults(); assertNotNull(summaries); assertEquals("expected 1 result", 1, 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 testCombinedSearches() throws IOException { // Structured Query, No Options StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(null); StructuredQueryDefinition t = qb.term("leaf3"); check(new StringHandle(head + t.serialize() + tail)); // String query no options String str = head + qtext1 + tail; check(new StringHandle(str).withMimetype("application/xml")); // String query plus options str = head + qtext2 + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str), "alerts"); // All three str = head + qtext3 + t.serialize() + optionsString + tail; check(new StringHandle(str)); }
@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); }