MultiQuerySearch.cs sample program

Query that searches in the combined results of two previously run queries.

/**
 * Title: MultiQuerySearch.cs - a sample that searches in a subset of a docbase.
 *
 * Description:  This sample shows how to use a query to search for documents
 *               in the combined results of two queries that were run previously.
 *
 * Syntax:       DoSearch       user=<domain\\user>
 *                              password=<password>
 *                              server=<ServerName>
 *                              docbase=<docBaseName>
 *                              queryfile1=<queryFileName1>
 *                              queryfile2=<queryFileName2>
 *                              queryfile3=<queryFileName3>
 *
 * Copyright:    Copyright (c) 2003, 2010
 * Company:      Ixiasoft Technologies Inc.
 * @author       Pierre Bisaillon
 * @version      2.0
 * Modified:     2011-02-10
 */

using Ixiasoft.TextmlServer41;
using System;
using System.Threading;
using System.Text;
using System.Collections;
using System.IO;

public class SearchDocuments
{

    static String TOKEN_USER       = "USER";
    static String TOKEN_PASSWORD   = "PASSWORD";
    static String TOKEN_SERVER     = "SERVER";
    static String TOKEN_DOCBASE    = "DOCBASE";
    static String TOKEN_QUERYFILE1  = "QUERYFILE1";
    static String TOKEN_QUERYFILE2  = "QUERYFILE2";
    static String TOKEN_QUERYFILE3  = "QUERYFILE3";

    static String[] validTokens =
        { TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE,
          TOKEN_QUERYFILE1, TOKEN_QUERYFILE2, TOKEN_QUERYFILE3 };

    static bool[] mandatory =
        { true      , true          , true        , true         ,
          true      , true          , true };


    private static void Usage()
    {
        Console.WriteLine
            ("MultiQuerySearch user=<domain\\user> password=<password> " +
             "server=<serverName> docbase=<docBaseName> " +
             "queryfile1=<[path\\]queryFileName1> " +
             "queryfile2=<[path\\]queryFileName2> " +
             "queryfile3=<[path\\]queryFileName3>");

        Console.WriteLine
            ("\t<domain\\user>  Name of the user used for security purpose");
        Console.WriteLine
            ("\t<password>      Password of the user");
        Console.WriteLine
            ("\t<ServerName>    Name of TEXTML Server instance");
        Console.WriteLine
            ("\t<[path\\]queryFileName1>  First query searching docbase");
        Console.WriteLine
            ("\t<[path\\]queryFileName2>  Second query searching docbase");
        Console.WriteLine
            ("\t<[path\\]queryFileName3>  " +
             "Query searching combined ResultSpaces of first two queries");
    }


    // Extract run-time parameters from command-line
        private static Hashtable Extract(String[] args)
        {
            Hashtable retval = new Hashtable(10);

            for (int i = 0; i < args.Length; ++i)
            {
                String[] tokens = args[i].Split('=');
                String token = null, value = null;

                if (tokens.Length > 1)
                    token = tokens[0];
                if (tokens.Length > 1)
                    value = tokens[1];

                if (token == null || value == null)
                {
                    retval.Clear();
                    return retval;
                }

                bool found = false;
                for (int j = 0; j < validTokens.Length && !found; ++j)
                {
                    if (validTokens[j].CompareTo(token.ToUpperInvariant()) == 0 &&
                        !retval.ContainsKey(validTokens[j]))
                    {
                        retval.Add(validTokens[j], value);
                        found = true;
                    }
                }

                if (!found)
                {
                    retval.Clear();
                    return retval;
                }
            }

            for (int i = 0; i < validTokens.Length; ++i)
            {
                if (mandatory[i] && !retval.ContainsKey(validTokens[i]))
                {
                    retval.Clear();
                    return retval;
                }
            }

            return retval;
        }


    // Read a query file, and return its contents as a String object
    private static String ReadQuery(String fileName)
    {
        FileStream stream = new FileStream(fileName,FileMode.Open);
        StreamReader reader = new StreamReader(stream, Encoding.Unicode);

        StringBuilder buff = new StringBuilder((int)stream.Length);

        try
        {
            while ( !reader.EndOfStream )
                buff.Append((char)reader.Read());
        }
        finally
        {
            reader.Close();
        }

        return buff.ToString();
    }


    //*** This function is never used ***
    private static char[] ReadFile(String file)
    {
        System.IO.StreamReader f = new System.IO.StreamReader(file);
        char[] retval = new char[f.BaseStream.Length];


        try
        {
            f.Read(retval, 0, (int)f.BaseStream.Length);
        }
        finally
        {
            f.Close();
        }

        return retval;
    }


  static void Main(string[] args)
    {
        String user = "";

        // Parse the command line

        Hashtable map = Extract(args);

        // Validate the command-line parameters

        if (map.Count == 0)
        {
            Usage();
            return;
        }

        if (map.ContainsKey(TOKEN_USER))
            user = (String)map[TOKEN_USER];

        if (user.IndexOf("\\") == -1)
        {
            Usage();
            return;
        }

        // Prepare parameters for ClientServicesFactory
        Hashtable parms = new Hashtable(1);

        try
        {
            // Get the ClientServices object
            IxiaClientServices cs =
                Ixiasoft.TextmlServer41.ClientServicesFactory.getInstance();

            // extract domain (or machine-name) from <user>
            String domain = user.Substring(0, user.IndexOf("\\"));
            String userName = user.Substring(user.IndexOf("\\") + 1);

            // Prepare to login to the TEXTML Server instance
            cs.Login(domain, userName, (String)map[TOKEN_PASSWORD]);

            // Read the queries from the query files
            String query1 = ReadQuery((String)map[TOKEN_QUERYFILE1]);
            String query2 = ReadQuery((String)map[TOKEN_QUERYFILE2]);
            String query3 = ReadQuery((String)map[TOKEN_QUERYFILE3]);

            try
            {
                // Get the ServerServices for the specified server
                IxiaServerServices ss =
                    cs.ConnectServer((String) map[TOKEN_SERVER]);

                try
                {
// Get DocbaseServices object for the specified docbase
IxiaDocBaseServices docbase =
    ss.ConnectDocBase((String) map[TOKEN_DOCBASE]);

try
{
    // Get the SearchServices object for the docbase
    IxiaSearchServices search = docbase.SearchServices;

    try
    {
        // Display the three queries.
        Console.WriteLine();
        Console.WriteLine("Query1:");
        Console.WriteLine(query1);
        Console.WriteLine();
        Console.WriteLine("Query2:");
        Console.WriteLine(query2);
        Console.WriteLine();
        Console.WriteLine("Query3:");
        Console.WriteLine(query3);
        Console.WriteLine();
        Console.WriteLine();
 
        
        // Run <query1>, which searches the docbase and stores the result in
        // <result1>. Display the number of documents retrieved.
        IxiaResultSpace result1 = search.SearchDocuments(query1);
        Console.WriteLine("Query1 returned " + result1.Count + " documents.");
        Console.WriteLine();

        // Do the same for <query2> and <result2>.
        IxiaResultSpace result2 = search.SearchDocuments(query2);
        Console.WriteLine("Query2 returned " + result2.Count + " documents.");
        Console.WriteLine();

        // Run <query3>, which searches <result1> and <result2>. Store result
        // in <result3> . Display the number of documents retrieved.
        IxiaResultSpace result3 = search.SearchDocuments(query3);
        Console.WriteLine("Query3 returned " + result3.Count + " documents" +
                           " from Query1 and Query2.");

        result1.Release();
        result2.Release();
        result3.Release();
     }
     finally
     {
         search.Release();
     }
}
finally
{
    docbase.Release();
}
                }
                finally
                {
                    ss.Release();
                }
            }
            finally
            {
                // Don't forget to logout
                cs.Logout();
                Console.WriteLine();
                Console.WriteLine("Press any key");
                Console.ReadKey();

            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception occurred: " + e.Message);

        }

    }
}