Search in sources :

Example 81 with XQuery

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

the class LuceneIndexTest method inlineAndIgnore.

@Test
public void inlineAndIgnore() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG5, XML5, "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") }, "simple", 1);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "mixed", 1);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "dangerous", 1);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "note", 0);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "ignore", 0);
        checkIndex(docs, broker, new QName[] { new QName("p") }, "warnings", 1);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "/article[ft:query(head, 'title')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'highlighted')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'mixed')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'mix')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'dangerous')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'ous')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'danger')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(p, 'note')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'highlighted')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'mixed')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'dangerous')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'warnings')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'danger')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'note')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/article[ft:query(., 'ignore')]", 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)

Example 82 with XQuery

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

the class LuceneIndexTest method xupdateInsert.

/**
 * Remove nodes from different levels of the tree and check if the index is
 * correctly updated.
 */
@Test
public void xupdateInsert() 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());
        // Append to root node
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        String xupdate = XUPDATE_START + "   <xu:append select=\"/test\">" + "       <item id='4'><description>Armchair</description> <condition>bad</condition></item>" + "   </xu:append>" + XUPDATE_END;
        Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("condition") }, null, 2);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 4);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 6);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "bad", 1);
        assertEquals("bad", o[0].getTerm());
        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") }, "bad", 1);
        assertEquals("bad", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "armchair", 1);
        assertEquals("armchair", o[0].getTerm());
        // Insert before top element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']\">" + "           <item id='0'><description>Wheelchair</description> <condition>poor</condition></item>" + "       </xu:insert-before>" + 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("condition") }, null, 3);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 8);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "poor", 1);
        assertEquals("poor", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "wheelchair", 1);
        assertEquals("wheelchair", o[0].getTerm());
        // Insert after element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']\">" + "           <item id='1.1'><description>refrigerator</description> <condition>perfect</condition></item>" + "       </xu:insert-after>" + 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("condition") }, null, 4);
        checkIndex(docs, broker, new QName[] { new QName("description") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 10);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "perfect", 1);
        assertEquals("perfect", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("description") }, "refrigerator", 1);
        assertEquals("refrigerator", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "perfect", 1);
        assertEquals("perfect", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "refrigerator", 1);
        assertEquals("refrigerator", o[0].getTerm());
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-after select=\"//item[@id = '1']/description\">" + "           <condition>average</condition>" + "       </xu:insert-after>" + 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("condition") }, null, 5);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 11);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "average", 1);
        assertEquals("average", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "average", 1);
        assertEquals("average", o[0].getTerm());
        // Insert before nested element
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:insert-before select=\"//item[@id = '1']/description\">" + "           <condition>awesome</condition>" + "       </xu:insert-before>" + 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("condition") }, null, 6);
        checkIndex(docs, broker, new QName[] { new QName("item") }, null, 12);
        o = checkIndex(docs, broker, new QName[] { new QName("condition") }, "awesome", 1);
        assertEquals("awesome", o[0].getTerm());
        o = checkIndex(docs, broker, new QName[] { new QName("item") }, "awesome", 1);
        assertEquals("awesome", o[0].getTerm());
        // Overwrite attribute
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        xupdate = XUPDATE_START + "       <xu:append select=\"//item[@id = '1']\">" + "           <xu:attribute name=\"attr\">abc</xu:attribute>" + "       </xu:append>" + XUPDATE_END;
        modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
        QName[] qnattr = { new QName("attr", XMLConstants.NULL_NS_URI, XMLConstants.DEFAULT_NS_PREFIX, ElementValue.ATTRIBUTE) };
        o = checkIndex(docs, broker, qnattr, null, 1);
        assertEquals("abc", o[0].getTerm());
        checkIndex(docs, broker, qnattr, "attribute", 0);
        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 83 with XQuery

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

the class LuceneIndexTest method simpleQueries.

@Test
public void simpleQueries() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG1, XML1, "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);
        final Occurrences[] o = checkIndex(docs, broker, new QName[] { new QName("p") }, "with", 1);
        assertEquals(2, o[0].getOccurrences());
        checkIndex(docs, broker, new QName[] { new QName("hi") }, "just", 1);
        checkIndex(docs, broker, null, "in", 1);
        final QName attrQN = new QName("rend", XMLConstants.NULL_NS_URI, ElementValue.ATTRIBUTE);
        checkIndex(docs, broker, new QName[] { attrQN }, null, 2);
        checkIndex(docs, broker, new QName[] { attrQN }, "center", 1);
        checkIndex(docs, broker, new QName[] { attrQN }, "right", 1);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "/section[ft:query(p, 'content')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(p/@rend, 'center')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(hi, 'just')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(p/*, 'just')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/section[ft:query(head/*, 'just')]", 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)

Example 84 with XQuery

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

the class LuceneIndexTest method moreElaborateQueries.

@Test
public void moreElaborateQueries() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final String XML10 = // xmlns=\"http://www.tei-c.org/ns/1.0\">\n" +
    "<TEI>\n" + "   <teiHeader>\n" + // this should get indexed
    "      <title type='t' xml:lang=\"Sa-Ltn\">       Buick             </title> \n" + // this should not get indexed -- attribute name ns does not match
    "      <title type='t'     lang=\"Sa-Ltn\">       Cadillac          </title> \n" + // this should not get indexed -- attribute value does not match
    "      <title type='t' xml:lang=\"En\"    >       Dodge             </title> \n" + // this should not get indexed -- attribute is entirely missing
    "      <title type='t'                    >       Ford              </title> \n" + // this should get indexed
    "      <title type='t' xml:lang=\"Sa-Ltn\"> <tag> ABuick    </tag>  </title> \n" + // this should not get indexed -- attribute name ns does not match
    "      <title type='t'     lang=\"Sa-Ltn\"> <tag> ACadillac </tag>  </title> \n" + // this should not get indexed -- attribute value does not match
    "      <title type='t' xml:lang=\"En\"    > <tag> ADodge    </tag>  </title> \n" + // this should not get indexed -- attribute is entirely missing
    "      <title type='t'                    > <tag> AFord     </tag>  </title> \n" + "   </teiHeader>\n" + "   <text>\n" + "       <group>\n" + "           <text>Nested</text>\n" + "       </group>\n" + "   </text>\n" + "</TEI>";
    final String COLLECTION_CONFIG10 = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\">\n" + "    <index>\n" + "        <!-- Lucene indexes -->\n" + "        <lucene diacritics='no'>\n" + "            <analyzer class='org.apache.lucene.analysis.standard.StandardAnalyzer'/>\n" + "            <text match=\"//title[@xml:lang='Sa-Ltn']\"/>\n" + "            <text match=\"/TEI/text\"><ignore qname=\"text\"/></text>\n" + "            <text field=\"not-equals-Sa-Ltn\" match=\"//title[@xml:lang != 'Sa-Ltn']\"/>\n" + "            <text field=\"ne-Sa-Ltn\" match=\"//title[fn:not(@xml:lang='Sa-Ltn')]\"/>\n" + "            <text field=\"eq-Sa-Ltn\" match=\"//title[@xml:lang eq 'Sa-Ltn']\"/>\n" + "        </lucene> \n" + "    </index>\n" + "</collection>";
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG10, XML10, "test.xml");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        // unbeknownst to me, the test on the next line fails if the literal "buick" is replaced with "Buick":
        checkIndex(docs, broker, new QName[] { new QName("title") }, "buick", 1);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "cadillac", 0);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "dodge", 0);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "ford", 0);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "abuick", 1);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "acadillac", 0);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "adodge", 0);
        checkIndex(docs, broker, new QName[] { new QName("title") }, "aford", 0);
        // nested <text> should be ignored and not indexed by match="/TEI/text"
        checkIndex(docs, broker, new QName[] { new QName("text") }, "nested", 0);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        String[] searchTerms = { "Buick", "Cadillac", "Dodge", "Ford", "ABuick", "ACadillac", "ADodge", "AFord" };
        String[] queryTemplates = { "//.[ft:query(title, '%s')]", // Field query with != attribute predicate: <text field="non-En" match="//title[@xml:lang != 'En']"/>:
        "//.[ft:query-field('not-equals-Sa-Ltn', '%s')]", // Field query with 'ne' attribute predicate: <text field="non-En" match="//title[@xml:lang ne 'En']"/>:
        "//.[ft:query-field('ne-Sa-Ltn', '%s')]", // Field query with 'eq' attribute predicate: <text field="non-En" match="//title[@xml:lang ne 'En']"/>:
        "//.[ft:query-field('eq-Sa-Ltn', '%s')]" };
        int[][] resultCounts = { { 1, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 1 }, { 1, 0, 0, 0 } };
        for (int qi = 0; qi < queryTemplates.length; ++qi) {
            int[] resultCount = resultCounts[qi];
            for (int ri = 0; ri < searchTerms.length; ++ri) {
                String query = String.format(queryTemplates[qi], searchTerms[ri]);
                Sequence seq = xquery.execute(broker, query, null);
                assertNotNull(seq);
                int expected = resultCount[ri % resultCount.length];
                assertEquals(query, expected, 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)

Example 85 with XQuery

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

the class MatchListenerTest method constructedNodes.

@Test
public void constructedNodes() throws PermissionDeniedException, XPathException, SAXException, IOException, XpathException, CollectionConfigurationException, LockException, EXistException {
    configureAndStore(CONF3, 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[] strings = new String[] { "龍", "龍護", "曰龍護", "名曰龍護" };
        for (int i = 0; i < strings.length; i++) {
            final Sequence seq = xquery.execute(broker, "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";\n" + "for $para in //tei:p[ngram:contains(., '" + strings[i] + "')]\n" + "return\n" + "   <match>{$para}</match>", null);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            final String result = queryResult2String(broker, seq, 0);
            XMLAssert.assertXpathEvaluatesTo(i < 2 ? "2" : "1", "count(//exist:match)", result);
            XMLAssert.assertXpathExists("//exist:match[text() = '" + strings[i] + "']", 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)

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