XQuery/Reindex a Collection

MotivationEdit

You would like a command line function that will reindex a collection.

MethodEdit

We will use an Apache Ant task that will call an XQuery script that will run the reindex XQuery function.

Sample Ant TargetEdit

<target name="reindex-faqs">
    <property name="server-uri" value="xmldb:exist://localhost:8080/xmlrpc"/>
    <property name="collection" value="/db/dan/apps/faqs/data"/>
    <echo message="Reindexing collection ${collection}"/>
    <echo message="on server-uri = ${server-uri}"/>
    <xdb:xquery uri="${server-uri}/db"  
         user="admin" password="myadminpw"
         outputproperty="result">
            xquery version "1.0";
 
            let $start-time := util:system-time()
            let $reindex := xmldb:reindex('/db/dan/apps/faqs/data')
            let $end-time := util:system-time()
            let $runtimems := (($end-time - $start-time) div xs:dayTimeDuration('PT1S'))  * 1000 
            return $runtimems
     </xdb:xquery>
     <echo message="Reindex performed in = ${result} milliseconds."/>
</target>

Running your Ant TargetEdit

To run the command just use the ant command

  $ant reindex-faqs
  Buildfile: build.xml
  reindex-faqs:
    [echo] Reindexing collection /db/dan/apps/faqs/data
    [echo] test server = xmldb:exist://localhost/xmlrpc
  [xdb:xquery] Database driver registered.
  [xdb:xquery] Found 1 results
  [echo] Reindex performed in = 203 milliseconds.
  BUILD SUCCESSFUL
  Total time: 1 second

DiscussionEdit

This should work both under Windows and UNIX systems.

Last modified on 14 March 2012, at 19:22