@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 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); }
assertTrue("Searh response has entry for facets", jh.get().has("metrics")); queryMgr.setView(QueryView.FACETS); queryMgr.search(qd, jh); System.out.println(jh.get().toString());
assertTrue("Searh response has entry for metrics", jh.get().has("metrics")); queryMgr.setView(QueryView.FACETS); queryMgr.search(qd, jh); System.out.println(jh.get().toString());
@Test public void testPointNegativeLangLat() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testPointNegativeLangLat"); String queryOptionName = "geoConstraintOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs for (int i = 1; i <= 7; i++) { writeDocumentUsingInputStreamHandle(client, "geo-constraint" + i + ".xml", "/geo-constraint/", "XML"); } setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); queryMgr.setView(QueryView.ALL); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("geo-elem:\"-12,-5\""); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); System.out.println("testPointNegativeLangLat Result : " + convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("geo-elem:\"-12,-5\"", "string(//*[local-name()='qtext'])", resultDoc); // release client client.release(); }
queryMgr.setView(QueryView.DEFAULT);
queryMgr.setView(QueryView.ALL);
@Test public void testSetViewResults() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testSetViewResults"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; String queryOptionName = "setViewOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-results/", "XML"); } setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); queryMgr.setView(QueryView.RESULTS); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("pop:high"); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); // System.out.println(convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); assertXpathExists("//*[local-name()='result']", resultDoc); // release client client.release(); }
@Test public void testSetViewFacets() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testSetViewFacets"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; String queryOptionName = "setViewOpt.xml"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-facets/", "XML"); } setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); queryMgr.setView(QueryView.FACETS); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("pop:high"); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); // System.out.println(convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); assertXpathExists("//*[local-name()='facet']", resultDoc); // release client client.release(); }
RawCombinedQueryDefinition query = queryMgr.newRawCombinedQueryDefinition(new StringHandle( "<search xmlns='http://marklogic.com/appservices/search'>" + "<options>" + "<extract-document-data selected='all'>" + "<extract-path>//description[@xml:lang='en']</extract-path>" + "</extract-document-data>" + "<constraint name='myFacet'>" + "<range type='xs:string' facet='true'>" + "<element name='grandchild'/>" + "</range>" + "</constraint>" + "<extract-metadata>" + "<qname elem-name='description'/>" + "</extract-metadata>" + "<return-constraints>true</return-constraints>" + "<return-facets>true</return-facets>" + "<return-metrics>true</return-metrics>" + "<return-plan>true</return-plan>" + "<return-qtext>true</return-qtext>" + "<return-query>true</return-query>" + "<return-results>true</return-results>" + "<debug>true</debug>" + "</options>" + "<query>" + "<and-query>" + "<collection-query><uri>xml</uri></collection-query>" + "<collection-query><uri>products</uri></collection-query>" + "</and-query>" + "</query>" + "</search>")); queryMgr.setView(QueryView.ALL); SearchHandle results = queryMgr.search(query, new SearchHandle()); assertTrue(results.getConstraintIterator(new StringHandle()).next().get().startsWith("<search:constraint"));
queryMgr.setView(QueryView.METADATA);
@Test public void testStringSearch4() throws IOException, 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); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); 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(); assertTrue(summaries == null || summaries.length == 0); queryMgr.setView(QueryView.RESULTS); results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); facets = results.getFacetResults(); assertTrue(facets == null || facets.length == 0); summaries = results.getMatchResults(); assertNotNull(summaries); assertEquals("expected 2 results", 2, summaries.length); assertEquals("empty-snippet", results.getSnippetTransformType()); }