Search in sources :

Example 31 with XQuery

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

the class LuceneIndexTest method removeCollection.

@Test
public void removeCollection() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG1, SAMPLES.getShakespeareXmlSampleNames());
    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()) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//SPEECH[ft:query(LINE, 'love')]", null);
        assertNotNull(seq);
        assertEquals(166, seq.getItemCount());
        broker.removeCollection(transaction, root);
        root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
        transact.commit(transaction);
        root = null;
        checkIndex(docs, broker, null, null, 0);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 32 with XQuery

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

the class LuceneIndexTest method boosts.

@Test
public void boosts() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException {
    configureAndStore(COLLECTION_CONFIG6, XML6, "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, "for $a in ft:query((//b|//c), 'AAA') " + "order by ft:score($a) descending return $a/local-name(.)", null);
        assertNotNull(seq);
        assertEquals(3, seq.getItemCount());
        assertEquals("c", seq.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)

Example 33 with XQuery

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

the class LuceneIndexTest method queryTranslation.

@Test
public void queryTranslation() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException {
    configureAndStore(COLLECTION_CONFIG1, XML7, "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);
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        final CompiledXQuery compiled = xquery.compile(context, "declare variable $q external; " + "ft:query(//p, parse-xml($q)/query)");
        context.declareVariable("q", "<query><term>heiterkeit</term></query>");
        Sequence seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <term>heiterkeit</term><term>blablabla</term>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <term occur='should'>heiterkeit</term><term occur='should'>blablabla</term>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <term occur='must'>heiterkeit</term><term occur='must'>blablabla</term>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <term occur='must'>heiterkeit</term><term occur='not'>herzen</term>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <phrase occur='must'>wunderbare heiterkeit</phrase><term occur='must'>herzen</term>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <phrase slop='5'>heiterkeit seele eingenommen</phrase>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        // phrase with wildcards
        context.declareVariable("q", "<query>" + "   <phrase slop='5'><term>heiter*</term><term>se?nnnle*</term></phrase>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <wildcard>?eiter*</wildcard>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <fuzzy max-edits='2'>selee</fuzzy>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <bool>" + "       <fuzzy occur='must' max-edits='2'>selee</fuzzy>" + "       <wildcard occur='should'>bla*</wildcard>" + "   </bool>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <regex>heit.*keit</regex>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        context.declareVariable("q", "<query>" + "   <phrase><term>wunderbare</term><regex>heit.*keit</regex></phrase>" + "</query>");
        seq = xquery.execute(broker, compiled, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 34 with XQuery

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

the class LuceneIndexTest method xupdateReplace.

@Test
public void xupdateReplace() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, ParserConfigurationException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG2, XML2, "xupdate.xml");
    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()) {
        final Occurrences[] occur = checkIndex(docs, broker, new QName[] { new QName("description") }, "chair", 1);
        assertEquals("chair", occur[0].getTerm());
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 5);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ft:query(description, 'chair')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        String xupdate = XUPDATE_START + "<xu:replace select=\"//item[@id = '1']\">" + "<item id='4'><description>Wheelchair</description> <condition>poor</condition></item>" + "</xu:replace>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("description") }, "chair", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "chair", 0);
        Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("description") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "<xu:replace select=\"//item[@id = '4']/description\">" + "<description>Armchair</description>" + "</xu:replace>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("description") }, "wheelchair", 0);
        checkIndex(docs, broker, new QName[] { new QName("item") }, "wheelchair", 0);
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "armchair", 1);
        assertEquals("armchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "armchair", 1);
        assertEquals("armchair", o[0].getTerm());
        transact.commit(transaction);
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) BrokerPool(org.exist.storage.BrokerPool)

Example 35 with XQuery

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

the class LuceneIndexTest method analyzers.

@Test
public void analyzers() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG3, XML3, "test.xml");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        checkIndex(docs, broker, new QName[] { new QName("head") }, "TITLE", 1);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "uppercase", 1);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "/section[ft:query(p, 'UPPERCASE')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(head, 'TITLE')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(head, 'title')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) 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