Search this site:
Enterprise Search Blog
« NIE Newsletter

Command Line K2 using Windows Scripting

Last Updated Mar 2009

By: Mark Bennett & Miles Kehoe - NIE Enterprise Search - Issue 1 - April 25, 2003

The Verity K2 API supports both ASP and JSP scripting because the low-level Java calls are wrapped as COM objects. This lets developers access the various objects - VSearch being among the most useful - to perform searches and display results.

For several years now, Microsoft has shipped a tool known as the Windows Script Host, or WSH, to provide command-line programming of various Windows capabilities. WSH does not replace batch file programming, but it goes far beyond the capabilities of traditional batch file scripting by providing access to COM and ActiveX objects. This allows developers and system administrators to easily script many of the administrative tasks involved in running a Windows site.

While Verity does not support K2 access via WSH, it does require VBScript be installed for the K2 Dashboard and some of the WebTop elements. This article shows you how to access K2 and perform very simple searches using WSH - running as a command-line application.

Why Not Use rcvdk or rck2?

Verity provides handy command line query tools - rcvdk and rck2 among others. They are great tools for verifying a collection, a search, and even field contents. But it isn't really a useful command line tool for batch applications. Using WSH, you can write custom applications, perform searches or system status calls, and re-direct output to files. Combined with a scheduling tool like the Windows AT command, you can even schedule search applications to run periodically. For example, you can schedule a script to run every night after indexing to confirm the number of documents in a collection, or even take a collection offline for archiving.

Caveats

First, this program is an example of very simple search. Even if you ignore the fact that WSH is not a supported environment for K2, the code here does no error checking which should be part of every production program. This script also "hard codes" the collection name - "doccoll" - and the user query - "vsearch" - not very useful for most real applications!

Also, because WSH has no non-dialog-box I/O, the program opens the console as CON: for output. This means you cannot redirect output of the program to a file, which might be useful in most applications.

Still, this will serve as a reasonable example of what sorts of things you can do with WSH and the Verity VSearch object.

Running the Programming

Copy the contents of the script below into a text file on a system that has K2 installed; and save it as simpleK2.vbs. Make sure there is a collection called doccoll - the standard Verity documentation collection - on your K2 server or broker.

Open a command windows - Start | Run | cmd. Change to the directory where you saved the program.

From the command line, start the program using the command like interface to WSH, cscript.exe:

cscript simpleK2.vbs You should see a display of the first 10 results from the Verity doccoll, including a result number, a title, and a date.

Note there is a windowed version of the script engine called wscript.exe, but because the program uses console output, you need to run this under the command line engine.

That's it!

' --------------------------------------------
' Name: simpleK2.vbs : simple search using wsh
' Command line searching using windows scripting
' Usage: cscript simpleK2.vbs  
' Provided by New Idea Engineering Inc (http://www.ideaeng.com)

' See notes in accompanying text
' Specifically regarding error checking and collection names

' define objects and variables
Dim fso
Dim fs
Dim outFile
Dim forWrite
Dim maxDocs
Dim collName
Dim queryText
Dim docTitle
Dim startDoc
Dim s
Dim count
Dim res
Dim docSet

forWrite = 2 

collName = "doccoll"
queryText = "vsearch"
startDoc = 1
maxDocs = 10

'
' open the console for output since wsh has no print/write statements
'
outFile="con:"
set fso = CreateObject("Scripting.FileSystemObject")
set fs = fso.OpenTextFile(outFile,forWrite)


' create the vsearch object
set s = CreateObject("Verity.VSearch")

' set some properties
s.addCollection(collName)
s.setQueryText(queryText)
s.setDocsStart(startDoc)
s.setMaxDocCount(maxDocs)
s.addField("Title")
s.addField("Date")

' do the actual search
'     should check error status after call
set res = s.getResult()

' and get some stats from the search
docsFound = res.docsFound
docsSearched = res.docsSearched
fs.writeline("Found " & CStr(docsFound) & " of " & CStr(docsSearched))

' create the result object
set docSet =  res.documents

' and iterate thru the results
count=1
fs.writeline("DocNum  Title")
for each doc in docSet
    docKey = doc.field("vdkvgwkey")
    docTitle = doc.field("Title")
    docTitle = Trim(docTitle)
    if len(docTitle) < 1 then
        docTitle = docKey
    end if
    docDate = doc.field("Date")
    fs.writeline(CStr(count) & "  " & docTitle & "  " & docDate )
    count = count + 1
next

' cleanup and close objects
set fso=Nothing
fs.close()

' --------------------------------------------