Search in sources :

Example 26 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class IndexerTest3 method store_and_retrieve_suppress_type.

private String store_and_retrieve_suppress_type(final String type, final String typeXml, final String typeXquery) throws EXistException, IOException, LockException, AuthenticationException, PermissionDeniedException, SAXException, XPathException {
    store_suppress_type(type, typeXml);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final StringWriter out = new StringWriter()) {
        final XQuery xquery = pool.getXQueryService();
        final Sequence result = xquery.execute(broker, typeXquery, null);
        final Properties props = new Properties();
        props.setProperty(OutputKeys.INDENT, "no");
        final SAXSerializer serializer = new SAXSerializer(out, props);
        serializer.startDocument();
        for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
            final Item next = i.nextItem();
            next.toSAX(broker, serializer, props);
        }
        serializer.endDocument();
        return out.toString();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) SequenceIterator(org.exist.xquery.value.SequenceIterator) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) Properties(java.util.Properties) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool)

Example 27 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class LuceneMatchListenerTest method inlineMatchNodes_whenIndenting.

@Test
public void inlineMatchNodes_whenIndenting() throws EXistException, PermissionDeniedException, XPathException, SAXException, CollectionConfigurationException, LockException, IOException {
    configureAndStore(CONF5, XML2);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        final String query = "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";" + "//tei:p[.//tei:w[ft:query(., <query><bool><term>дознајем</term></bool></query>)]] ! util:expand(.)";
        final Sequence seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        final String result = queryResult2String(broker, seq, true);
        final String expected = "<p xmlns=\"http://www.tei-c.org/ns/1.0\">\n" + "    <s type=\"combo\">\n" + "        <w lemma=\"из\">из</w>\n" + "        <w>новина</w>\n" + "        <w lemma=\"и\">и</w>\n" + "        <w lemma=\"од\">од</w>\n" + "        <lb/>\n" + "        <pb n=\"32\"/>\n" + "        <w>других</w>\n" + "        <w lemma=\"човек\">људи</w>\n" + "        <w>" + MATCH_START + "дознајем" + MATCH_END + "</w>, <w xml:id=\"VSK.P13.t1.p4.w205\" lemma=\"ма\">ма</w>\n" + "        <w>се</w>\n" + "        <w lemma=\"не\">не</w>\n" + "        <w>прорезује</w>\n" + "        <w>право</w>\n" + "        <w lemma=\"по\">по</w>\n" + "        <w>имућству</w>, <w xml:id=\"VSK.P13.t1.p4.w219\" lemma=\"те\">те</w>\n" + "        <w>се</w>\n" + "        <w>на</w>\n" + "        <w lemma=\"то\">то</w>\n" + "        <w>видим</w>\n" + "        <w>многи</w>\n" + "        <w>љуте</w>.</s>\n" + "</p>";
        XMLAssert.assertEquals(expected, result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 28 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class LuceneMatchListenerTest method matchInAncestor.

@Test
public void matchInAncestor() throws EXistException, PermissionDeniedException, XPathException, SAXException, IOException, XpathException, LockException, CollectionConfigurationException {
    configureAndStore(CONF1, 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')]/hi", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//exist:match)", result);
        seq = xquery.execute(broker, "//para[ft:query(., 'nested')]/note", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//hi/exist:match)", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 29 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class SerializeAttrMatchesTest method expandAttr.

@Test
public void expandAttr() throws CollectionConfigurationException, LockException, IOException, SAXException, PermissionDeniedException, EXistException, XPathException {
    configureAndStore(COLLECTION_CONFIG, XML, "test1.xml");
    // query and expand
    final String query = "for $hit in collection(\"" + TestConstants.TEST_COLLECTION_URI.toString() + "\")//w[ft:query(@lemma, <query><regex>књиг.*</regex></query>)]\n" + "return util:expand($hit, \"highlight-matches=both\")";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        final Sequence seq = xquery.execute(broker, query, null);
        assertEquals(1, seq.getItemCount());
        final Item item = seq.itemAt(0);
        assertTrue(item instanceof ElementImpl);
        assertEquals("lemma", ((ElementImpl) item).getAttribute("exist:matches"));
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 30 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class LuceneIndexTest method MultiTermQueryRewriteMethod.

@Test
public void MultiTermQueryRewriteMethod() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException {
    configureAndStore(COLLECTION_CONFIG8, XML9, "test.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, "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";" + " for $expr in (\"au*\", \"ha*\", \"ma*\", \"za*\", \"ya*\", \"ra*\", \"qa*\")" + " let $query := <query><wildcard>{$expr}</wildcard></query>" + " return for $hit in //tei:p[ft:query(., $query)]" + " return util:expand($hit)//exist:match", null);
        assertNotNull(seq);
        assertEquals(10, seq.getItemCount());
        assertEquals("aus", seq.itemAt(0).getStringValue());
        seq = xquery.execute(broker, "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";" + " for $expr in (\"ha*\", \"ma*\")" + " let $query := <query><wildcard>{$expr}</wildcard></query>" + " return for $hit in //tei:p[ft:query(., $query)]" + " return util:expand($hit)//exist:match", null);
        assertNotNull(seq);
        assertEquals(2, seq.getItemCount());
        assertEquals("haus", seq.itemAt(0).getStringValue());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11