Search in sources :

Example 71 with QName

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

the class NGramSearch method analyze.

/*
     * (non-Javadoc)
     *
    * @see org.exist.xquery.PathExpr#analyze(org.exist.xquery.Expression)
    */
@Override
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
    super.analyze(contextInfo);
    List<LocationStep> steps = BasicExpressionVisitor.findLocationSteps(getArgument(0));
    if (!steps.isEmpty()) {
        LocationStep firstStep = steps.get(0);
        LocationStep lastStep = steps.get(steps.size() - 1);
        if (firstStep != null && steps.size() == 1 && firstStep.getAxis() == Constants.SELF_AXIS) {
            Expression outerExpr = contextInfo.getContextStep();
            if (outerExpr != null && outerExpr instanceof LocationStep) {
                LocationStep outerStep = (LocationStep) outerExpr;
                NodeTest test = outerStep.getTest();
                if (!test.isWildcardTest() && test.getName() != null) {
                    if (outerStep.getAxis() == Constants.ATTRIBUTE_AXIS || outerStep.getAxis() == Constants.DESCENDANT_ATTRIBUTE_AXIS) {
                        contextQName = new QName(test.getName(), ElementValue.ATTRIBUTE);
                    } else {
                        contextQName = new QName(test.getName());
                    }
                    contextStep = firstStep;
                    axis = outerStep.getAxis();
                    optimizeSelf = true;
                }
            }
        } else if (lastStep != null && firstStep != null) {
            NodeTest test = lastStep.getTest();
            if (!test.isWildcardTest() && test.getName() != null) {
                if (lastStep.getAxis() == Constants.ATTRIBUTE_AXIS || lastStep.getAxis() == Constants.DESCENDANT_ATTRIBUTE_AXIS) {
                    contextQName = new QName(test.getName(), ElementValue.ATTRIBUTE);
                } else {
                    contextQName = new QName(test.getName());
                }
                axis = firstStep.getAxis();
                optimizeChild = steps.size() == 1 && (axis == Constants.CHILD_AXIS || axis == Constants.ATTRIBUTE_AXIS);
                contextStep = lastStep;
            }
        }
    }
}
Also used : EvaluatableExpression(org.exist.xquery.modules.ngram.query.EvaluatableExpression) EmptyExpression(org.exist.xquery.modules.ngram.query.EmptyExpression) WildcardedExpression(org.exist.xquery.modules.ngram.query.WildcardedExpression) QName(org.exist.dom.QName)

Example 72 with QName

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

the class NGramSearch method processMatches.

private NodeSet processMatches(NGramIndexWorker index, DocumentSet docs, List<QName> qnames, String query, NodeSet nodeSet, int axis) throws XPathException {
    EvaluatableExpression parsedQuery = null;
    if (getLocalName().equals("wildcard-contains"))
        parsedQuery = parseQuery(query);
    else
        parsedQuery = new FixedString(this, query);
    LOG.debug("Parsed Query: {}", parsedQuery);
    NodeSet result = parsedQuery.eval(index, docs, qnames, nodeSet, axis, this.getExpressionId());
    if (getLocalName().startsWith("starts-with"))
        result = NodeSets.getNodesMatchingAtStart(result, getExpressionId());
    else if (getLocalName().startsWith("ends-with"))
        result = NodeSets.getNodesMatchingAtEnd(result, getExpressionId());
    result = NodeSets.transformNodes(result, proxy -> NodeProxies.transformOwnMatches(proxy, Match::filterOutOverlappingOffsets, getExpressionId()));
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) Match(org.exist.dom.persistent.Match) EvaluatableExpression(org.exist.xquery.modules.ngram.query.EvaluatableExpression) java.util(java.util) QName(org.exist.dom.QName) NodeProxy(org.exist.dom.persistent.NodeProxy) org.exist.xquery.value(org.exist.xquery.value) Wildcard(org.exist.xquery.modules.ngram.query.Wildcard) NodeSet(org.exist.dom.persistent.NodeSet) EmptyExpression(org.exist.xquery.modules.ngram.query.EmptyExpression) org.exist.xquery(org.exist.xquery) NodeProxies(org.exist.xquery.modules.ngram.utils.NodeProxies) NGramIndex(org.exist.indexing.ngram.NGramIndex) Matcher(java.util.regex.Matcher) NodeSets(org.exist.xquery.modules.ngram.utils.NodeSets) ElementValue(org.exist.storage.ElementValue) Error(org.exist.xquery.util.Error) DocumentSet(org.exist.dom.persistent.DocumentSet) AlternativeStrings(org.exist.xquery.modules.ngram.query.AlternativeStrings) StartAnchor(org.exist.xquery.modules.ngram.query.StartAnchor) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) NGramIndexWorker(org.exist.indexing.ngram.NGramIndexWorker) Logger(org.apache.logging.log4j.Logger) FixedString(org.exist.xquery.modules.ngram.query.FixedString) EndAnchor(org.exist.xquery.modules.ngram.query.EndAnchor) WildcardedExpressionSequence(org.exist.xquery.modules.ngram.query.WildcardedExpressionSequence) Pattern(java.util.regex.Pattern) WildcardedExpression(org.exist.xquery.modules.ngram.query.WildcardedExpression) LogManager(org.apache.logging.log4j.LogManager) EvaluatableExpression(org.exist.xquery.modules.ngram.query.EvaluatableExpression) FixedString(org.exist.xquery.modules.ngram.query.FixedString) Match(org.exist.dom.persistent.Match)

Example 73 with QName

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

the class Query method analyze.

/* (non-Javadoc)
    * @see org.exist.xquery.PathExpr#analyze(org.exist.xquery.Expression)
    */
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
    super.analyze(new AnalyzeContextInfo(contextInfo));
    List<LocationStep> steps = BasicExpressionVisitor.findLocationSteps(getArgument(0));
    if (!steps.isEmpty()) {
        LocationStep firstStep = steps.get(0);
        LocationStep lastStep = steps.get(steps.size() - 1);
        if (firstStep != null && steps.size() == 1 && firstStep.getAxis() == Constants.SELF_AXIS) {
            Expression outerExpr = contextInfo.getContextStep();
            if (outerExpr != null && outerExpr instanceof LocationStep) {
                LocationStep outerStep = (LocationStep) outerExpr;
                NodeTest test = outerStep.getTest();
                final byte contextQNameType;
                if (outerStep.getAxis() == Constants.ATTRIBUTE_AXIS || outerStep.getAxis() == Constants.DESCENDANT_ATTRIBUTE_AXIS) {
                    contextQNameType = ElementValue.ATTRIBUTE;
                } else {
                    contextQNameType = ElementValue.ELEMENT;
                }
                if (test.getName() == null) {
                    contextQName = new QName(null, null, contextQNameType);
                } else {
                    contextQName = new QName(test.getName(), contextQNameType);
                }
                contextStep = firstStep;
                axis = outerStep.getAxis();
                optimizeSelf = true;
            }
        } else if (lastStep != null && firstStep != null) {
            NodeTest test = lastStep.getTest();
            if (test.getName() == null)
                contextQName = new QName(null, null, null);
            else if (test.isWildcardTest())
                contextQName = test.getName();
            else if (lastStep.getAxis() == Constants.ATTRIBUTE_AXIS || lastStep.getAxis() == Constants.DESCENDANT_ATTRIBUTE_AXIS) {
                contextQName = new QName(test.getName(), ElementValue.ATTRIBUTE);
            } else {
                contextQName = new QName(test.getName());
            }
            axis = firstStep.getAxis();
            optimizeChild = steps.size() == 1 && (axis == Constants.CHILD_AXIS || axis == Constants.ATTRIBUTE_AXIS);
            contextStep = lastStep;
        }
    }
}
Also used : QName(org.exist.dom.QName)

Example 74 with QName

use of org.exist.dom.QName 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 75 with QName

use of org.exist.dom.QName 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

QName (org.exist.dom.QName)271 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)18 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10