Checking Search Collection Status

This example application enqueues a document (a set of Watson™ Explorer Engine content elements) to a search collection, which means that the specified search collection must already exist before attempting the enqueue. The following block of code therefore first creates an object in which the status of the search collection is returned, adds the pre-defined authentication object to it, and sets the name of the search collection about which we are requesting status information. It then uses the port that we created earlier to call the search collection status function, passing the status object as a parameter. All of this is shown in the following Java code example:

    SearchCollectionStatus scs = new SearchCollectionStatus();
    scs.setAuthentication(authentication);
    scs.setCollection(collection);
    if (verbose) System.out.println(" done.");
    SearchCollectionStatusResponse scsr;
    VseStatus vses;
    VseIndexStatus vseis;
    CrawlerStatus cs;
    try {
        if (verbose) System.out.print("Request status of ["+collection+"]...");
        scsr = vp.searchCollectionStatus(scs);
        if (verbose) System.out.println(" done.");
        vses = scsr.getVseStatus();
        vseis = vses.getVseIndexStatus();
        cs = vses.getCrawlerStatus();
        System.out.println("This collection's crawler is ["+cs.getServiceStatus()+"].");
        System.out.println("This collection's indexer is ["+vseis.getServiceStatus()+"].");
    } catch (javax.xml.ws.soap.SOAPFaultException e) {
        if (e.getFault().getFaultString().contains("search-collection-invalid-name") ) {
    	System.out.println("\n\n\tWARNING: collection ["+collection+"] does not exist.");
    	System.out.print("\tWARNING: create ["+collection+"] based on [default-push]...");
    	/* [default-push] is the default based-on collection when creating a new collection via the api */
    	SearchCollectionCreate scc = new SearchCollectionCreate();
    	scc.setAuthentication(authentication);
    	scc.setCollection(collection);
    	vp.searchCollectionCreate(scc);
    	System.out.println(" done.");
        } else {
    	System.out.println("\n\n\tWARNING: "+e);
    	System.out.println("\nproceed anyway...");
        }
    }

The use of the search-collection-status call in this example is substantially different than that in the previous sample application (which was discussed in Checking Watson Explorer Engine Status). In the previous example, where the collection was pulling data by crawling files on a remote host, the collection was assumed to exist. Since the goal was to start the crawling process, we tested for a null return from the status call to determine whether the collection actually had any status information (in other words, if any services were running) and, if so, what services those were. If the crawler for that collection was already running, the sample application couldn't proceed, because one of the things that the previous sample application was designed to illustrate was how to start a specific service.

In this example, we don't actually care about any status information that is returned (other than so that we can print it out) - the real point of the status call in this example is to determine whether the search collection already exists and to create it, if not. As you can see from the preceding block of code, we therefore simply make the status call without testing the return value because requesting status from a collection that does not exist raises an exception, and the exception handler for the call (the catch body) creates the collection. If the call (the try block) actually succeeds, we display whatever status is returned using pointers to pre-declared search collection status (VseStatus) and search collection index status (VseIndexStatus) objects and use these to extract and displays the collection, index service, and crawler service status information.

The equivalent C# code for this operation is the following:

    SearchCollectionStatus scs = new SearchCollectionStatus();
    scs.authentication = auth;
    scs.collection = collection;
    if (verbose) Console.WriteLine(" done.");
    try
    {
        if (verbose) Console.Write("Request status of [" + collection + "]...");
        scsr = vpc.SearchCollectionStatus(scs);
        if (verbose) Console.WriteLine(" done.");
        if (scsr == null)
        {
            /* either this collection has never been started or has no data */
            if (verbose) Console.Write("Collection [" + collection + "] has no status.");
        }
        else
        {
            vses = scsr.vsestatus;
            vseis = vses.vseindexstatus;
            cs = vses.crawlerstatus;
            if (verbose) Console.Write("This collection's crawler is [" + cs.servicestatus.ToString() + "].");
            if (verbose) Console.Write("This collection's indexer is [" + vses.vseindexstatus.ToString() + "].");
        }
    }
    catch (Exception e)
    {
        if (e.Message.Contains("search-collection-invalid-name"))
        {
            Console.Out.WriteLine("\n\n\tWARNING: collection [" + collection + "] does not exist.");
            Console.Out.Write("\tWARNING: create [" + collection + "] based on [default-push]...");
            /* [default-push] is the default based-on collection when creating a new collection via the api */
            SearchCollectionCreate scc = new SearchCollectionCreate();
            scc.authentication = auth;
            scc.collection = collection;
            vpc.SearchCollectionCreate(scc);
            Console.Out.WriteLine(" done.");
        }
        else
        {
            Console.Out.WriteLine("\n\n\tWARNING: " + e);
            Console.Out.WriteLine("\nproceed anyway...");
        }
        throw e;
    }