Lucene queries and kwic summary outputs
Posted by mholmes on 14 Oct 2011 in Activity log
There don't seem to be any really good examples of how to search a document collection and display kwic summaries of hits grouped by document, so it took me a little puzzling to figure this one out. Most of the examples show processing only of the first hit in a document. Here's my sample code:
xquery version "1.0"; declare default element namespace "http://www.tei-c.org/ns/1.0"; declare namespace tei="http://www.tei-c.org/ns/1.0"; declare namespace ft="http://exist-db.org/xquery/lucene"; import module namespace kwic="http://exist-db.org/xquery/kwic"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace exist="http://exist.sourceforge.net/NS/exist"; declare namespace xh="http://www.w3.org/1999/xhtml"; for $hit in //TEI/text[ft:query(., 'henslowe')] let $expanded := kwic:expand($hit), $config := <config xmlns="" width="40"/>, $t := $hit/ancestor::TEI order by ft:score($hit) descending return <xh:div> <xh:h3><xh:a href="{xs:string($t/@xml:id)}.htm">{$t/teiHeader/fileDesc/titleStmt/title[1]/text()}</xh:a></xh:h3> { for $h in $expanded//exist:match return kwic:get-summary($expanded, $h, $config) } </xh:div>