Thursday, Jan 26, 2006, 5:33 PM in Tools
Calling a Remote Index Server from .NET
I was building some code to access Index Server's results via .NET and I got this:
// Catalog specified in connection string
string query = "select Path from Scope() where FREETEXT('foo')";
using( OleDbConnection conn = new OleDbConnection("Provider=MSIDXS.1;Data Source=MyCatalog") ) {
conn.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter(query, conn);
DataSet ds = new DataSet();
cmd.Fill(ds, "SearchResults");
DataTable table = ds.Tables[0];
ds.Tables.Remove(table);
return table;
}
This came from a combo of stuff I found on the net and my own experimenting. However, I had to get an MS employee who know IS (thanks Chad Ray!) to figure out how to access IS remotely, which I was finally able to get working thusly:
// Server and catalog specified in query string, not connection string
string query = "select Path from MyServer.MyCatalog..Scope() where FREETEXT('foo')";
using( OleDbConnection conn = new OleDbConnection("Provider=MSIDXS.1") ) {
conn.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter(query, conn);
DataSet ds = new DataSet();
cmd.Fill(ds, "SearchResults");
DataTable table = ds.Tables[0];
ds.Tables.Remove(table);
return table;
}
Also, if the server or catalog name has anything "weird" in it, e.g. those wacky dashes, you're supposed to wrap it in double quotes. My experimenting showed that wrapping any server or catalog name in double quotes worked just fine, e.g.
string query = "select Path from \"MyServer\".\"MyCatalog\"..Scope() where FREETEXT('foo')";
However, if the server name is "localhost" or "127.0.0.1", IS doesn't like it, which meant that I had to check for those server names and drop the ones that used the loopback adapter (although using the machine name of the local machine worked just fine), e.g.
static bool IsLocalHost(string server) {
return string.IsNullOrEmpty(server) || (string.Compare(server, "localhost", true) == 0) || (string.Compare(server, "127.0.0.1") == 0);
}
string GetPlainTextQueryString(string text, string columns) {
// IS doesn't like "localhost"
if( IsLocalHost(_server) ) {
return string.Format("select {0} from \"{1}\"..Scope() where FREETEXT('{2}')", columns, _catalog, text);
}
else {
return string.Format("select {0} from \"{1}\".\"{2}\"..Scope() where FREETEXT('{3}')", columns, _server, _catalog, text);
}
}
I'm only posting this 'cuz I couldn't find it on the net and I want to be able to find it again later. : )