@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? }
@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. } } }
StructuredQueryDefinition termQuery1 = qb.term("Atlantic");
StructuredQueryDefinition qd = qb.and(qb.term("woof")); docMgr.setNonDocumentFormat(Format.JSON);
@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(); }
StructuredQueryDefinition containerQuery = qb.containerConstraint("title-contain", qb.term("Bush"));
StructuredQueryDefinition termQuery = qb.term("bill_kara"); StructuredQueryDefinition finalOrQuery = qb.or(geoElementConstraintQuery, termQuery);
StructuredQueryDefinition termQuery = qb.term("Memex"); StructuredQueryDefinition docQuery = qb.or(qb.document("/dir1/dir2/constraint2.xml"), qb.document("/dir3/dir4/constraint4.xml"), qb.document("/dir3/dir4/constraint5.xml")); StructuredQueryDefinition andFinalQuery = qb.and(termQuery, docQuery);
StructuredQueryDefinition termQuery = qb.term("Memex"); StructuredQueryDefinition dirQuery = qb.directory(true, "/dir3/"); StructuredQueryDefinition andFinalQuery = qb.and(termQuery, dirQuery);
StructuredQueryDefinition termQuery = qb.term("Memex"); StructuredQueryDefinition docQuery = qb.or(qb.document("/dir1/dir2/constraint2.xml"), qb.document("/dir3/dir4/constraint4.xml"), qb.document("/dir3/dir4/constraint5.xml")); StructuredQueryDefinition andFinalQuery = qb.and(termQuery, docQuery);
StructuredQueryDefinition termQuery = qb.term("Memex"); StructuredQueryDefinition docQuery = qb.or(qb.document("/dir1/dir2/constraint2.xml"), qb.document("/dir3/dir4/constraint4.xml"), qb.document("/dir3/dir4/constraint5.xml")); StructuredQueryDefinition andFinalQuery = qb.and(termQuery, docQuery);
@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 testBulkSearchSQDwithResponseFormatandStringHandle() throws KeyManagementException, NoSuchAlgorithmException, Exception { loadJSONDocuments(); JSONDocumentManager docMgr = client.newJSONDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("dog1", "dog11")); queryMgr.search(qd, new SearchHandle()); 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.JSON); 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 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 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); }