(: will be rewritten to an empty sequence :)
/non-existing-name
</syntaxhighlightpre>
The contents of the name indexes can be directly accessed with the XQuery functions {{Function|Index|index:element-names}} and {{Function|Index|index:attribute-names}}.
(: ...will be rewritten to... :)
doc('factbook.xml')/mondial/country/province
</syntaxhighlightpre>
* The {{Code|fn:count}} function will be pre-evaluated by looking up the number in the index:
<pre lang='xquery'>
count(doc('factbook')//country)
</syntaxhighlightpre>
* The distinct values of elements or attributes can be looked up in the index as well:
<pre lang='xquery'>
distinct-values(db:get('factbook')//religions)
</syntaxhighlightpre>
The contents of the path index can be directly accessed with the XQuery function {{Function|Index|index:facets}}.
map { 'ftindex': true(), 'ftinclude': 'p div' }
)
</syntaxhighlightpre>
==Text Index==
where $c//city/name = 'Hanoi'
return $c/name
</syntaxhighlightpre>
Before the actual index rewriting takes places, some preliminary optimizations are applied:
map { 'updindex':true(), 'textindex': true(), 'textinclude':'id' }
)
</syntaxhighlightpre>
===Range Queries===
let $max := '2014-04-19T23:59:59'
return db:get('news')//entry[date-time > $min and date-time < $max]
</syntaxhighlightpre>
With {{Function|Database|db:text-range}}, you can access all text nodes whose values are between a minimum and maximum value.
(: 4th example :)
fn:id('f0_119', db:get('factbook'))
</syntaxhighlightpre>
''Attribute nodes'' (which you can use as starting points of navigation) can directly be retrieved from the index with the XQuery functions {{Function|Database|db:attribute}} and {{Function|Database|db:attribute-range}}. The index contents (''strings'') can be accessed with {{Function|Index|index:attributes}}.
(: 3rd example :)
doc('graph.xml')/idref('edge8')
</syntaxhighlightpre>
''Attribute nodes'' with a matching value (containing at least one from a set of given tokens) can be directly retrieved from the index with the XQuery function {{Function|Database|db:token}}. The index contents (''token strings'') can be accessed with {{Function|Index|index:tokens}}.
//religions[.//text() contains text { 'Catholic', 'Roman' }
using case insensitive distance at most 2 words]
</syntaxhighlightpre>
The index provides support for the following full-text features (the values can be changed in the GUI or via the {{Command|SET}} command):
<xquery> /text[. contains text { 'houses' } using no stemming] </xquery>
</commands>
</syntaxhighlightpre>
Text nodes can be directly requested from the index via the XQuery function {{Function|Full-Text|ft:search}}. The index contents can be accessed with {{Function|Full-Text|ft:tokens}}.
# Restore default
SET ATTRINCLUDE
</syntaxhighlightpre>
; XQuery
db:create('factbook', 'http://files.basex.org/xml/factbook.xml', '',
map { 'attrinclude': 'id,name' })
</syntaxhighlightpre>
With {{Command|CREATE INDEX}} and {{Function|Database|db:optimize}}, new selective indexing options will be applied to an existing database.
return db:get($db)//person[name/text() = 'John']
}
</syntaxhighlightpre>
* The following query contains two predicates that may both be rewritten for index access. If the automatically chosen rewriting is known not to be optimal, another index rewriting can enforced by surrounding the specific expression with the pragma:
}]
[religions/text() = 'Protestant']
</syntaxhighlightpre>
The option can also be assigned to predicates with dynamic values. In the following example, the comparison of the first comparison will be rewritten for index access. Without the pragma expression, the second comparison is preferred and chosen for the rewriting because the statically known string allows for an exact cost estimation:
where $country/religions/text() = 'Protestant'
return $country
</syntaxhighlightpre>
Please note that:
return db:create($db || '-index', $index, $db || '-index.xml')
</syntaxhighlightpre>
In the following query, a text string is searched, and the text nodes of the original database are retrieved:
for $id in db:get($db || '-index')//*[@string = $text]/id
return db:get-id($db, $id)/..
</syntaxhighlightpre>
With some extra effort, and if {{Option|UPDINDEX}} is enabled for both your original and your index database (see below), your index database will support updates as well (try it, it’s fun!).