I've played around with IBM's Omnifind Yahoo! Edition and came up with these little procedures. For those reading this article, you'll know there isn't much on the web regarding this topic. Hopefully this will help in your quest to integrate the search engine into a web/from application before a flash of peripeteia forces you otherwise.
(I would like to apologize to Q for not properly acknowledging his code. This code is all you, man.)
Searching:
First, place an XML and textbox control onto your aspx page.
<asp:TextBox runat="server" ID="txtSearch" /><br><br>
<asp:Xml runat="server" TransformSource="~/search.xsl" EnableViewState="false" ID="xmlSearch" />
Now, you need to build your query string that will power the search in the code behind. All this can be placed into an OnClick event of a button, link, or custom control.
string strSearch = txtSearch.Text;
string strRequest = "http://[omnifindserver:port#]/api/search?query=" + strSearch + "&collection=Default";
I've specified the collection so it won't attempt to look through all collections. Note: In the Yahoo! Edition, you're allowed only 5 collections. For a list of all available parameters, see
here.
Finally, query the collection and read in the resulting XML to your control.
string strResults = "";
strResults = new System.Net.WebClient().DownloadString(strRequest);
xmlSearch.DocumentContent = strResults;
That's it for a quick way to search your index. You can add more parameters, create a XSLT sheet to format the results, or specify how to return the results.
Adding Documents to the Index manually: (This is my code)
Create the location to which your OmniFind Server resides and the necessary credentials to access it. The username and password are exactly what you use to log into the Administration Console of the OmniFind Server.
For this, you'll need the following namespaces:
System.Net, System.Text
Now, create connection and credentials necessary to interact with Omnifind:
string strLocation = "http://[omnifindserver:port#]/api/document";
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(strLocation), "Basic", new NetworkCredential("username", "password"));
You'll need to encode the document. Keep in mind, you'll need to read your documents and store the raw data into an encoded format. In our example, we are pushing an aspx page.
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] htmlBytes = encoding.GetBytes(htmlText); //I read the generated data of the aspx page into this variable earlier (not shown in this blog)
Build the HTTPWebRequest:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strLocation);
request.Method = "POST";
request.UserAgent = "exrobinson"; //If you specified a useragent the set it here
request.Credentials = myCache;
request.Headers.Add("action", "addDocument");
request.Headers.Add("collection", "Default");
request.Headers.Add("docId", "full path of document");
request.Headers.Add("docType", "text/xhtml"); //change type to what you need, it is important you match the correct type
request.Headers.Add("Authorization", myCache.ToString());
request.ContentLength = htmlBytes.Length;
Now we try to "push" the document into the index.
try
{
using (Stream streamWrite = request.GetRequestStream())
{
if (streamWrite.CanWrite)
{
streamWrite.Write(htmlBytes, 0, htmlBytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader strRead = new StreamReader(streamResponse);
lblMessage.Text = strRead.ReadToEnd();
response.Close();
strRead.Close();
streamResponse.Close();
}
}
catch (WebException webError)
{
lblMessage.Text = webError.Message.ToString();
}
Note, if document succesfully is added to the index, the strRead will be empty. You can now easily place this into a loop to put in multiple documents.
The end. Let us know if you have questions.
65ae9494-b9e8-4b93-aeeb-e76b57767871|0|.0
ASP.NET, C#
omnifind