Database Reference
In-Depth Information
Searching the Plays
The next two enhancements add search functionality to our little play application.
One uses straight XQuery (actually as an example of how not to do it), and the other
uses eXist's full-text indexing capabilities.
Searching Using Straight XQuery
Let's assume we need to search the plays for certain words or phrases. A first naive
approach could be to use just straight XQuery. The searching is not hard at all. For
instance, try the following surprisingly short code snippet, which searches all the
plays for the word fantasy (and adds a play attribute to tell us which play the results
came from):
for $ line in
collection ( '/db/apps/exist101/data' )// SPEECH / LINE [ contains (., 'fantasy' )]
return
<LINE play = "{ base-uri ( $ line )}" > { string ( $ line )} </LINE>
This shows that Hamlet is three times as fantastic as Romeo and Juliet , and there's no
fantasy in Macbeth (for the Shakespearians: that was a joke):
<LINE play= "/db/apps/exist101/data/hamlet.xml" >
Horatio says 'tis but our fantasy, </LINE>
<LINE play= "/db/apps/exist101/data/hamlet.xml" >
Is not this something more than fantasy? </LINE>
<LINE play= "/db/apps/exist101/data/hamlet.xml" >
That, for a fantasy and trick of fame, </LINE>
<LINE play= "/db/apps/exist101/data/r%20and%20j.xml" >
Begot of nothing but vain fantasy, </LINE>
Although the searching itself is simple to program, we do need some code to tie it all
together. First, add a search form to the main page. Reopen plays-home.xq
( Example 3-6 ) and add the following lines between the </ul> and </body> closing
elements:
<br/>
<h3> Search using XQuery: </h3>
<form method= "POST" action= "search-1.xq" >
<input type= "text" name= "searchphrase" size= "40" />
<input type= "submit" value= "Search!" />
</form>
When you reopen the page in your browser ( http://localhost:8080/exist/rest/db/apps/
exist101/plays-home.xq ) you'll see a nice little search form with a Search! button.
Now we have to create an XQuery script that does something with the search request
and displays the results. Open a new XQuery file in eXide, save it as search-1.xq , and
enter the code shown in Example 3-8 .
Search WWH ::




Custom Search