use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method indexOnInnerElement.
@Test
public void indexOnInnerElement() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
configureAndStore(CONF2, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ngram:contains(note, 'nested inner')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note> elements.</para>", result);
seq = xquery.execute(broker, "//note[ngram:contains(., 'nested inner')]/parent::para", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note> elements.</para>", result);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method wildcardMatch.
@Test
public void wildcardMatch() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException, XpathException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., 'double.*match')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "double match double match" + MATCH_END + "</para>", result);
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., 'paragraph.*content\\.')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>some " + MATCH_START + "paragraph with " + MATCH_END + "<hi>" + MATCH_START + "mixed" + MATCH_END + "</hi>" + MATCH_START + " content." + MATCH_END + "</para>", result);
String wildcardQuery = "...with.*[tn].*ele.ent[sc].*";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>another paragra" + MATCH_START + "ph with " + MATCH_END + "<note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note>" + MATCH_START + " elements." + MATCH_END + "</para>", result);
final XpathEngine xpe = XMLUnit.newXpathEngine();
final NodeList matches = xpe.getMatchingNodes("//exist:match", XMLUnit.buildControlDocument(result));
final StringBuilder m = new StringBuilder();
for (int i = 0; i < matches.getLength(); i++) {
m.append(matches.item(i).getTextContent());
}
String match = m.toString();
assertMatches(wildcardQuery, match);
wildcardQuery = "\\*.*\\?";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>Where did all the " + MATCH_START + "*s go?" + MATCH_END + "</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
wildcardQuery = ".est[][?]tes.";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " " + MATCH_START + "test[test" + MATCH_END + " " + MATCH_START + "test?test" + MATCH_END + "</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '^" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " test[test test?test</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "$')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>test]test test[test " + MATCH_START + "test?test" + MATCH_END + "</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
wildcardQuery = "^aaa.aaa$";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "aaacaaa" + MATCH_END + "</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
wildcardQuery = ".+simple";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "a simple" + MATCH_END + " paragraph</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
wildcardQuery = "b.{3,6}c";
seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
assertNotNull(seq);
assertEquals(2, seq.getItemCount());
for (int i = 0; i < 2; i++) {
result = queryResult2String(broker, seq, i);
match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
assertMatches(wildcardQuery, match);
}
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method matchInParent.
@Test
public void matchInParent() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'mixed')]/hi", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
final String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<hi>" + MATCH_START + "mixed" + MATCH_END + "</hi>", result);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method doubleMatch.
@Test
public void doubleMatch() throws PermissionDeniedException, XPathException, SAXException, EXistException, CollectionConfigurationException, LockException, IOException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'double match')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "double match" + MATCH_END + " " + MATCH_START + "double match" + MATCH_END + "</para>", result);
seq = xquery.execute(broker, "//para[ngram:contains(., 'aaa aaa')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>" + MATCH_START + "aaa aaa" + MATCH_END + " aaa</para>", result);
seq = xquery.execute(broker, "//para[ngram:ends-with(., 'aaa aaa')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>aaa " + MATCH_START + "aaa aaa" + MATCH_END + "</para>", result);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method matchInAncestor.
@Test
public void matchInAncestor() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'nested')]/note", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi> inner</note>", result);
seq = xquery.execute(broker, "//para[ngram:contains(., 'nested')]//hi", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<hi>" + MATCH_START + "nested" + MATCH_END + "</hi>", result);
}
}
Aggregations