Search in sources :

Example 26 with DocumentSet

use of org.exist.dom.persistent.DocumentSet 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 27 with DocumentSet

use of org.exist.dom.persistent.DocumentSet 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 28 with DocumentSet

use of org.exist.dom.persistent.DocumentSet 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 29 with DocumentSet

use of org.exist.dom.persistent.DocumentSet 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 30 with DocumentSet

use of org.exist.dom.persistent.DocumentSet in project exist by eXist-db.

the class LuceneIndexTest method dropSingleDoc.

@Test
public void dropSingleDoc() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG1, XML1, "dropDocument.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()) {
        root.removeXMLResource(transaction, broker, XmldbURI.create("dropDocument.xml"));
        transact.commit(transaction);
        checkIndex(docs, broker, null, null, 0);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

DocumentSet (org.exist.dom.persistent.DocumentSet)50 QName (org.exist.dom.QName)20 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)18 Sequence (org.exist.xquery.value.Sequence)18 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)16 NodeSet (org.exist.dom.persistent.NodeSet)14 DBBroker (org.exist.storage.DBBroker)14 BrokerPool (org.exist.storage.BrokerPool)13 CompiledXQuery (org.exist.xquery.CompiledXQuery)12 XQuery (org.exist.xquery.XQuery)12 IOException (java.io.IOException)9 Txn (org.exist.storage.txn.Txn)9 TransactionManager (org.exist.storage.txn.TransactionManager)8 Test (org.junit.Test)7 DocumentImpl (org.exist.dom.persistent.DocumentImpl)6 InputSource (org.xml.sax.InputSource)6 StringReader (java.io.StringReader)5 LuceneIndexWorker (org.exist.indexing.lucene.LuceneIndexWorker)5 XPathException (org.exist.xquery.XPathException)5 VirtualNodeSet (org.exist.dom.persistent.VirtualNodeSet)4