Introduction
My project is Exact Phrase Website Live Search Script ASP.NET and works well in .NET Framework 2.0 and above. Script can be used in ASP.NET Websites: localhost, www.mydomain.com, etc. I have programmed this script in C#. This script is a free utility which searches out the entire domain.com, the text files which include: .html, .htm, .txt, .rtf, .nfo., .doc, .php, .asp, .aspx, .php4, .php5, .xml, etc. and according to your own customization. You can search for an exact keyword or an exact phrase. Limitations can be further imposed by the Administrator of this script. You can upgrade this script into far more advanced web applications. By using this Search Script, you do not require any database, neither any temporary file creation, nor temporary space. Search is done right live into the website, in the server which runs the website! In one go, in a matter of only two seconds on the Xeon server, the search completes parsing out at least 100 large web pages and indexes the searched files right away. Due to live searching, it gives load on the entire physical server, therefore this script can be used in super servers and in universities, colleges, libraries, and in schools. This Script is a full web application and solves the problem of website live searching in the websites of those who do not know programming or simply wish to have website searching for keywords and phrases.
Extended Information
This is version 2.0 + Revision 8 of the older version 1.0 of the Script. Put this ASP.NET script into yourdomain.com/<folder> and search the entire website for an exact word or a phrase. This script is freeware, for personal use, and for educational purposes. You can implement this script in universities, schools, and public libraries to search all* files on the website. The script is upgradable. Currently, because the script searches out files live, it does not require any database, file, or any temporary space to index files, but it puts load on Server. What it requires is memory and load on Server. Whatever new is stored on server, this Script processes even the new files. The phrase or the keyword you specify is searched exactly as you had specified in the search form. It is searched exactly as it is specified by the user in the search engine form. All search phrases are searched in lower case so that if the user did not know if the word he is searching for is in lower case or in upper case alphabets, the search will seek out all such matching words or phrase. 99.9% of guaranteed exact search is found when matched if it exists in a valid parsed file (.html, .htm, .txt., .nfo, .rtf), so this script fits the public libraries to search for books, titles, names, etc. from the configured file extensions.
Background
This is a free search engine module written in ASP.NET and C#. It contains what a demanding educational, or a free webmaster requires in his website. The script is upgradable because it is driven totally by methods/functions, is structured and organized, is not congested with random coding.
Using the Code
Download the code, extract the files into <localhost>/search folder, or in <domain.com>/search folder directly in your website. If you have ASP.NET, the script will be executed from primary file: phrasesearch.aspx which will then display a search engine form. The phrasesearch.aspx.cs file is the primary C# code file in which at the top are some constant or global variables required to be configured manually. But there is no need to configure them unless required. There are only four-six variables and are easily understood by the programmers who have knowledge of web development and ASP.
In phrasesearch.aspx.cs, these are the global variables which are only "optional". There is actually no need to configure unless errors arise.
public const String mywebsiteaddr = "";
public const String mydomain = "";
public const String mydomainrootaddr = "";
public const String mysearchdir = "/";
public const Boolean isSetupDone = false;
public static int myfileslimit = 100;
public const String myphysicalpath = "";
public static String[] arrayFileTypesToSearch =
{".htm", ".html", ".txt", ".nfo", ".rtf", ".doc", ".xml", ".php",
".asp", ".aspx", ".php4", ".php5"};
These variables above are configured manually if the script does not work as expected.
public static Boolean initializeSiteSearching()
{
Boolean returnval = false;
strSearchPhrase = "";
inumResultsLimit = 0;
strDomainRootDir = "";
strDomainUrl = "";
boolSearchingDone = false;
arrayPhraseMatches = null;
iPhraseMatchCount = 0;
arrayPhrasePositions = null;
numMatchingFilesFound = 0;
iCountSearchedFiles = 0;
iFilesLimitReached = false;
try
{
strDomainRootDir = HttpContext.Current.Server.MapPath("/");
}
catch (Exception)
{
strDomainRootDir = HttpContext.Current.Server.MapPath("~/");
}
strDomainUrl = HttpContext.Current.Request.Url.Host;
strDomainPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (myfileslimit <= 0)
myfileslimit = 1000;
if (strDomainRootDir.Length > 0 && strDomainUrl.Length > 0)
{
returnval = true;
}
return returnval;
}
This function initiates the search.
Some Functions in the Code
-
public static string PageName()
Returns the script <filename.aspx> filename. This function is optional.
-
public static Boolean initializeSiteSearching()
This function is initialized only when the new search is about to take place. This function resets all the required variables to null
or 0
values. To automate the task so that no one ASP.NET function is called limitless times while parsing uncountable number of files, the function also obtains global website related values including the website URL, domain name, and the port number on which the website is running. This function also sets a global configurable variable, a limit of the number of files to be searched in one go.
-
public static String GenerateInternalLink(String strPath)
This function is called whenever a valid file is parsed and it is about to be indexed live on the search results page. This function is important because it removes the physical path of the file, converts the file to a relative URL path, then returns the URL relative path to be given in the indexed result as a URL or the hyperlink of the file which is being indexed for matched pattern. This function is important for resolving Website related paths.
-
public static String GetRelavitePathOfFile(String filepath)
This function is also called whenever a valid file is parsed and is about to be indexed. The function parses the physical path relative to the localhost. This is the primary function for converting the physical path to the website relative path/URL. Suppose if this function returns an error but not the processed website relative URL, then only the GenerateInternalLink
is called as the final method to obtain a website relative URL. These two methods are upgradable and are called in FindMatchingCurrentFile()
method in which the required automated tasks are performed such as: Extracting the entire data of the file, stripping tags, etc. and then matching the user's specified phrase to the text contained in the file buffer. If the text is matched, any number of times, the file is then indexed, title obtained from the HTML file, and the link is produced so that the user can click the link to view the file in which the user's specified phrase or the keyword are present. This function uses GenerateInternalLink()
function for help in resolving paths.
-
public static Boolean FindMatchingCurrentFile(string path)
This function performs file related tasks, tries to search any match of the user specified phrase and indexes the file into the results section when any matched pattern is found in the file. This is the primary file and indexing related function.
Some Sample Code Snippets
public static String GenerateInternalLink(String strPath)
{
String strfinalurl = "";
String strPathstripped = "";
String strDomainRootStripped = "";
String strDomainRootPath = "";
try
{
strDomainRootPath = HttpContext.Current.Request.MapPath("/");
}
catch (Exception)
{
strDomainRootPath = HttpContext.Current.Request.MapPath
(HttpContext.Current.Request.ApplicationPath);
}
strPathstripped = Replace(strPath, @"\\", "/", -1, 0,
RegexOptions.IgnoreCase | RegexOptions.Singleline);
strDomainRootStripped = Replace(strDomainRootPath, @"\\",
"/", -1, 0, RegexOptions.IgnoreCase | RegexOptions.Singleline);
strfinalurl = Replace(strPathstripped, strDomainRootStripped,
"", -1, 0, RegexOptions.IgnoreCase | RegexOptions.Singleline);
return strfinalurl;
}
public static String GetRelavitePathOfFile(String filepath)
{
String strresult = "";
String strfinalurl = "";
String strRootPath = "";
try
{
strRootPath = HttpContext.Current.Request.MapPath("/");
}
catch (Exception)
{
strRootPath = HttpContext.Current.Request.MapPath
(HttpContext.Current.Request.ApplicationPath);
}
if (myphysicalpath.Length > 0)
{
strfinalurl = Replace(filepath, strRootPath, "",
-1, 0, RegexOptions.IgnoreCase | RegexOptions.Singleline);
String modphysicalpath = Replace(strfinalurl, @"\\", "/",
-1, 0, RegexOptions.IgnoreCase | RegexOptions.Singleline);
strfinalurl = Replace(strfinalurl, @"\\", "/",
-1,0,RegexOptions.IgnoreCase | RegexOptions.Singleline);
strresult = Replace(strfinalurl, modphysicalpath, "",
-1, 0, RegexOptions.IgnoreCase | RegexOptions.Singleline);
return strresult;
}
String strfileurl = GenerateInternalLink(filepath);
if (isSetupDone == false)
{
if (thisDomainName == "localhost")
{ strfileurl = "http://localhost:" + strDomainPort + "/" + strfileurl; }
else { strfileurl = "http://" + thisDomainName + ":" +
strDomainPort + "/" + strfileurl; }
return strfileurl;
}
else
{
if (mydomain == "localhost")
{strfileurl = "http://localhost:" + strDomainPort + "/" + strfileurl; }
else { strfileurl = "http://" + mydomain + "/" + strfileurl; }
return strfileurl;
}
return "Error in GetRelativePathOfFile method";
}
Code/User Interface Language
Language is English.
Conclusion
This is my second release and is the latest one, it runs on IIS+, .NET 2.0 or newer, is fully automated and bugs are fixed. Contact me at: aroratushar@gmail.com if any problem arises.
Points of Interest
I was fascinated with keywords and phrase searching, and so I tried to make a script based Search Engine. I love .NET and fund of programming in Visual Studio, Visual Basic, and in .NET. I felt happy to create my own search engine and found this script very useful, for those who need it.
History
- 17th August 2010: New updates and bug fixes
- 27th August 2009: First release