@Test(expected = ClassCastException.class) public void testPOJOqbeSearchWithSearchHandle() { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\":{" + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" + ",\"$filtered\": true}}"; System.out.println(queryAsString); PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawQueryByExampleDefinition( new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("even"); SearchHandle results = new SearchHandle(); products.setPageLength(10); p = products.search(qd, 1, results); assertEquals("total no of pages", 5, p.getTotalPages()); System.out.println(p.getTotalPages()); // System.out.println(results.getMetrics().getQueryResolutionTime()); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo, results); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); assertTrue( "Enventory lies between 1010 to 1110", a.getInventory() > 1010 && a.getInventory() <= 1110); assertTrue("Artifact Id is even", a.getId() % 2 == 0); assertTrue("Company name contains Acme", a.getManufacturer().getName().contains("Acme")); count++; // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); } assertEquals("Page size", count, p.size()); pageNo = pageNo + p.getPageSize(); MatchDocumentSummary[] mds = results.getMatchResults(); 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")); } String[] facetNames = results.getFacetNames(); for (String fname : facetNames) { System.out.println(fname); } // assertEquals("Total results from search handle ",50,results.getTotalResults()); // assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertEquals("Page start check", 41, p.getStart()); assertEquals("page number after the loop", 5, p.getPageNumber()); assertEquals("total no of pages", 5, p.getTotalPages()); }
@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); }
// This test is to verify extract-document-data & extract-path with selected=exclude option query @Test public void testExtractDocumentData2() throws Exception { this.loadJSONDocuments(); this.loadXMLDocuments(); String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; String tail = "</search:search>"; String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; DocumentManager docMgr = client.newDocumentManager(); QueryManager queryMgr = client.newQueryManager(); String options = "<search:options>" + "<search:extract-document-data selected=\"exclude\">" + "<search:extract-path>//foo</search:extract-path>" + "<search:extract-path>//says</search:extract-path>" + "</search:extract-document-data>" + "</search:options>"; // test XML response with extracted XML and JSON matches String combinedSearch = head + qtext4 + options + tail; RawCombinedQueryDefinition rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition( new StringHandle(combinedSearch).withMimetype("application/xml")); SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); MatchDocumentSummary[] summaries = results.getMatchResults(); assertNotNull(summaries); assertEquals(2, summaries.length); for (MatchDocumentSummary summary : summaries) { ExtractedResult extracted = summary.getExtracted(); if (Format.XML == summary.getFormat()) { // we don't test for kind because it isn't sent in this case System.out.println("EXTRACTED Size ==" + extracted.size()); // TODO:: Bug 33921 also add test to include-with-ancestors assertEquals(0, extracted.size()); // Document item1 = extracted.next().getAs(Document.class); // assertEquals("This is so foo with a bar 71", item1.getFirstChild().getTextContent()); continue; } else if (Format.JSON == summary.getFormat()) { // we don't test for kind because it isn't sent in this case assertEquals(1, extracted.size()); for (ExtractedItem item : extracted) { String stringJsonItem = item.getAs(String.class); JsonNode nodeJsonItem = item.getAs(JsonNode.class); if (nodeJsonItem.has("animal")) { assertEquals("{\"animal\":\"dog14\"}", stringJsonItem); continue; } fail("unexpected extracted item:" + stringJsonItem); } continue; } fail("unexpected search result:" + summary.getUri()); } }
@Test public void testExtractMetadata() throws SAXException, IOException { QueryManager queryMgr = Common.client.newQueryManager(); String combined = "<search xmlns=\"http://marklogic.com/appservices/search\">" + "<query>" + "<value-query>" + "<element ns=\"http://marklogic.com/xdmp/json\" name=\"firstKey\"/>" + "<text>first value</text>" + "</value-query>" + "</query>" + "<options>" + "<extract-metadata>" + "<qname elem-ns=\"http://marklogic.com/xdmp/json\" elem-name=\"subKey\"/>" + "</extract-metadata>" + "</options>" + "</search>"; StringHandle rawHandle = new StringHandle(combined); RawCombinedQueryDefinition rawDef = queryMgr.newRawCombinedQueryDefinition(rawHandle); SearchHandle sh = queryMgr.search(rawDef, new SearchHandle()); MatchDocumentSummary[] summaries = sh.getMatchResults(); assertNotNull(summaries); assertEquals("expected 1 result", 1, summaries.length); MatchDocumentSummary matchResult = summaries[0]; Document metadata = matchResult.getMetadata(); Element subKey = (Element) metadata.getElementsByTagNameNS("http://marklogic.com/xdmp/json", "subKey").item(0); assertEquals("string", subKey.getAttribute("type")); assertEquals("sub value", subKey.getTextContent()); String docStr = Common.testDocumentToString(metadata); String handleStr = matchResult.getMetadata(new StringHandle()).get(); assertXMLEqual("Different metadata for handle", docStr, handleStr); Document snippet = matchResult.getSnippets()[0]; docStr = Common.testDocumentToString(snippet); handleStr = matchResult.getSnippetIterator(new StringHandle()).next().get(); assertXMLEqual("Different snippet for handle", docStr, handleStr); }
@Test public void testStructuredSearch2() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); EditableNamespaceContext namespaces = new EditableNamespaceContext(); namespaces.put("x", "root.org"); namespaces.put("y", "target.org"); StructuredQueryBuilder qb = new StructuredQueryBuilder(namespaces); StructuredQueryDefinition qdef = qb.geospatial(qb.geoPath(qb.pathIndex("/x:geo/y:path")), qb.box(1, 2, 3, 4)); SearchHandle results = queryMgr.search(qdef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
@Test public void testPOJOSearchWithSearchHandle() { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("Acme"); SearchHandle results = new SearchHandle(); products.setPageLength(11); p = products.search(qd, 1, results); assertEquals("total no of pages", 5, p.getTotalPages()); System.out.println(p.getTotalPages()); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo, results); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); assertTrue("Artifact Id is even", a.getId() % 2 == 0); assertTrue("Company name contains Acme", a.getManufacturer().getName().contains("Acme")); count++; // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); } assertEquals("Page size", count, p.size()); pageNo = pageNo + p.getPageSize(); MatchDocumentSummary[] mds = results.getMatchResults(); assertEquals("Size of the results summary", 11, mds.length); for (MatchDocumentSummary md : mds) { assertTrue("every uri should contain the class name", md.getUri().contains("Artifact")); } String[] facetNames = results.getFacetNames(); for (String fname : facetNames) { System.out.println(fname); } assertEquals("Total resulr from search handle ", 55, results.getTotalResults()); // assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertEquals("Page start check", 45, p.getStart()); assertEquals("page number after the loop", 5, p.getPageNumber()); assertEquals("total no of pages", 5, p.getTotalPages()); }
@Override public PojoPage<T> search( PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) { if (searchHandle != null) { HandleAccessor.checkHandle(searchHandle, "search"); if (searchHandle instanceof SearchHandle) { SearchHandle responseHandle = (SearchHandle) searchHandle; if (docMgr instanceof DocumentManagerImpl) { responseHandle.setHandleRegistry( ((DocumentManagerImpl<?, ?>) docMgr).getHandleRegistry()); } responseHandle.setQueryCriteria(query); } } DocumentPage docPage = docMgr.search(wrapQuery(query), start, searchHandle, transaction); PojoPage<T> pojoPage = new PojoPageImpl<T>(docPage, entityClass); return pojoPage; }
@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 testBulkSearchSQDwithDifferentPageSizes() throws IOException, ParserConfigurationException, SAXException, TransformerException { int count; loadXMLDocuments(); // Creating a txt document manager for bulk search TextDocumentManager docMgr = client.newTextDocumentManager(); // using QueryManger for query definition and set the search criteria StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("foo", "bar")); // set document manager level settings for search response System.out.println("Default Page length setting on docMgr :" + docMgr.getPageLength()); docMgr.setPageLength(1); docMgr.setSearchView(QueryView.RESULTS); docMgr.setNonDocumentFormat(Format.XML); assertEquals("format set on document manager", "XML", docMgr.getNonDocumentFormat().toString()); assertEquals( "Queryview set on document manager ", "RESULTS", docMgr.getSearchView().toString()); assertEquals("Page length ", 1, docMgr.getPageLength()); // Search for documents where content has bar and get first result record, get search handle on // it SearchHandle sh = new SearchHandle(); DocumentPage page = docMgr.search(qd, 0); // test for page methods assertEquals("Number of records", 1, page.size()); assertEquals("Starting record in first page ", 1, page.getStart()); assertEquals("Total number of estimated results:", 102, page.getTotalSize()); assertEquals("Total number of estimated pages :", 102, page.getTotalPages()); // till the issue #78 get fixed assertTrue("Is this First page :", page.isFirstPage()); // this is bug assertFalse("Is this Last page :", page.isLastPage()); assertTrue("Is this First page has content:", page.hasContent()); // Need the Issue #75 to be fixed assertFalse("Is first page has previous page ?", page.hasPreviousPage()); // long pageNo = 1; do { count = 0; page = docMgr.search(qd, pageNo, sh); if (pageNo > 1) { assertFalse("Is this first Page", page.isFirstPage()); assertTrue("Is page has previous page ?", page.hasPreviousPage()); } while (page.hasNext()) { DocumentRecord rec = page.next(); rec.getFormat(); validateRecord(rec, Format.XML); // System.out.println(rec.getUri()); count++; } MatchDocumentSummary[] mds = sh.getMatchResults(); assertEquals("Matched document count", 1, mds.length); // since we set the query view to get only results, facet count supposed be 0 assertEquals("Matched Facet count", 0, sh.getFacetNames().length); assertEquals("document count", page.size(), count); // assertEquals("Page Number #",pageNo,page.getPageNumber()); pageNo = pageNo + page.getPageSize(); } while (!page.isLastPage()); // assertTrue("page count is 101 ",pageNo > page.getTotalPages()); assertTrue("Page has previous page ?", page.hasPreviousPage()); assertEquals("page size", 1, page.getPageSize()); assertEquals("document count", 102, page.getTotalSize()); page = docMgr.search(qd, 103); assertFalse("Page has any records ?", page.hasContent()); }