MultiQuerySearch.java sample program
Query that searches in the combined results of two previously run queries.
package com.ixiasoft.samples;
/**
* Title: SearchDocuments - a sample that searches for documents in 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.
*
* @version 2.0
* Modified: 2011-01-04
*/
import com.ixia.textmlserver.*;
import java.io.*;
import java.util.*;
public class MultiQuerySearch
{
final static String TOKEN_USER = "USER";
final static String TOKEN_PASSWORD = "PASSWORD";
final static String TOKEN_SERVER = "SERVER";
final static String TOKEN_DOCBASE = "DOCBASE";
final static String TOKEN_QUERYFILE1 = "QUERYFILE1";
final static String TOKEN_QUERYFILE2 = "QUERYFILE2";
final static String TOKEN_QUERYFILE3 = "QUERYFILE3";
// Valid parameters accepted from command-line
final static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE,
TOKEN_QUERYFILE1, TOKEN_QUERYFILE2, TOKEN_QUERYFILE3 };
// Which parameters must be specified
final static boolean[] mandatory =
{ true , true , true , true ,
true , true , true };
final static String newLine = System.getProperty("line.separator") ;
// If run-time parameters are missing or invalid, display Help
private static void Usage()
{
System.out.println
("MultiQuerySearch user=<domain\\user> password=<password> " +
"server=<serverName> docbase=<docBaseName> " +
"queryfile1=<[path\\]queryFileName1> " +
"queryfile2=<[path\\]queryFileName2> " +
"queryfile3=<[path\\]queryFileName3>");
System.out.println
("\t<domain\\user> Name of the user used for security purpose");
System.out.println
("\t<password> Password of the user");
System.out.println
("\t<ServerName> Name of TEXTML Server instance");
System.out.println
("\t<DocBaseName> Document base name");
System.out.println
("\t<[path\\]queryFileName1> First query searching docbase");
System.out.println
("\t<[path\\]queryFileName2> Second query searching docbase");
System.out.println
("\t<[path\\]queryFileName3> " +
"Query searching combined ResultSpaces of first two queries");
}
// Extract run-time parameters from command-line
private static HashMap Extract(String [] args)
{
HashMap retval = new HashMap(10);
for (int i = 0; i < args.length; ++i)
{
StringTokenizer tokens = new StringTokenizer(args[i], "=", false);
String token = null, value = null;
if (tokens.hasMoreElements())
token = tokens.nextToken();
if (tokens.hasMoreElements())
value = tokens.nextToken();
if (token == null || value == null)
{
retval.clear();
return retval;
}
boolean found = false;
for (int j = 0; j < validTokens.length && !found; ++j)
{
if (validTokens[j].equalsIgnoreCase(token) &&
!retval.containsKey(validTokens[j]))
{
retval.put(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 the query file, and return its contents as a String object
private static String ReadQuery(String fileName) throws java.io.IOException
{
FileInputStream stream = new FileInputStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, "UTF-16");
StringBuffer buff = new StringBuffer(stream.available());
try
{
while (reader.ready())
buff.append((char)reader.read());
}
finally
{
reader.close();
}
return buff.toString();
}
//*** This function is never used ***
private static byte[] ReadFile(File file) throws java.io.IOException
{
FileInputStream f = new FileInputStream(file);
byte[] retval = new byte[f.available()];
try
{
f.read(retval);
}
finally
{
f.close();
}
return retval;
}
public static void main(String[] args)
{
String user = null;
// Parse the command line
HashMap map = Extract(args);
if (map.isEmpty())
{
Usage();
return;
}
// Validate the command-line parameters
user = (String) map.get(TOKEN_USER);
if (user.indexOf("\\") == -1)
{
Usage();
return;
}
// Prepare parameters for ClientServicesFactory
HashMap<String, String> parms = new HashMap<String, String>(1);
try
{
// Get the ClientServices object
ClientServices cs =
com.ixia.textmlserver.ClientServicesFactory.getInstance
("CORBA", parms);
// 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.get(TOKEN_PASSWORD));
// Read the queries from the query files
String query1 = ReadQuery((String)map.get(TOKEN_QUERYFILE1));
String query2 = ReadQuery((String)map.get(TOKEN_QUERYFILE2));
String query3 = ReadQuery((String)map.get(TOKEN_QUERYFILE3));
try
{
// Get the ServerServices for the specified server
IxiaServerServices ss =
cs.ConnectServer((String) map.get(TOKEN_SERVER));
try
{
// Get the DocbaseServices for the specified docbase
IxiaDocBaseServices docbase =
ss.ConnectDocBase((String) map.get(TOKEN_DOCBASE));
try
{
// Get the SearchServices for the docbase
IxiaSearchServices search = docbase.SearchServices();
// Display the three specified queries.
// A set of three sample queries for this sample program is in the
// program files directory for TEXTML Server
System.out.println("Executing query1:");
System.out.println(query1);
System.out.print(newLine) ;
System.out.println("Then, executing query2:");
System.out.println(query2);
System.out.print(newLine) ;
System.out.println("Finally, executing query3:");
System.out.println(query3);
System.out.print(newLine) ;
System.out.print(newLine) ;
// Run query1, which searches the docbase and stores the result in result1.
// Display the number of documents retrieved.
IxiaResultSpace result1 = search.SearchDocuments(query1);
System.out.println ("Query1 retrieved: " + result1.Count() + " documents.");
// Run query2, which searches the docbase and stores the result in result2.
// Display the number of documents retrieved.
IxiaResultSpace result2 = search.SearchDocuments(query2);
System.out.println ("Query2 retrieved: " + result2.Count() + " documents.");
// Run query3, which searches result1 and result2,
// and stores its own result in result3.
// Display the number of documents retrieved.
IxiaResultSpace result3 = search.SearchDocuments(query3);
System.out.println ("Query3 retrieved: " + result3.Count() + " documents.");
result1.Release();
result2.Release();
result3.Release();
search.Release();
}
finally
{
docbase.Release();
}
}
finally
{
ss.Release();
}
}
finally
{
// Don't forget to logout
cs.Logout();
}
}
catch (Exception e)
{
System.err.println("Exception occurred: ");
e.printStackTrace(System.err);
}
}
}