Simple expressions using the full-text index

In the following example queries, the entire set of Shakespeare plays and works in this demo are queried.

Find out where Juliet talks about love

declare namespace tei="http://www.tei-c.org/ns/1.0"; collection("/db/apps/shakespeare/data")//tei:sp[ft:query(., 'love')][tei:speaker = "Juliet"]

Search for a phrase

declare namespace tei="http://www.tei-c.org/ns/1.0"; collection("/db/apps/shakespeare/data")//tei:sp[ft:query(., '"fenny snake"')]

Find speeches in which "love" and "father" occur closely together, using XML query syntax:

declare namespace tei="http://www.tei-c.org/ns/1.0"; let $query := <query> <near slop="20"><term>love</term><near>father</near></near> </query> return collection("/db/apps/shakespeare/data")//tei:sp[ft:query(., $query)]

Find speeches in which "boil" and "bubble" occur, ordering them by full-text match score

declare namespace tei="http://www.tei-c.org/ns/1.0"; for $m in collection("/db/apps/shakespeare/data")//tei:sp[ft:query(., "boil bubble")] let $score := ft:score($m) order by $score descending return <m score="{$score}">{$m}</m>

Find speeches and group them by play

xquery version "3.0"; import module namespace kwic="http://exist-db.org/xquery/kwic"; declare namespace tei="http://www.tei-c.org/ns/1.0"; declare function local:search($query) { let $hits := //tei:sp[ft:query(., $query)] return local:display($hits) }; declare function local:display($hits) { for $speechInPlay in $hits group by $play := $speechInPlay/ancestor::tei:TEI/@xml:id return <div class="play"> <h1>{$play/../tei:teiHeader//tei:titleStmt/tei:title/text()}</h1> <ul> { for $speech in $speechInPlay return <li> <h4>{$speech/tei:speaker/text()}</h4> { kwic:summarize($speech, <config width="40" table="no"/>) } </li> } </ul> </div> }; let $query := "'fenny snake'" return local:search($query)