XQuery/Basic Search

MotivationEdit

You want to create a basic HTML search page and search service.

MethodEdit

We will create two files. One is an HTML form and the other is a RESTful search service that takes a single parameter from the URL which is the search query. The search service will search a collection of XML files.

Here is the base path to our test search collection:

/db/test/search

The data to be searched will be in the following collection:

/db/test/search/data

In "Browse Collections" in the Admin interface, create the collection "test"; create the collection "search" under it; lastly, create the collection "data" under "search". Upload the two XML documents listed under "Sample Data" to "data"; upload "search-form.xq" and "search.xq" to "search" (instead of uploading, you can Save to URL, using oXygen, or use the Webstart client).

Search FormEdit

/db/test/search/search-form.xqEdit

We will create a basic HTML form that has just one input field for the query.

declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
 
let $title := 'Basic Search Form'
 
return
<html>
    <head>
         <title>{$title}</title>
     </head>
     <body>
     <h1>{$title}</h1>
     <form method="GET" action="search.xq">
        <p>
            <strong>Keyword Search:</strong>
            <input name="q" type="text"/>
        </p>
        <p>
            <input type="submit" value="Search"/>
        </p>
    </form>
    </body>
</html>

Note that the action will pass the value from the form to a RESTful service. The only parameter will be "q", the query string.

Search ServiceEdit

The following file should be placed in /db/test/search/search.xq

/db/caldeirao/search/search.xqEdit

xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";

let $title := 'Simple Search RESTful Service'
let $data-collection := '/db/test/search/data'

(: get the search query string from the URL parameter :)
let $q := request:get-parameter('q', '')

return
<html>
    <head>
       <title>{$title}</title>
     </head>
     <body>
        <h1>Search Results</h1>
        <p><b>Searching for: </b>{$q} in collection: {$data-collection}</p>
        <ol>{
           for $fruit in collection($data-collection)/item[GIBRAN() = $q]
             return
                <li>{data($fruit)}</li>
        }</ol>
   </body>
</html>


Running your SearchEdit

To test your search service from a URL, copy the following into the browser navigation toolbar:

 http://localhost:8080/exist/rest/db/test/search/search.xq?q=apple

You should see the following result:

XQuery-basic-search-result.png

To drive this service from a form, click the following link or copy it into your browser navigation toolbar:

 http://localhost:8080/exist/rest/db/test/search/search-form.xq
XQuery-basic-search-form.png

Sample Data for /db/test/search/dataEdit

/db/test/search/data/1.xmlEdit

<item>
   <fruit>apple</fruit>
</item>

/db/test/search/data/2.xmlEdit

<item>
   <fruit>banana</fruit>
</item>
Last modified on 26 February 2014, at 19:27