Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search box Parsing Issue

Status
Not open for further replies.

Beta14

MIS
Jan 25, 2010
3
US
Hi,

I have a search box on a html page that is not working properly. I think changing the javascript might be the answer but I am not sure. (I am new to this). Basically the search box has a radio button operator with two choices: "AND" and "OR"

When a user types a search item in such as the terms: GUI Graphical User Interface, the search does not know that GUI and "Graphical User Interface" are two separate terms so it interprets the AND selection to be: GUI AND Graphical AND User AND Interface. The OR selection is interpreted as: GUI OR Graphical OR User OR Interface.

If the search was working correctly, I would enter the search text GUI OR "Graphical User Interface" and it would know that I would like to search by either term. You must use the quotes to enclose a phrase that includes spaced between words.

Below is the relevant code for how the words are being parsed:

Code:
if(arrayTemp[0] == "searchString") {
 strTemp = ''
 arrayKey [j] = arrayTemp[0];
 arrayTempKeyword = arrayTemp[1].split('+');
 for(k=0; k<arrayTempKeyword.length; k++) {
 strTemp += unescape(arrayTempKeyword[k]) + " ";
 }//for
 strTemp = strTemp.substring(0,strTemp.length-1);
 arrayValue [j] = strTemp
 }
Anyone have any ideas?

Thanks in advance for this!
 
While you could parse the search string using client-side JavaScript, I'm not sure why you would want to, as surely your search service would handle all of this for you?

What are you actually using to perform the search?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
The search box queries a XML file using XPath.

This code will basically take each word from the query string and parse it into a XML array. If I can change the parsing logic so that it keeps phrases together and single words individual (based on whether it is surrounded by quotations) then that is definitely part of the battle. The other part will be determining when to use the AND or OR operator depending on where I encounter those words in the query string.

Here is my code for the XML array query:


Code:
// ActiveX
var xmlDoc;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.load("Directory.xml");

//// SEARCH EXTRACT
function searchCatalog(searchElement,searchTerm) 
{
	// Get the search term from the 'searchText' form field

	var assets = xmlDoc.getElementsByTagName("Directory");
	arrayResults = new Array;
    arrayAllKeywords = searchTerm.split(' ');
	
	for (var i=0;i<assets.length;i++) 
	{
	    for(z=0; z<arrayAllKeywords.length; z++)
	    {
	        if(searchType == "OR"){
		        // Find XML elements that contain the search term
	            var assetElement_or = assets[i].getElementsByTagName(searchElement)[0].childNodes[0].nodeValue;
	            var exp_or = new RegExp(arrayAllKeywords[z],"i");		//The "i" attribute indicates a case "i"nsensitive match
    		                
                // Store XML elements in an array
                if(arrayResults.length == 0 && assetElement_or.match(exp_or) != null) {
		            arrayResults.push(assets[i]);
		            arrayKeywords.push(assets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + "_" + arrayAllKeywords[z]);
	            }//if
	            else if (assetElement_or.match(exp_or) != null) {
	                for(y=0; y<arrayResults.length; y++) {
	                    var or_counter = 0;
	                    if (arrayResults[y] == assets[i])
	                        or_counter += 1;
	                }//for
	                if (or_counter == 0)
	                    arrayResults.push(assets[i]);
	                arrayKeywords.push(assets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + "_" + arrayAllKeywords[z]);
	            }//if
    	    }//if
	        if(searchType == "AND"){
		        // Find XML elements that contain the search term
	            var assetElement_and = assets[i].getElementsByTagName(searchElement)[0].childNodes[0].nodeValue;
	            var matches = 0;
                
                // Store XML elements in an array
                
                for(l=0;l<arrayAllKeywords.length;l++) {
                    exp_and = new RegExp(arrayAllKeywords[l],"i");		//The "i" attribute indicates a case "i"nsensitive match
                    if (assetElement_and.match(exp_and) != null)
                        matches += 1;
                }//for
                if (arrayResults.length == 0 && matches == arrayAllKeywords.length){
                    arrayResults.push(assets[i]);
                    arrayKeywords.push(assets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + "_" + arrayAllKeywords[z]);
                }//if
                else if (matches == arrayAllKeywords.length) {
	                for(y=0; y<arrayResults.length; y++) {
	                    var and_counter = 0;
	                    if (arrayResults[y] == assets[i])
	                        and_counter += 1;
	                }//for
	                if (and_counter == 0)
	                    arrayResults.push(assets[i]);
	                arrayKeywords.push(assets[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + "_" + arrayAllKeywords[z]);
	            }//if
            }//if
        }//for
	}//for
	
	// Pass results to a display function
	showResults(arrayResults, searchTerm);
}


Can you help? I didn't write this code so might not be what you asked for.
 
Why don't you use multiple input text field for the search "keyword"? but instead rely on the discipline of users in entering a single text field! Use multiple text field would be clean and unambiguous.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top