use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method xupdateRename.
@Test
public void xupdateRename() throws EXistException, LockException, XPathException, PermissionDeniedException, SAXException, IOException, ParserConfigurationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:rename select=\"//item[@id='2']\">renamed</xu:rename>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "tab", 0);
transact.commit(transaction);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method indexKeys.
@Test
public void indexKeys() throws SAXException, PermissionDeniedException, XPathException, EXistException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "util:index-key-occurrences(/test/item, 'cha', 'ngram-index')", null);
// Sequence seq = xquery.execute("util:index-key-occurrences(/test/item, 'cha', 'org.exist.indexing.impl.NGramIndex')", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "util:index-key-occurrences(/test/item, 'le8', 'ngram-index')", null);
// seq = xquery.execute("util:index-key-occurrences(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "util:index-key-documents(/test/item, 'le8', 'ngram-index')", null);
// seq = xquery.execute("util:index-key-documents(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "util:index-key-documents(/test/item, 'le8', 'ngram-index')", null);
// seq = xquery.execute("util:index-key-doucments(/test/item, 'le8', 'org.exist.indexing.impl.NGramIndex')", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String queryBody = "declare function local:callback($key as item(), $data as xs:int+)\n" + "as element()+ {\n" + " <item>\n" + " <key>{$key}</key>\n" + " <frequency>{$data[1]}</frequency>\n" + " </item>\n" + "};\n" + "\n";
String query = queryBody + "util:index-keys(/test/item, \'\', util:function(xs:QName(\'local:callback\'), 2), 1000, 'ngram-index')";
// String query = queryBody + "util:index-keys(/test/item, \'\', util:function(xs:QName(\'local:callback\'), 2), 1000, 'org.exist.indexing.impl.NGramIndex')";
seq = xquery.execute(broker, query, null);
assertNotNull(seq);
// TODO : check cardinality
StringWriter out = new StringWriter();
Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "yes");
SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (SequenceIterator i = seq.iterate(); i.hasNext(); ) {
Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
// TODO : check content
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method dropIndex.
@Test
public void dropIndex() throws EXistException, PermissionDeniedException, XPathException, LockException, TriggerException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
assertNotNull(root);
root.removeXMLResource(transaction, broker, XmldbURI.create("test_string.xml"));
}
checkIndex(broker, docs, "cha", 0);
seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(0, seq.getItemCount());
transact.commit(transaction);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class LuceneMatchListenerTest method indexByQName.
/**
* Test match highlighting for index configured by QName, e.g.
* <create qname="a"/>.
*/
@Test
public void indexByQName() throws EXistException, PermissionDeniedException, XPathException, SAXException, CollectionConfigurationException, LockException, IOException {
configureAndStore(CONF2, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ft:query(., 'mixed')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq);
XMLAssert.assertEquals("<para>some paragraph with <hi>" + MATCH_START + "mixed" + MATCH_END + "</hi> content.</para>", result);
seq = xquery.execute(broker, "//para[ft:query(., '+nested +inner +elements')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq);
XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi> " + MATCH_START + "inner" + MATCH_END + "</note> " + MATCH_START + "elements" + MATCH_END + ".</para>", result);
seq = xquery.execute(broker, "//para[ft:query(term, 'term')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq);
XMLAssert.assertEquals("<para>a third paragraph with <term>" + MATCH_START + "term" + MATCH_END + "</term>.</para>", result);
seq = xquery.execute(broker, "//para[ft:query(., '+double +match')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq);
XMLAssert.assertEquals("<para>" + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + " " + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + "</para>", result);
seq = xquery.execute(broker, "for $para in //para[ft:query(., '+double +match')] return\n" + " <hit>{$para}</hit>", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq);
XMLAssert.assertEquals("<hit><para>" + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + " " + MATCH_START + "double" + MATCH_END + " " + MATCH_START + "match" + MATCH_END + "</para></hit>", result);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class Modification method select.
/**
* Evaluate the select expression.
*
* @param docs the documents to evaludate the expression over
*
* @return The selected nodes.
*
* @throws PermissionDeniedException if the caller has insufficient priviledges
* @throws EXistException if the database raises an error
* @throws XPathException if the XPath raises an error
*/
protected NodeList select(DocumentSet docs) throws PermissionDeniedException, EXistException, XPathException {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence resultSeq = null;
try {
resultSeq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (!(resultSeq.isEmpty() || Type.subTypeOf(resultSeq.getItemType(), Type.NODE))) {
throw new EXistException("select expression should evaluate to a node-set; got " + Type.getTypeName(resultSeq.getItemType()));
}
if (LOG.isDebugEnabled()) {
LOG.debug("found {} for select: {}", resultSeq.getItemCount(), selectStmt);
}
return resultSeq.toNodeSet();
}
Aggregations