Queries constrained to elements
26 August 2019 12:57 PM
In this Knowledgebase article, we will discuss a technique which will allow you to scope queries in such a way to ensure that they occur only within a parent element.
Consider a scenario where you have an XML document structured in this way:
And you want to find the document where where a
A search like
will give you the above document, but doesn't consider where the
will not return the above document since there is no
This applies to more-complicated subqueries too. For example, looking for a document that has a
will return the above document. But you may want these two child element-values both inside the same
This should give you expected results, as it won't return the above document since the two child element-value queries do not match inside the same
Filtering and indexes
Investigating a bit further, if you run the query with xdmp:query-meters you will see (depending on your database settings)
What is happening is that the query can only determine from the current indexes that there is a fragment with a
At scale you may find this filtering slow, or the query may hit Expanded Tree Cache limits if it retrieves many false positives to filter through.
If you have the correct positions enabled, the indexes can resolve this query without retrieving the document and filtering. In this case, after setting both
(To track element-value-queries inside element-queries you need
Now this query can be run without filtering. However, if you have a lot of relationship instances in a document, the calculations using positions can become quite expensive to compute.
Further details: Empty-element positions are problematic. Positions are word positions, and the position of an element is the word position of the first word after the element starts to the word position of the first word after the element ends. Positions of attributes are the positions of their element. If everything is an empty element, you have no words and everything has the same position and so positions cannot discriminate between elements.
Note that if you change these settings you will need to reindex your database, and the usual tradeoffs apply (larger indexes and slower indexing). Please see the following for guidance on adding an index and reindexing in general: